Class: Kramdown::Parser::BlockKramdown

Inherits:
Kramdown
  • Object
show all
Defined in:
lib/documentation_editor/parser.rb

Constant Summary collapse

BLOCK_TAGS_START =
/\[block:(.+?)\](.+?)\[\/block\]/m

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ BlockKramdown

Returns a new instance of BlockKramdown.



8
9
10
11
12
# File 'lib/documentation_editor/parser.rb', line 8

def initialize(source, options)
  @language = options[:language]
  super
  @span_parsers.unshift(:block_tags)
end

Instance Method Details

#parse_block_tagsObject



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
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/documentation_editor/parser.rb', line 14

def parse_block_tags
  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}") }
      matchall_codes = content['codes'].select { |code| code['language'].end_with?("|*") }
      if code || matchall_codes.size == 1
        code ||= matchall_codes.first
        @tree.children << Element.new(:html_element, 'pre', { class: 'highlight' })
        @tree.children.last.children << Element.new(:raw, code ? highlight(code['language'].split('|').first, code['code']) : "FIXME")
      else
        append_code_tabs matchall_codes
      end
    elsif content['codes'].size == 1
      code = content['codes'].first
      @tree.children << Element.new(:html_element, 'pre', { class: 'highlight' })
      @tree.children.last.children << Element.new(:raw, highlight(code['language'].split('|').first, code['code']))
    else
      append_code_tabs(content['codes'])
    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 'html'
    coder = HTMLEntities.new
    @tree.children << Element.new(:raw, coder.decode(content['body']))
  when 'image'
    clazz = case content['float']
    when 'left'
      'pull-left'
    when 'right'
      'pull-right'
    else
      nil
    end
    attrs = { src: content['images'][0]['image'][0] }
    if !content['width'].blank?
      attrs[:width] = content['width'].to_i
    end
    @tree.children << Element.new(:html_element, 'figure', { class: clazz })
    @tree.children.last.children << Element.new(:img, nil, attrs)
    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 'buttons'
    buttons = new_block_el(:html_element, 'div', { class: "text-center" })
    content['buttons'].each do |b|
      buttons.children << Element.new(:html_element, 'a', { href: b['link'], class: 'btn btn-default' })
      buttons.children.last.children << Element.new(:raw, b['label'])
    end
    @tree.children << buttons
  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'
    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("param #{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