Class: BookLab::SML::Renderer

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/booklab/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
# File 'lib/booklab/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)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#in_blockObject

For table, list for temp mark in block



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

def in_block
  @in_block
end

#smlObject

Returns the value of attribute sml.



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

def sml
  @sml
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#children_to_html(node) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/booklab/sml/renderer.rb', line 37

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

  children = self.class.get_children(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

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

#children_to_text(node) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/booklab/sml/renderer.rb', line 59

def children_to_text(node)
  return node if node.is_a?(String)
  children = self.class.get_children(node)
  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



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

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

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

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



53
54
55
56
57
# File 'lib/booklab/sml/renderer.rb', line 53

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

#to_htmlObject



22
23
24
# File 'lib/booklab/sml/renderer.rb', line 22

def to_html
  node_to_html(self.value)
end

#to_sObject



26
27
28
# File 'lib/booklab/sml/renderer.rb', line 26

def to_s
  to_html
end

#to_textObject



49
50
51
# File 'lib/booklab/sml/renderer.rb', line 49

def to_text
  node_to_text(self.value)
end