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.4"

Class Method Summary collapse

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
131
132
133
134
135
# File 'lib/markdownizer.rb', line 111

def coderay(text, options = {})
  text.gsub(%r[\{% code (\w+?) %\}(.+?)\{% endcode %\}]m) do
    options.delete(:highlight_lines)
    options.delete(:caption)

    enclosing_class = options[:enclosing_class] || 'markdownizer_code'

    code, language = $2.strip, $1.strip

    # Mark comments to avoid conflicts with Header parsing
    code.gsub!(/(#+)/) do
      '\\' + $1
    end

    code, options, caption = extract_caption_from(code, options)
    code, options = extract_highlights_from(code, options)

    html_caption = caption ? '<h5>' << caption << '</h5>' : nil

    "<div class=\"#{enclosing_class}#{caption ? "\" caption=\"#{caption}" : ''}\">" << 
      (html_caption || '') <<
        CodeRay.scan(code, language).div({:css => :class}.merge(options)) <<
          "</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*)(#+)([\w\s]+)$] do
    $1 << ('#' * hierarchy) << $2 << $3
  end
  text.gsub!('\#','#')
  RDiscount.new(text).to_html
end