Class: Syntax::Convertors::HTML

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

Overview

A simple class for converting a text into HTML.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokenizer) ⇒ HTML

Creates a new HTML convertor that uses the given tokenizer.



16
17
18
# File 'lib/syntax/convertors/html.rb', line 16

def initialize( tokenizer )
  @tokenizer = tokenizer
end

Class Method Details

.for_syntax(syntax) ⇒ Object

A convenience method for instantiating a new HTML convertor for a specific syntax.



11
12
13
# File 'lib/syntax/convertors/html.rb', line 11

def self.for_syntax( syntax )
  new( Syntax.load( syntax ) )
end

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.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/syntax/convertors/html.rb', line 23

def convert( text, pre=true )
  html = ""
  html << "<pre>" if pre
  @tokenizer.tokenize( text ) do |tok|
    if tok.group == :normal
      html << html_escape( tok )
    else
      html << "<span class=\"#{tok.group}\">#{html_escape(tok)}</span>"
    end
  end
  html << "</pre>" if pre
  html
end