| Path: | README |
| Last Update: | Tue Dec 13 09:06:52 Pacific Standard Time 2005 |
Syntaxi is a class for formatting code blocks in your HTML. It can:
Installing Syntaxi is as easy as installing the gem. From your command line run the following:
gem install syntaxi
This will download and install Syntaxi and Jamis Buck’s Syntax gem.
Syntaxi requires the Syntax library to do syntax coloring, and will not function without it. For more information on Syntax, visit syntax.rubyforge.org.
In order for Syntaxi to do its magic, you’ll need to write your code blocks like this:
[code lang="ruby"]
def foo
puts 'bar'
end
[/code]
The square bracket syntax allows Syntaxi to work seamlessly in HTML, Markdown, and Textile alike. The lang attribute specifies the language of the code block.
Having Syntaxi format your code containing the special code blocks is easy. Lets say your text is stored in text. To format this text simply do the following:
require 'syntaxi' formatted_text = Syntaxi.new(text).process
If the lang attribute you specified in your opening code block tag is recognized by Jamis Buck’s Syntax gem, the block will have syntax coloring applied to it automatically. See the documentation for Syntax to learn more about how to style the different spans it produces.
Syntaxi can also add line numbers to your code blocks. It can do this in two different ways:
By default, line numbering is set to inline. To change the line number method, simply call line_number_method= like so:
Syntaxi::line_number_method = 'floating'
Valid values are ‘none’, ‘inline’, and ‘floating’
Because your code blocks will often have lines that are longer than the available width, Syntaxi can wrap long lines for you. If possible the line break will be done on a space. No line numbers will appear on the wrapped portions of long lines (but subsequent lines will still have the right line numbers).
By default, line wrap is enabled and wraps on column 60. To disable line wrap, simply use wrap_enabled= like this:
Syntaxi::wrap_enabled = false
To change the column on which to wrap, call wrap_at_column= like this:
Syntaxi::wrap_at_column = 70
Since the configuration options are stored as class variables, you need only change them once, and all future instances of Syntaxi will use the new values. If your entire application will use the same values, simply set the values to your liking at the beginning of execution. In Ruby on Rails, an ideal place to do this is in [RAILS_ROOT]/config/environment.rb. It would look something like this:
require 'syntaxi' Syntaxi::line_number_method = 'floating' Syntaxi::wrap_enabled = false Syntaxi::wrap_at_column = 70