Class: NoraMark::Html::Generator

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/nora_mark/html/generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#escape_html

Constructor Details

#initialize(param = {}) ⇒ Generator

Returns a new instance of Generator.



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
# File 'lib/nora_mark/html/generator.rb', line 22

def initialize(param = {})
  @context = Context.new(param)
  frontmatter_writer = FrontmatterWriter.new self
  paragraph_writer = ParagraphWriter.new self
  abstract_node_writer = AbstractNodeWriter.new self
  raw_writer = RawTextBlockWriter.new self
  page_writer = TagWriter.create('body', self,
                                 node_preprocessor: proc do |node|
                                   @context.end_html
                                   if node.first_child.class == Frontmatter
                                     frontmatter = node.first_child
                                     frontmatter_writer.write frontmatter
                                     frontmatter.remove
                                     @context.save_default_metas
                                   end
                                   node
                                 end);

  @writers = {
    Text => raw_writer,
    Paragraph => paragraph_writer,
    ParagraphGroup => paragraph_writer,
    Inline => TagWriter.create(nil, self, trailer: ''),
    Block => TagWriter.create(nil, self),
    Document => abstract_node_writer,
    Page => page_writer,
    Frontmatter => frontmatter_writer,
  }
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



17
18
19
# File 'lib/nora_mark/html/generator.rb', line 17

def context
  @context
end

Class Method Details

.nameObject



18
19
20
# File 'lib/nora_mark/html/generator.rb', line 18

def self.name
  :html
end

Instance Method Details

#assign_id_to_headingsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nora_mark/html/generator.rb', line 69

def assign_id_to_headings
  collect_id_and_headings
  count = 1
  @headings.each do |heading|
    if heading.ids.size == 0
      begin
        id = "heading_index_#{count}"
        count = count + 1
      end while @id_pool[id]
      heading.ids << id
    end
  end
end

#collect_id_and_headingsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nora_mark/html/generator.rb', line 52

def collect_id_and_headings
  @id_pool = {}
  @headings = []

  all_nodes = @parsed_result.all_nodes
  all_nodes.each do |x|
    x.ids ||= []
    x.ids.each do |id|
      if !@id_pool[id].nil?
        warn "duplicate id #{id}"
      end
      @id_pool[id] = x
    end
    @headings << x if (x.kind_of?(Block) && x.name =~ /h[1-6]/)
  end
end

#convert(parsed_result, render_parameter = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/nora_mark/html/generator.rb', line 83

def convert(parsed_result, render_parameter = {})
  transformer = Html.default_transformer
  transformer.options[:render_parameter] = render_parameter
  @parsed_result = transformer.transform parsed_result
  assign_id_to_headings

  children = parsed_result.children
  @context.file_basename = parsed_result.document_name
  @context.render_parameter = render_parameter
  children.each { |node|
    to_html(node)
  }
  @context.set_toc generate_toc
  @context.result
end

#generate_tocObject



99
100
101
102
103
# File 'lib/nora_mark/html/generator.rb', line 99

def generate_toc
  @headings.map do |heading|
    { page: heading.ancestors(type: :Page)[0].page_no }.merge heading.heading_info
  end
end

#to_html(node) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/nora_mark/html/generator.rb', line 105

def to_html(node)
  if node.kind_of? Text
    if node.noescape
      @context << node.content
    else
      @context << escape_html(node.content)
    end
  else
    writer = @writers[node.class]
    if writer.nil?
      warn "can't find html generator for \"#{node.class}\""
    else
      writer.write(node)
    end
  end
end