Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/gerbil/html.rb
Constant Summary collapse
- PROTECTED_TAGS =
The content of these HTML tags will be preserved while they are being processed by Textile. By doing this, we avoid unwanted Textile transformations, such as quotation marks becoming curly ( ), in source code.
%w[tt code pre]
- VERBATIM_TAGS =
The content of these HTML tags will be preserved verbatim throughout the text-to-HTML conversion process.
%w[noformat]
Instance Method Summary collapse
-
#thru_coderay ⇒ Object
Adds syntax coloring to <code> elements in the given text.
-
#thru_redcloth ⇒ Object
Returns the result of running this string through RedCloth.
-
#to_html ⇒ Object
Transforms this string into HTML.
Instance Method Details
#thru_coderay ⇒ Object
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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/gerbil/html.rb', line 90 def thru_coderay gsub %r{<(code)(.*?)>(.*?)</\1>}m do atts, code = $2, CGI.unescapeHTML($3) lang = if $2 =~ /lang=('|")(.*?)\1/i $2 else :ruby end tag = if code =~ /\n/ :pre else :code end html = CodeRay.scan(code, lang).html(:css => :style) %{<#{tag} class="code"#{atts}>#{html}</#{tag}>} end end |
#thru_redcloth ⇒ Object
Returns the result of running this string through RedCloth.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/gerbil/html.rb', line 56 def thru_redcloth text = dup # redcloth does not correctly convert -- into — text.gsub! %r{\b--\b}, '—' html = RedCloth.new(text).to_html # redcloth adds <span> tags around acronyms html.gsub! %r{<span class="caps">([[:upper:][:digit:]]+)</span>}, '\1' # redcloth wraps indented text within <pre><code> tags html.gsub! %r{(<pre>)\s*<code>(.*?)\s*</code>\s*(</pre>)}m, '\1\2\3' # redcloth wraps a single item within paragraph tags, which # prevents the item's HTML from being validly injected within # other block-level elements, such as headings (h1, h2, etc.) html.sub! %r{\A<p>(.*)</p>\Z}m do |match| payload = $1 if payload =~ /<p>/ match else payload end end html end |
#to_html ⇒ Object
Transforms this string into HTML.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gerbil/html.rb', line 34 def to_html text = dup text, VERBATIM_TAGS, verbatimStore = {}, true text, PROTECTED_TAGS, protectedStore = {}, false html = text.thru_redcloth html, protectedStore # collapse redundant <pre> elements -- a side effect of RedCloth html.gsub! %r{(<pre>)\s*<pre>(.*?)</pre>\s*(</pre>)}m, '\1\2\3' html = html.thru_coderay html, verbatimStore # ensure tables have a border (this GREATLY improves # readability in text-mode web browsers like Lynx and w3m) html.gsub! %r/<table/, '\& border="1"' html end |