Class: Koara::KoaraRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/koara/koararenderer.rb

Instance Method Summary collapse

Instance Method Details

#escape(text) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/koara/koararenderer.rb', line 192

def escape(text)
  return text.gsub(/\[/, "\\\\[")
             .gsub(/\]/, "\\\\]")
             .gsub(/\*/, "\\\\*")
             .gsub(/_/, "\\\\_")
             .sub(/`/, "\\\\`")
             .sub(/=/, "\\\\=")
             .sub(/>/, "\\\\>")
             .sub(/-/, "\\\\-")
             .sub(/(\d+)\./) { |m| "\\#{$1}." }
end

#escape_url(text) ⇒ Object



187
188
189
190
# File 'lib/koara/koararenderer.rb', line 187

def escape_url(text)
  return text.gsub(/\(/, "\\\\(")
             .gsub(/\)/, "\\\\)")
end

#indentObject



204
205
206
207
208
# File 'lib/koara/koararenderer.rb', line 204

def indent
  @left.each { |s|
    @out << s
  }
end

#outputObject



210
211
212
# File 'lib/koara/koararenderer.rb', line 210

def output
  @out.string.strip
end

#visit_blockelement(node) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/koara/koararenderer.rb', line 117

def visit_blockelement(node)
  if !node.is_first_child
    indent
  end
  node.children_accept(self)
  @out << "\n"
  if !node.nested || (node.parent.instance_of? ListItem && (node.next.instance_of? Paragraph) && !node.is_last_child())
    @out << "\n"
  elsif node.parent.instance_of?(BlockQuote) && node.next.instance_of?(Paragraph)
    indent
    @out << "\n"
  end
end

#visit_blockquote(node) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/koara/koararenderer.rb', line 35

def visit_blockquote(node)
  if !node.is_first_child
    indent
  end
  if node.has_children
    @out << '> '
    @left.push('> ')
    node.children_accept(self)
    @left.pop
  else
    @out << ">\n"
  end
  if !node.nested
    @out << "\n"
  end
end

#visit_code(node) ⇒ Object



173
174
175
176
177
# File 'lib/koara/koararenderer.rb', line 173

def visit_code(node)
  @out << '`'
  node.children_accept(self)
  @out << '`'
end

#visit_codeblock(node) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/koara/koararenderer.rb', line 83

def visit_codeblock(node)
  str = StringIO.new
  @left.each { |s| str << s }
  @out << '```'
  if node.language
    @out << node.language
  end
  @out << "\n"
  @out << node.value.gsub(/^/, str.string)
  @out << "\n"
  indent
  @out << '```'
  @out << "\n"
  if !node.is_last_child
    indent
    @out << "\n"
  end
end

#visit_document(node) ⇒ Object



10
11
12
13
14
15
# File 'lib/koara/koararenderer.rb', line 10

def visit_document(node)
  @left = Array.new
  @out = StringIO.new
  @hard_wrap = false
  node.children_accept(self)
end

#visit_em(node) ⇒ Object



167
168
169
170
171
# File 'lib/koara/koararenderer.rb', line 167

def visit_em(node)
  @out << '_'
  node.children_accept(self)
  @out << '_'
end

#visit_heading(node) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/koara/koararenderer.rb', line 17

def visit_heading(node)
  if !node.is_first_child
    indent
  end
  node.value.times {
    @out << '='
  }
  if node.has_children
    @out << ' '
    node.children_accept(self)
  end
  @out << "\n"
  if !node.is_last_child
    indent
    @out << "\n"
  end
end

#visit_image(node) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/koara/koararenderer.rb', line 131

def visit_image(node)
  @out << '[image: '
  node.children_accept(self)
  @out << ']'
  if node.value && node.value.strip.length > 0
    @out << '('
    @out << escape_url(node.value)
    @out << ')'
  end
end

#visit_linebreak(node) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/koara/koararenderer.rb', line 179

def visit_linebreak(node)
  if @hard_wrap || node.explicit
    @out << '  '
  end
  @out << "\n"
  indent
end


142
143
144
145
146
147
148
149
150
151
# File 'lib/koara/koararenderer.rb', line 142

def visit_link(node)
  @out << '['
  node.children_accept(self)
  @out << ']'
  if node.value && node.value.strip.length > 0
    @out << '('
    @out << escape_url(node.value)
    @out << ')'
  end
end

#visit_list_block(node) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/koara/koararenderer.rb', line 52

def visit_list_block(node)
  node.children_accept(self)
  if !node.is_last_child
    indent
    @out << "\n"
    next_node = node.next
    if next_node.instance_of?(Ast::ListBlock) && next_node.ordered == node.ordered
      @out << "\n"
    end
  end
end

#visit_list_item(node) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/koara/koararenderer.rb', line 64

def visit_list_item(node)
  if !node.parent.nested || !node.is_first_child || !node.parent.is_first_child
    indent
  end
  @left.push('  ')
  if node.number
    @out << node.number + '.'
  else
    @out << '-'
  end
  if node.has_children
    @out << ' '
    node.children_accept(self)
  else
    @out << "\n"
  end
  @left.pop
end

#visit_paragraph(node) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/koara/koararenderer.rb', line 102

def visit_paragraph(node)
  if !node.is_first_child
    indent
  end

  node.children_accept(self)
  @out << "\n"
  if !node.nested || ((node.parent.instance_of?(Ast::ListItem) && node.next.instance_of?(Ast::Paragraph)) && !node.is_last_child)
    @out << "\n"
  elsif node.parent.instance_of?(Ast::BlockQuote) && node.next.instance_of?(Ast::Paragraph)
    indent
    @out << "\n"
  end
end

#visit_strong(node) ⇒ Object



161
162
163
164
165
# File 'lib/koara/koararenderer.rb', line 161

def visit_strong(node)
  @out << '*'
  node.children_accept(self)
  @out << '*'
end

#visit_text(node) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/koara/koararenderer.rb', line 153

def visit_text(node)
  if node.parent.instance_of? Ast::Code
    @out << node.value.to_s;
  else
    @out << escape(node.value.to_s);
  end
end