Class: Guides::TextileTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/guides/textile_transformer.rb

Constant Summary collapse

LANGUAGES =
{ "ruby" => "ruby", "sql" => "sql", "javascript" => "javascript",
"css" => "css", "plain" => "plain", "erb" => "ruby; html-script: true",
"html" => "xml", "xml" => "xml", "shell" => "plain", "yaml" => "yaml" }
NOTES =
{ "CAUTION" => "warning", "IMPORTANT" => "warning", "WARNING" => "warning",
"INFO" => "info", "TIP" => "info", "NOTE" => "note" }

Instance Method Summary collapse

Constructor Details

#initialize(production = false) ⇒ TextileTransformer

Returns a new instance of TextileTransformer.



21
22
23
# File 'lib/guides/textile_transformer.rb', line 21

def initialize(production=false)
  @production = production
end

Instance Method Details

#consume_constructionObject



78
79
80
81
82
83
# File 'lib/guides/textile_transformer.rb', line 78

def consume_construction
  match = scan_until(%r{</construction>})
  unless @production
    @string = match.pre_match + @string
  end
end

#consume_note(css_class) ⇒ Object



71
72
73
74
75
76
# File 'lib/guides/textile_transformer.rb', line 71

def consume_note(css_class)
  match = scan_until /(\r?\n){2,}/ # We need at least 2 line breaks but we want to match as many as exist
  note = match.pre_match.gsub(/\n\s*/, " ")
  note = RedCloth.new(note, [:lite_mode]).to_html
  @output << %{<div class="#{css_class}"><p>#{note}</p></div>\n}
end

#flush_textileObject



85
86
87
88
# File 'lib/guides/textile_transformer.rb', line 85

def flush_textile
  @output << RedCloth.new(@pending_textile).to(HTMLFormatter) << "\n"
  @pending_textile = ""
end

#generate_brushes(tag, replace, filename) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/guides/textile_transformer.rb', line 57

def generate_brushes(tag, replace, filename)
  match = scan_until %r{</#{tag}>}
  @output << %{<div class="code_container">\n}
  @output << %{<div class="filename">#{filename}</div>\n} if filename
  @output << %{<pre class="brush: #{replace}; gutter: false; toolbar: false">\n} <<
             CGI.escapeHTML(match.pre_match) << %{</pre></div>}
end

#scan_until(regex) ⇒ Object



65
66
67
68
69
# File 'lib/guides/textile_transformer.rb', line 65

def scan_until(regex)
  match = @string.match(regex)
  @string = match.post_match
  match
end

#transform(string) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/guides/textile_transformer.rb', line 25

def transform(string)
  @string = string.dup

  @output  = ""
  @pending_textile = ""

  until @string.empty?
    notes     = NOTES.keys.map {|note| "#{note}" }.join("|")
    languages = LANGUAGES.keys.join("|")

    match = scan_until /(\+(\S.*?\S?)\+|<(#{languages})(?: filename=["']([^"']*)["'])?>|(#{notes}): |<(construction)>|\z)/m

    @pending_textile << match.pre_match

    if match[2]    # +foo+
      @pending_textile << "<notextile><tt>#{CGI.escapeHTML(match[2])}</tt></notextile>" if match[2]
    elsif match[3] # <language>
      flush_textile
      generate_brushes match[3], LANGUAGES[match[3]], match[4]
    elsif match[5] # NOTE:
      flush_textile
      consume_note NOTES[match[5]]
    elsif match[6] # <construction>
      consume_construction
    end
  end

  flush_textile

  @output
end