Class: Kramdown::Parser::BlockKramdown
- Inherits:
-
Kramdown
- Object
- Kramdown
- Kramdown::Parser::BlockKramdown
- Defined in:
- lib/documentation_editor/parser.rb
Constant Summary collapse
- BLOCK_TAGS_START =
/\[block:(.+?)\](.+?)\[\/block\]/m
Instance Method Summary collapse
-
#initialize(source, options) ⇒ BlockKramdown
constructor
A new instance of BlockKramdown.
- #parse_block_tags ⇒ Object
Constructor Details
#initialize(source, options) ⇒ BlockKramdown
Returns a new instance of BlockKramdown.
7 8 9 10 11 |
# File 'lib/documentation_editor/parser.rb', line 7 def initialize(source, ) @language = [:language] || ['language'] super @span_parsers.unshift(:block_tags) end |
Instance Method Details
#parse_block_tags ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/documentation_editor/parser.rb', line 13 def block = @src[1] content = JSON.parse(@src[2]) @src.pos += @src.matched_size case block when 'code' if @language code = content['codes'].detect { |code| code['language'] == @language || code['language'].end_with?("|#{@language}") } code ||= content['codes'].detect { |code| code['language'].end_with?("|*") } @tree.children << Element.new(:html_element, 'pre') @tree.children.last.children << Element.new(:raw, code ? highlight(code['language'].split('|').first, code['code']) : "FIXME:#{@language}") else ul = Element.new(:html_element, 'ul', { class: 'nav nav-tabs' }) tab_content = Element.new(:html_element, 'div', { class: 'tab-content' }) content['codes'].each_with_index do |v, i| language, label = v['language'].split('|') label ||= language 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: "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 when 'callout' callout = new_block_el(:html_element, 'div', { class: "alert alert-#{content['type']}" }) callout.children << Element.new(:raw, parse_cached(content['body'])) @tree.children << callout when 'image' clazz = case content['float'] when 'left' 'pull-left' when 'right' 'pull-right' else nil end @tree.children << Element.new(:html_element, 'figure', { class: clazz }) @tree.children.last.children << Element.new(:img, nil, { src: content['images'][0]['image'][0] }) unless content['caption'].blank? @tree.children.last.children << Element.new(:html_element, 'figcaption') @tree.children.last.children.last.children << Element.new(:raw, parse_cached(content['caption'])) end when 'if' @tree.children << Element.new(:comment, "if #{content['condition']}", { }, { start: true, condition: content['condition'], negation: false }) when 'ifnot' @tree.children << Element.new(:comment, "if NOT #{content['condition']}", { }, { start: true, condition: content['condition'], negation: true }) when 'endif' @tree.children << Element.new(:comment, '/if', { }, { start: false, condition: content['condition'] }) when 'parameters' table = Element.new(:html_element, 'table', { class: 'table' }) thead = Element.new(:html_element, 'thead') thead.children << Element.new(:html_element, 'tr') 1.upto(content['cols']) do |col| thead.children.last.children << Element.new(:html_element, 'th') thead.children.last.children.last.children << Element.new(:raw, parse_cached(content['data']["h-#{col - 1}"])) end table.children << thead tbody = Element.new(:html_element, 'tbody') 1.upto(content['rows']) do |row| tbody.children << Element.new(:html_element, 'tr') 1.upto(content['cols']) do |col| md = content['data']["#{row - 1}-#{col - 1}"] id = generate_id(md) anchor = col == 1 ? "\n<a href=\"##{id}\" class=\"anchor\"><i class=\"fa fa-link\"></i></a>" : '' html = parse_cached("#{md}#{anchor}") tbody.children.last.children << Element.new(:html_element, 'td', col == 1 ? { id: id } : nil) tbody.children.last.children.last.children << Element.new(:raw, html) end end table.children << tbody @tree.children << table else raise "Block not supported: #{block}" end end |