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.
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:
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.
Incidentally, this entire document uses the Syntax library to colorize the Ruby code snippets!
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.