Class: String

Constants

PROTECTED_TAGS
%w[tt code pre]
VERBATIM_TAGS
%w[noformat]

Public Visibility

Public Instance Method Summary

#thru_coderay

Adds syntax coloring to <code> elements in the given text.

#thru_maruku(inline = false)

Returns the result of running this string through Maruku.

#to_inline_xhtml

Transforms this string into an inline XHTML string (one that does not contain any block-level XHTML elements at the root).

#to_xhtml(inline = false)

Transforms this string into XHTML while ensuring that the result contains one or more block-level elements at the root.

Public Instance Method Details

thru_coderay

public thru_coderay

Adds syntax coloring to <code> elements in the given text. If the <code> tag has an attribute lang="…", then that is considered the programming language for which appropriate syntax coloring should be applied. Otherwise, the programming language is assumed to be ruby.

[View source]


81
82
83
84
85
86
87
88
89
90
# File 'lib/erbook/to_xhtml.rb', line 81

def thru_coderay 
gsub %r{<(code)(.*?)>(.*?)</\1>}m do
  atts, code = $2, CGI.unescapeHTML($3).sub(/\A\r?\n/, '')
  lang = atts[/\blang=('|")(.*?)\1/i, 2] || :ruby

  html = CodeRay.scan(code, lang).html(:css => :style)
  tag = if code =~ /\n/ then :pre else :code end

  %{<#{tag} class="code"#{atts}>#{html}</#{tag}>}
end

thru_maruku

public thru_maruku(inline = false)

Returns the result of running this string through Maruku.

inline:If true, the resulting XHTML will not be wrapped in a XHTML paragraph element.
[View source]


71
72
73
74
75
# File 'lib/erbook/to_xhtml.rb', line 71

def thru_maruku inline = false 
  html = Maruku.new(self).to_html
  html.sub! %r{\A<p>(.*)</p>\Z}, '\1' if inline
  html
end

to_inline_xhtml

public to_inline_xhtml

Transforms this string into an inline XHTML string (one that does not contain any block-level XHTML elements at the root).

[View source]


33
34
35
# File 'lib/erbook/to_xhtml.rb', line 33

def to_inline_xhtml
  to_xhtml true
end

to_xhtml

public to_xhtml(inline = false)

Transforms this string into XHTML while ensuring that the result contains one or more block-level elements at the root.

inline:If true, the resulting XHTML will not contain a block-level element at the root.
[View source]


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/erbook/to_xhtml.rb', line 43

def to_xhtml inline = false
  with_protected_tags(self, VERBATIM_TAGS, true) do |text|
    html = with_protected_tags(text, PROTECTED_TAGS, false) do
      |s| s.thru_maruku inline
    end

    # Markdown's "code spans" should really be "pre spans"
    while html.gsub! %r{(<pre>)<code>(.*?)</code>(</pre>)}m, '\1\2\3'
    end

    # allow user to type <pre> blocks on single lines
    # without affecting the display of their content
    html.gsub! %r{(<pre>)[ \t]*\r?\n|\r?\n[ \t]*(</pre>)}, '\1\2'

    # ensure tables have a border: this *greatly* improves
    # readability in text-based web browsers like w3m and lynx
    html.gsub! %r/<table\b/, '\& border="1"'

    # add syntax coloring
    html.thru_coderay
  end
end