Class: Mdown2PDF::Render

Inherits:
Redcarpet::Render::HTML
  • Object
show all
Defined in:
lib/mdown2pdf/render.rb

Overview

Custom Redcarpet render object that adds the following to the default stack:

* Code highlighting (through Rouge).
* Automatic expanding for image paths (since Wkhtmltopdf requires
  absolute paths).
* Anchor generation for titles.
* Possibility to use "`#hexcode`" to generate a div with the specified
  value as the background color.

It is a drop-in replacement for any Redcarpet render object that procuces HTML.

Instance Method Summary collapse

Instance Method Details

#block_code(code, lang) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/mdown2pdf/render.rb', line 17

def block_code(code, lang)
  lexer = Rouge::Lexer.find_fancy(lang, code) || Rouge::Lexers::PlainText

  if lexer.tag == 'make'
    code.gsub! /^  /, "\t"
  end

  %(<div class="highlight"><pre>#{Rouge::Formatters::HTML.new.format(lexer.lex(code))}</pre></div>)
end

#codespan(code) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/mdown2pdf/render.rb', line 27

def codespan(code)
  if code.start_with?("#")
    %(<div style="height: 20px; margin: auto; width: 40px; background: #{code}"></div>)
  else
    %(<code>#{code}</code>)
  end
end

#header(text, level) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/mdown2pdf/render.rb', line 35

def header(text, level)
  alphabet = ['a'..'z'] + []

  anchor = text.chars.keep_if do |c|
    c =~ /[[:alpha:]]/ || c == ''
  end.join("")

  %(<h#{level} id="#{anchor.downcase.gsub(' ', '-')}">#{text}</h#{level}>)
end

#image(link, title, text) ⇒ Object



45
46
47
# File 'lib/mdown2pdf/render.rb', line 45

def image(link, title, text)
  %(<img src="#{Dir.pwd}/#{link}">)
end