Module: Ink::Formatter

Extended by:
Helper
Defined in:
lib/ink/formatter.rb

Class Method Summary collapse

Methods included from Helper

format, highlight

Class Method Details

.format(content, file, options = {}) {|type, html| ... } ⇒ Object

Format the specified content based on the filename. It can detect Markdown (*.{markdown,mkdn}), Textile (*.textile), RDoc (*.rdoc). Any other file will be highlighted by using Pygments and language detection based on filename.

Ink::Formatter.format(@code, "Rakefile")
Ink::Formatter.format(@code, "Rakefile", :line_numbers => true)
Ink::Formatter.format(@code, "Rakefile") {|type, html| }

If you provide a block, the format helper will yield the type (:textile, :markdown, :rdoc or :code) and the generated markup.

Yields:

  • (type, html)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ink/formatter.rb', line 19

def self.format(content, file, options = {}, &block)
  basename = File.basename(file)

  case basename
  when /\.rdoc$/i
    html = RDoc::Markup::ToHtml.new.convert(content)
    type = :rdoc
  when /\.(markdown|mkdn)$/i
    html = RDiscount.new(content).to_html
    type = :markdown
  when /\.textile$/i
    html = RedCloth.new(content).to_html
    type = :textile
  else
    html = highlight(content, options.merge(:file => file))
    type = :code
  end

  yield(type, html) if block_given?

  html
end