Class: Syntax::Convertors::HTML

Inherits:
Abstract
  • Object
show all
Defined in:
lib/zena/code/default_syntax.rb

Overview

A simple class for converting a text into HTML.

Instance Method Summary collapse

Instance Method Details

#convert(text, pre = true) ⇒ Object

Converts the given text to HTML, using spans to represent token groups of any type but :normal (which is always unhighlighted). If pre is true, the html is automatically wrapped in pre tags.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/zena/code/default_syntax.rb', line 107

def convert( text, pre=true )
  html = ""
  html << "<pre>" if pre
  regions = []
  @tokenizer.tokenize( text ) do |tok|
    value = tok.escape ? html_escape(tok) : 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