Class: BlueDoc::SML::Renderer

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/bluedoc/sml/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sml, options) ⇒ Renderer

Returns a new instance of Renderer.



14
15
16
17
18
19
20
21
# File 'lib/bluedoc/sml/renderer.rb', line 14

def initialize(sml, options)
  @sml = sml
  @config = Config.new
  @config.plantuml_service_host = options[:plantuml_service_host]
  @config.mathjax_service_host = options[:mathjax_service_host]
  @value = YAML.load(sml)
  @list = {}
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/bluedoc/sml/renderer.rb', line 9

def config
  @config
end

#in_blockObject

For table, list for temp mark in block



12
13
14
# File 'lib/bluedoc/sml/renderer.rb', line 12

def in_block
  @in_block
end

#listObject

Returns the value of attribute list.



9
10
11
# File 'lib/bluedoc/sml/renderer.rb', line 9

def list
  @list
end

#smlObject

Returns the value of attribute sml.



9
10
11
# File 'lib/bluedoc/sml/renderer.rb', line 9

def sml
  @sml
end

#valueObject

Returns the value of attribute value.



9
10
11
# File 'lib/bluedoc/sml/renderer.rb', line 9

def value
  @value
end

Instance Method Details

#children_to_html(node) ⇒ Object



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
# File 'lib/bluedoc/sml/renderer.rb', line 38

def children_to_html(node)
  return node if node.is_a?(String)

  children = self.class.get_children(node)

  parent_is_list = self.class.tag_name(node) == "list"
  parent_attrs = self.class.attributes(node)

  children.each_with_index.map do |child, idx|
    prev_node = idx > 0 ? children[idx - 1] : nil
    next_node = idx < children.length ? children[idx + 1] : nil

    list = self.list
    is_list = self.class.tag_name(child) == "list"

    child_attrs = self.class.attributes(child)

    if is_list
      nid = child_attrs[:nid]

      # BUG: ignore invlid nested list
      if parent_is_list
        return ""
      end

      if list[nid]
        list[nid] << child
      else
        list[nid] = [child]
      end
    end

    pre_node_attrs = self.class.attributes(prev_node)
    if (!is_list && self.class.tag_name(prev_node) == "list") ||
      (is_list && self.class.tag_name(prev_node) == "list" && child_attrs[:nid] != pre_node_attrs[:nid])
      nid = pre_node_attrs[:nid]
      list.delete(nid)
    end


    node_to_html(child, prev: prev_node, next: next_node)
  end.join("")
end

#children_to_text(node) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/bluedoc/sml/renderer.rb', line 92

def children_to_text(node)
  return node if node.is_a?(String)
  children = self.class.get_children(node)
  text = children.each_with_index.map do |child, idx|
    text = node_to_text(child, {})
    text.blank? ? nil : text
  end.compact.join(" ")
end

#node_to_html(node, opts = {}) ⇒ Object



31
32
33
34
35
36
# File 'lib/bluedoc/sml/renderer.rb', line 31

def node_to_html(node, opts = {})
  opts[:renderer] = self

  rule = BlueDoc::SML::Rules::find_by_node(node)
  rule.to_html(node, opts)
end

#node_to_text(node, opts = {}) ⇒ Object



86
87
88
89
90
# File 'lib/bluedoc/sml/renderer.rb', line 86

def node_to_text(node, opts = {})
  opts[:renderer] = self
  rule = BlueDoc::SML::Rules::find_by_node(node)
  ERB::Util.html_escape(rule.to_text(node, opts)&.strip)
end

#to_htmlObject



23
24
25
# File 'lib/bluedoc/sml/renderer.rb', line 23

def to_html
  node_to_html(self.value)
end

#to_sObject



27
28
29
# File 'lib/bluedoc/sml/renderer.rb', line 27

def to_s
  to_html
end

#to_textObject



82
83
84
# File 'lib/bluedoc/sml/renderer.rb', line 82

def to_text
  node_to_text(self.value)
end