Class: Lm2doc::Markdown

Inherits:
Converter show all
Defined in:
lib/lm2doc/markdown.rb

Instance Attribute Summary

Attributes inherited from Converter

#content

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Converter

#as_html

Class Method Details

.perfer_extsObject



10
11
12
# File 'lib/lm2doc/markdown.rb', line 10

def self.perfer_exts
  %w[ .md ]
end

Instance Method Details

#code_block(doc) ⇒ Object



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

def code_block(doc)
  doc.css("pre > code[class^=language]").each do |code_node|
    lang = (code_node["class"].scan(/language-([^\s]+)/).first || []).first
    highlight_code_block(code_node, lang)
  end
  
  doc.css("pre[lang] > code").each do |code_node|
    lang = code_node.parent["lang"]
    highlight_code_block(code_node, lang)
  end
end

#convertObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lm2doc/markdown.rb', line 14

def convert
  html = Kramdown::Document.new(self.content, 
    enable_coderay: false,
    toc_levels: [ 2, 3 ]
  ).to_html

  doc = Nokogiri::HTML::DocumentFragment.parse(html)
  
  figure_role(doc)
  code_block(doc)
  doc.to_html
end

#down_heading(doc) ⇒ Object



90
91
92
93
94
# File 'lib/lm2doc/markdown.rb', line 90

def down_heading(doc)
  doc.css("h3").each {|n| n.name = "h4" }
  doc.css("h2").each {|n| n.name = "h3" }
  doc.css("h1").each {|n| n.name = "h2" }
end

#figure_role(doc) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/lm2doc/markdown.rb', line 80

def figure_role(doc)
  doc.css("p[role=figure]").each do |p|
    p.remove_attribute "role"
    p.name = "figure"
    p["class"] = "thumbnail"
    p.css("em").each {|s| s.name = "figcaption" }
    p.css("strong").each {|s| s.name = "figcaption" }
  end
end

#highlight(content, lang) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/lm2doc/markdown.rb', line 47

def highlight(content, lang)
  # require "pry-nav"
  # binding.pry
  require 'pygments'
  highlighted_code = Pygments.highlight(content, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'})
  str = highlighted_code.match(/<pre>(.+)<\/pre>/m)[1].to_s.gsub(/ *$/, '')
  tableize_code(str, lang)
end

#highlight_code_block(code_node, lang) ⇒ Object



40
41
42
43
44
45
# File 'lib/lm2doc/markdown.rb', line 40

def highlight_code_block(code_node, lang)
  pre = code_node.parent
  code_output = highlight(code_node.content, lang)
  code_doc = Nokogiri::HTML::DocumentFragment.parse(code_output)
  pre.replace code_doc.children
end

#tableize_code(str, lang = '') ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lm2doc/markdown.rb', line 56

def tableize_code (str, lang = '')
  line_number, code = '', ''
  # code = ''
  str.lines.each_with_index do |line, index|
    line_number += "<span class='line-number'>#{index + 1}</span>\n"
    code  += "<span class='line'>#{line}</span>"
  end
  
  table = <<HTML
<div class="highlight">
  <table>
<tr>
  <td class="lines">
    <pre class="line-numbers">#{line_number}</pre>
  </td>
  <td class="code">
    <pre><code>#{code}</code></pre>
  </td>
</tr>
  </table>
</div>
HTML
end