class Syntax::Convertors::HTML
A simple class for converting a text into HTML.
def convert( text, pre=true )
of any type but :normal (which is always unhighlighted). If
Converts the given text to HTML, using spans to represent token groups
def convert( text, pre=true ) html = "".dup html << "<pre>" if pre regions = [] @tokenizer.tokenize( text ) do |tok| value = html_escape(tok) case tok.instruction when :region_close then regions.pop html << "</span>" when :region_open then regions.push tok.group html << "<span class=\"#{tok.group}\">#{value}" else if tok.group == ( regions.last || :normal ) html << value else html << "<span class=\"#{tok.group}\">#{value}</span>" end end end html << "</span>" while regions.pop html << "</pre>" if pre html end
def html_escape( string )
def html_escape( string ) string.gsub( /&/, "&" ). gsub( /</, "<" ). gsub( />/, ">" ). gsub( /"/, """ ) end