Class: OrgParse::TextileVisitor

Inherits:
OrgVisitor show all
Includes:
VisitorUtils
Defined in:
lib/org-parse/textile-visitor.rb

Overview

output HTML format

Instance Method Summary collapse

Methods included from VisitorUtils

#exec_children, #exec_list

Methods inherited from OrgVisitor

#make_image_regs

Constructor Details

#initialize(root, cm_opts = {}) ⇒ TextileVisitor

Returns a new instance of TextileVisitor.



17
18
19
20
21
22
23
24
25
# File 'lib/org-parse/textile-visitor.rb', line 17

def initialize(root, cm_opts = {})
  @body = @title = @add_to_head = ''
  @root = root
  @ul_stack = []
  @list_level = 0
  @body = ''
  @options = cm_opts
  super()
end

Instance Method Details

#blockquote(node) ⇒ Object



87
88
89
90
91
# File 'lib/org-parse/textile-visitor.rb', line 87

def blockquote(node)
  body = exec_children(node)
  body.sub(/\n/, '<br/>')
  %Q|\nbq.#{body.sub(/^\s*/,'').chomp}\n\n|
end

#blocks(node) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/org-parse/textile-visitor.rb', line 93

def blocks(node)
  case node.block_name
  when 'VERSE'
    example node
  when 'EXAMPLE'
    example node
  when 'QUOTE'
    blockquote node
  when 'HTML'
    textblock node
  when 'COMMENT'
    ''
  when 'SRC'
    src node
  else
    puts "not implimented block=>[#{node.block_name}](#{node.inspect})"
    exec_children(node)
  end
end

#buildObject

build the textile



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/org-parse/textile-visitor.rb', line 28

def build
  @options = @root.options
  @before_text = exec_list @options[:text]
  title = exec_list @root.options[:title]
  body = "h1. " + title + "\n\n" + @before_text
  @footnotes = []
  @footnote_idxs = []
  start_flag = true
  @root.children.each do |node|
    if start_flag and node.kind != :SECTION
      @before_text += execute(node)
    else
      start_flag = false
      body += execute(node)
    end
  end
  body += footnotes
end

#clear_varsObject



153
154
155
# File 'lib/org-parse/textile-visitor.rb', line 153

def clear_vars
  @flash_vars.clear
end

#example(node) ⇒ Object



72
73
74
75
# File 'lib/org-parse/textile-visitor.rb', line 72

def example(node)
  body = exec_children(node)
  %Q|<pre>\n#{ body.chomp }\n</pre>\n|
end

#execute(node) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/org-parse/textile-visitor.rb', line 230

def execute(node)
  # puts "node:#{node.kind} [#{node.value}]\n"
  # STDERR.puts node.inspect
  return '' if node.done?
  case node.kind
  when :SECTION
    str = execute node.headline
    str += exec_children node # section node
  when :HEADLINE
    headline node
  when :TEXTBLOCK
    textblock node
  when :WHITELINE, :WHITELINES
    "\n" * node.value
  when :STRING
    node.value
  when :QUOTE
    if node.value == "\n"
      '<br/>'
    else
      node.value
    end
  when :TEXTLINE
    textline node
  when :EMPHASIS
    " *#{ exec_children node }* "
  when :ITALIC
    " _#{ exec_children node }_ "
  when :UNDER_LINE
    %Q| +#{exec_children node}+ |
  when :STRIKE_THROUGH
    " -#{ exec_children node}- "
  when :CODE, :VERBATIM
    " @#{ exec_children node }@ "
  when :VARIABLE
    variable node
  when :UNORDERED_LIST, :ORDERED_LIST, :DEFINITION_LIST
    lists node
  when :LIST_ITEM
    list_item node
  when :LINK
    link node
  when :BLOCK
    blocks node
  when :TABLE
    table node
  when :TABLE_ROW
    table_row node
  when :FN_LINK
    footnote_link node
  when :FN_DEFINE
    footnote_define node
  else
    puts "not implimented=>[#{node.kind}](#{node.inspect})"
    ''
  end
end

#footnote_define(node) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/org-parse/textile-visitor.rb', line 210

def footnote_define(node)
  idx = @footnote_idxs.index node.value
  unless idx
    idx = @footnote_idxs.size
    @footnote_idxs << node.value
  end
  @footnotes[idx] = exec_children(node)
  ''
end


200
201
202
203
204
205
206
207
208
# File 'lib/org-parse/textile-visitor.rb', line 200

def footnote_link(node)
  idx = @footnote_idxs.index node.value
  unless idx
    idx = @footnote_idxs.size
    @footnote_idxs << node.value
  end
  idx += 1
  "[#{idx}]"
end

#footnotesObject



220
221
222
223
224
225
226
227
228
# File 'lib/org-parse/textile-visitor.rb', line 220

def footnotes
  ret = ''
  @footnotes.each_index{|idx|
    n = idx+1
    val = @footnotes[idx]
    ret += "fn#{n}. #{val}\n"
  }
  ret
end

#headline(node) ⇒ Object



47
48
49
50
# File 'lib/org-parse/textile-visitor.rb', line 47

def headline(node)
  level = node.level+1
  %Q|\nh#{level}. #{exec_children(node).chomp}\n\n|
end

#html_escape(s) ⇒ Object Also known as: h

HTML escape



289
290
291
# File 'lib/org-parse/textile-visitor.rb', line 289

def html_escape(s)
  s.to_s.gsub(/&/, "&amp;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
end


113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/org-parse/textile-visitor.rb', line 113

def link(node)
  desc = exec_children node
  if desc.empty?
    if node.uri =~ @image_file_reg
      " !#{node.uri.sub(/^file:/, '')}! "
    else
      " #{node.uri.sub(/^file:/, '')} "
    end
  else
    %Q| "#{desc}":#{node.uri.sub(/^file:/, '')} |
  end
end

#list_item(node) ⇒ Object

list_item



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/org-parse/textile-visitor.rb', line 173

def list_item(node)
  marks = { :UL_START => '*', :OL_START => '#' }
  if node.children.empty?
    if node.type == :DL_START
      "  <dt>#{exec_list(node.dt)}</dt>\n  <dd>#{exec_list(node.value).chomp}</dd>\n"
    else
      mark = marks[node.type] * @list_level
      "#{mark} #{exec_list(node.value).chomp}\n"
    end
  else
    @list_level += 1
    @p_tag_flag = false
    str = exec_list(node.value)
    str += exec_children node
    str.sub(/\n/, '<br/>')
    if node.type == :DL_START
      str = "  <dt>#{exec_list(node.dt)}</dt>\n  <dd>\n#{str.chomp}</dd>\n"
    else
      mark = marks[node.type] * @list_level
      str = "#{mark} #{str.chomp}\n"
    end
    @p_tag_flag = true
    @list_level -= 1
    str
  end
end

#lists(node) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/org-parse/textile-visitor.rb', line 157

def lists(node)
  tags = { :UNORDERED_LIST => ['<ul>', '</ul>'],
    :ORDERED_LIST => ['<ol>', '</ol>'],
    :DEFINITION_LIST => ['<dl>', '</dl>'],
  }
  @list_level += 1
  body = exec_children node
  @list_level -= 1
  if node.kind == :DEFINITION_LIST
    "#{tags[node.kind][0]}\n#{body.chomp}\n#{tags[node.kind][1]}\n"
  else
    body
  end
end

#src(node) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/org-parse/textile-visitor.rb', line 77

def src(node)
  if @options[:redmine]
    text = exec_children(node)
    syntax = node.syntax
    %Q|<pre><code class="#{syntax}">\n#{ body.chomp }\n</code></pre>\n|
  else
    example node
  end
end

#table(node) ⇒ Object



126
127
128
129
130
# File 'lib/org-parse/textile-visitor.rb', line 126

def table(node)
  ret = ''
  ret += exec_list node.hrows
  ret += exec_children node
end

#table_row(node) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/org-parse/textile-visitor.rb', line 132

def table_row(node)
  ret = ''
  s = node.is_head? ? '|_.' : '|'
  node.children.each {|cols|
    cols.each{|col|
      ret += s + execute(col)
    }
  }
  ret += "|\n"
end

#textblock(node) ⇒ Object

paragraph if @p_tag_flag == false then we don’t output <p></p> tags.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/org-parse/textile-visitor.rb', line 54

def textblock(node)
  if node.verse? or node.example? or node.html? or node.src?
    exec_children node
  elsif @p_tag_flag
    "p. #{exec_children(node).sub(/^\s*/,'').chomp}\n"
  else
    str = exec_children node
    @p_tag_flag = true  # next paragraph has <p>
    str
  end
end

#textline(node) ⇒ Object



66
67
68
69
70
# File 'lib/org-parse/textile-visitor.rb', line 66

def textline(node)
  str = exec_children(node)
  str = h str unless node.verse? or node.example? or node.html? or node.src?
  str
end

#variable(node) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/org-parse/textile-visitor.rb', line 143

def variable(node)
  fvs = ['CAPTION', 'ATTR_HTML', 'TEXT']
  if fvs.include? node.name
    @flash_vars[node.name] = node.value
  else
    @options[node.name] = node.value
  end
  ''
end