What is Syntax?
Syntax is, first and foremost, a lexical analysis framework. It supports pluggable syntax modules, and comes with modules for Ruby, XML, and YAML.
What does it mean, a “lexical analysis framework”? It means you can use Syntax to take a body of text representing instructions in some syntax (like Ruby), and break that text into tokens.
What is that good for? Well, a few things. You could concievably use it to count the number of classes in a system, or the number of lines of code per method. But Syntax was written specifically with syntax highlighting in mind.
This was written by Jamis Buck, but has not been updated since 2005. The original RubyForge project can be found here.
Because Syntax was written primarily to support syntax highlighting, that is going to be the task most clients of the library will use it for. So, to get you up and running quickly, converting, for example, Ruby code to HTML, consider the following:
Convert a Ruby script to HTML [ruby]
require 'syntax/convertors/html' convertor = Syntax::Convertors::HTML.for_syntax "ruby" html = convertor.convert( File.read( "program.rb" ) ) puts html
The above script will convert a Ruby file called program.rb to HTML, writing the HTML to stdout. HTML span tags will be used in conjunction with “class” attributes to describe the various tokens—this allows you to use CSS to colorize the result.
Note: currently, only HTML conversion is supported, but it should be possible to create a convertor that takes the tokenized output and emits other formats, like LaTeX, PDF, or Rich Text.
For the rest of library’s documentation visit http://syntax.rubyforge.org/.