Module: Markdownizer
- Defined in:
- lib/markdownizer.rb,
lib/markdownizer/version.rb,
lib/generators/markdownizer/install_generator.rb
Defined Under Namespace
Modules: DSL, Generators
Constant Summary collapse
- VERSION =
"0.3.7"
Class Method Summary collapse
-
.coderay(text, options = {}) ⇒ Object
Markdownizer.coderaymethod parses a code block delimited from ‘code ruby %` until `endcode %` and replaces it with appropriate classes for code highlighting. -
.markdown(text, hierarchy = 0) ⇒ Object
Markdownizer.markdownmethod converts plain Markdown text to formatted html.
Class Method Details
.coderay(text, options = {}) ⇒ Object
Markdownizer.coderay method parses a code block delimited from ‘code ruby %` until `endcode %` and replaces it with appropriate classes for code highlighting. It can take many languages aside from Ruby.
With a hash of options you can specify :line_numbers (:table or :inline), and the class of the enclosing div with :enclosing_class.
It also parses a couple of special idioms:
* {% caption 'my caption' %} introduces an h5 before the code and passes
the caption to the enclosing div as well.
* {% highlight [1,2,3] %} highlights lines 1, 2 and 3. It accepts any
Enumerable, so you can also give a Range (1..3).
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/markdownizer.rb', line 111 def coderay(text, = {}) text.gsub(%r[\{% code (\w+?) %\}(.+?)\{% endcode %\}]m) do .delete(:highlight_lines) .delete(:caption) enclosing_class = [:enclosing_class] || 'markdownizer_code' code, language = $2.strip, $1.strip code, , caption = extract_caption_from(code, ) code, = extract_highlights_from(code, ) html_caption = caption ? '<h5>' << caption << '</h5>' : nil "<div class=\"#{enclosing_class}#{caption ? "\" caption=\"#{caption}" : ''}\">" << (html_caption || '') << CodeRay.scan(code, language).div({:css => :class}.merge()) << "</div>" end end |
.markdown(text, hierarchy = 0) ⇒ Object
Markdownizer.markdown method converts plain Markdown text to formatted html. To parse the markdown in a coherent hierarchical context, you must provide it with the current hierarchical level of the text to be parsed.
88 89 90 91 92 93 94 |
# File 'lib/markdownizer.rb', line 88 def markdown(text, hierarchy = 0) text.gsub! %r[^(\s*)(#+)(.+)$] do $1 << ('#' * hierarchy) << $2 << $3 end text.gsub!("\\#",'#') RDiscount.new(text).to_html end |