Class: Md::Renderer

Inherits:
Redcarpet::Render::HTML
  • Object
show all
Defined in:
lib/md/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Renderer

Returns a new instance of Renderer.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/md/renderer.rb', line 12

def initialize opts = {}
  super
  @opts = opts
  @opts[:pygments] ||= {}
  @opts[:graphviz] ||= {
    :format => :svg
  }
  @opts[:graphviz][:format] ||= :svg
  @opts[:wrapping_html] = true if not @opts.key? :wrapping_html
  @opts[:css] ||= []
  @opts[:script] ||= []
  @opts[:title_format] ||= "%s"

  @opts[:css].push Pygments.css @opts[:pygments]
end

Instance Attribute Details

#optsObject

Returns the value of attribute opts.



10
11
12
# File 'lib/md/renderer.rb', line 10

def opts
  @opts
end

Instance Method Details

#block_code(code, lang) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/md/renderer.rb', line 28

def block_code code, lang
  if lang.casecmp('graphviz') == 0
    block_code_graphviz code
  elsif lang.casecmp('graph') == 0
    block_code_graph code
  elsif lang.casecmp('digraph') == 0
    block_code_digraph code
  else
    block_code_default code, lang
  end
end

#block_code_default(code, lang) ⇒ Object



40
41
42
43
44
# File 'lib/md/renderer.rb', line 40

def block_code_default code, lang
  @opts[:pygments][:full] = false
  opts = {:lexer => lang, :options => @opts[:pygments]}
  return Pygments.highlight code, opts
end

#block_code_digraph(code) ⇒ Object



46
47
48
# File 'lib/md/renderer.rb', line 46

def block_code_digraph code
  block_code_digraph "digraph g { #{code} }"
end

#block_code_graph(code) ⇒ Object



50
51
52
# File 'lib/md/renderer.rb', line 50

def block_code_graph code
  block_code_graphviz "graph g { #{code} }"
end

#block_code_graphviz(code) ⇒ Object



54
55
56
57
# File 'lib/md/renderer.rb', line 54

def block_code_graphviz code
  opts = {:svg => String}
  block_code_svg GraphViz.parse_string(code).output(opts)
end

#block_code_svg(code) ⇒ Object



59
60
61
# File 'lib/md/renderer.rb', line 59

def block_code_svg code
  remove_doctype_declaration remove_xml_declaration code
end

#head_element_content(html) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/md/renderer.rb', line 84

def head_element_content html
  content = ""

  content << %{<meta charset="utf-8" />\n}

  title = scrape_title html
  content << "<title>#{@opts[:title_format]}</title>\n" % title if title

  @opts[:css].each do |style|
    content << "<style>#{minify_css style}</style>\n"
  end if @opts[:css]

  @opts[:script].each do |src|
    content << "<script>\n#{src}\n</script>\n"
  end if @opts[:script]

  return content
end

#minify_css(text) ⇒ Object

remove space and new line code



110
111
112
# File 'lib/md/renderer.rb', line 110

def minify_css text
  text.gsub(%r{/\*.+?\*/}, '').gsub(/\s+/,'')
end

#postprocess(html) ⇒ Object



71
72
73
74
# File 'lib/md/renderer.rb', line 71

def postprocess html
  html = wrapp_with_html html if @opts[:wrapping_html]
  return html
end

#remove_doctype_declaration(code) ⇒ Object



63
64
65
# File 'lib/md/renderer.rb', line 63

def remove_doctype_declaration code
  code.sub(/<!DOCTYPE .+?>/im,'')
end

#remove_xml_declaration(code) ⇒ Object



67
68
69
# File 'lib/md/renderer.rb', line 67

def remove_xml_declaration code
  code.sub(/<\?xml .+?\?>/im,'')
end

#scrape_title(html) ⇒ Object

get first h1, h2 or h3 element content



104
105
106
107
# File 'lib/md/renderer.rb', line 104

def scrape_title html
  m = html.match(/<h[1-3][^>]*>(.+?)<\/h[1-3]>/)
  return (m and m[1])
end

#wrapp_with_html(html) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/md/renderer.rb', line 76

def wrapp_with_html html
  return "<!DOCTYPE html>\n" +
    "<html><head>\n" +
    head_element_content(html) +
    "</head>\n"+
    "<body>#{html}</body></html>\n"
end