Class: Syntax::Convertors::HTML

Inherits:
Abstract
  • Object
show all
Defined in:
lib/syntax/convertors/html.rb

Overview

A simple class for converting a text into HTML.

Instance Attribute Summary

Attributes inherited from Abstract

#tokenizer

Instance Method Summary collapse

Methods inherited from Abstract

for_syntax, #initialize

Constructor Details

This class inherits a constructor from Syntax::Convertors::Abstract

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.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/syntax/convertors/html.rb', line 12

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