class Kramdown::Parser::BlockKramdown
Constants
- BLOCK_TAGS_START
Public Class Methods
new(source, options)
click to toggle source
Calls superclass method
# File lib/documentation_editor/parser.rb, line 8 def initialize(source, options) @language = options[:language] super @span_parsers.unshift(:block_tags) end
Public Instance Methods
Private Instance Methods
append_code_tabs(codes)
click to toggle source
# File lib/documentation_editor/parser.rb, line 147 def append_code_tabs(codes) ul = Element.new(:html_element, 'ul', { class: 'nav nav-tabs' }) tab_content = Element.new(:html_element, 'div', { class: 'tab-content' }) codes.each_with_index do |v, i| language, label = v['language'].split('|') label = language if label.nil? || label == '*' id = "snippet_#{@src.pos}_#{generate_id(v['language'])}" ul.children << Element.new(:html_element, 'li', { class: ('active' if i == 0) }) ul.children.last.children << Element.new(:html_element, 'a', { href: "##{id}", 'data-toggle' => 'tab' }) ul.children.last.children.last.children << Element.new(:raw, label) tab_content.children << Element.new(:html_element, 'pre', { class: "highlight tab-pane#{' in active' if i == 0}", id: id }) tab_content.children.last.children << Element.new(:raw, highlight(language, v['code'])) end @tree.children << ul @tree.children << tab_content end
cache(key) { || ... }
click to toggle source
# File lib/documentation_editor/parser.rb, line 116 def cache(key, &block) if !Rails.cache.exist?(key) html = yield Rails.cache.write(key, html) end Rails.cache.read(key) end
generate_id(str)
click to toggle source
# File lib/documentation_editor/parser.rb, line 108 def generate_id(str) id = (str || '').gsub(/<\/?[^>]+>/, '').strip.gsub(/[^a-zA-Z0-9]+/, '-') @ids ||= {} @ids[id] ||= 0 @ids[id] += 1 @ids[id] == 1 ? id : "#{id}-#{@ids[id]}" end
highlight(language, code)
click to toggle source
# File lib/documentation_editor/parser.rb, line 124 def highlight(language, code) language = language.to_s.downcase language = case language when 'php', 'swift', 'node', 'node.js' 'js' when 'android' 'java' when 'go' 'c++' else language end cache "#{language}_#{code.hash}" do Simplabs::Highlight.highlight(language, CGI.unescapeHTML(code)) end end
parse_cached(text)
click to toggle source
# File lib/documentation_editor/parser.rb, line 141 def parse_cached(text) cache "#{text.hash}" do Kramdown::Document.new(text, input: 'BlockKramdown').to_html end end