class PrettyDoc::Markdown
Public Class Methods
perfer_exts()
click to toggle source
# File lib/pretty_doc/converters/markdown.rb, line 9 def self.perfer_exts ['.md', '.markdown', '.mdown', '.txt'] end
Public Instance Methods
before_convert()
click to toggle source
# File lib/pretty_doc/converters/markdown.rb, line 13 def before_convert self.content = content .gsub(/\[NO_TOC\]\n/, "{:.no_toc}\n") .gsub(/\[TOC\]\n/, "+ __toc_line__\n{:toc}\n") end
Private Instance Methods
code_block(doc)
click to toggle source
# File lib/pretty_doc/converters/markdown.rb, line 35 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
convert()
click to toggle source
# File lib/pretty_doc/converters/markdown.rb, line 21 def convert before_convert html = Kramdown::Document.new( content, input: :GFM, enable_coderay: false, transliterated_header_ids: true ).to_html doc = Nokogiri::HTML::DocumentFragment.parse(html) figure_role(doc) code_block(doc) doc.to_html end
figure_role(doc)
click to toggle source
# File lib/pretty_doc/converters/markdown.rb, line 98 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)
click to toggle source
# File lib/pretty_doc/converters/markdown.rb, line 54 def highlight(content, lang) 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)
click to toggle source
# File lib/pretty_doc/converters/markdown.rb, line 47 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 = '')
click to toggle source
# File lib/pretty_doc/converters/markdown.rb, line 65 def tableize_code (str, lang = '') line_numbers = '' code = '' str.lines.each_with_index do |line, index| line_numbers += "<span class='line-number'>#{index + 1}</span>\n" if options.enable_line_numbers code += "<span class='line'>#{line}</span>" end if options.enable_line_numbers line_numbers = <<-HTML <td class="lines"> <pre class="line-numbers">#{line_numbers}</pre> </td> HTML end no_line_numbers_class = options.enable_line_numbers ? '' : 'no-lines' table = <<-HTML <div class="highlight #{no_line_numbers_class}"> <table> <tr> #{line_numbers} <td class="code"> <pre><code>#{code}</code></pre> </td> </tr> </table> </div> HTML table end