Class: OrgParse::HtmlVisitor
- Inherits:
-
OrgVisitor
- Object
- OrgVisitor
- OrgParse::HtmlVisitor
- Includes:
- VisitorUtils
- Defined in:
- lib/org-parse/visitor.rb
Overview
output HTML format
Constant Summary collapse
- TEMPLATE =
::File.join(OrgParse::LIBPATH , 'org-parse', 'templates', 'single.html.erb')
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
Instance Method Summary collapse
- #blockquote(node) ⇒ Object
- #blocks(node) ⇒ Object
-
#build ⇒ Object
build the HTML string.
- #clear_vars ⇒ Object
- #close_ul_sec(level) ⇒ Object
- #example(node) ⇒ Object
- #execute(node) ⇒ Object
- #footnote_define(node) ⇒ Object
- #footnote_link(node) ⇒ Object
- #footnotes ⇒ Object
- #get_indent ⇒ Object
- #headline(node) ⇒ Object
-
#html_escape(s) ⇒ Object
(also: #h)
HTML escape.
- #html_quote(node) ⇒ Object
- #image_tag(uri, attr = '') ⇒ Object
-
#initialize(root, template = nil, cm_opt = {}) ⇒ HtmlVisitor
constructor
A new instance of HtmlVisitor.
- #link(node) ⇒ Object
-
#list_item(node) ⇒ Object
list_item.
- #lists(node) ⇒ Object
- #make_image_regs(exts) ⇒ Object
- #mk_link(node) ⇒ Object
-
#rm_html_tag(str) ⇒ Object
remove HTML tags.
- #section(node) ⇒ Object
- #section_indent(level = nil) ⇒ Object
- #src(node) ⇒ Object
- #table(node) ⇒ Object
- #table_of_contents ⇒ Object
- #table_row(node) ⇒ Object
-
#textblock(node) ⇒ Object
paragraph if @p_tag_flag == false then we do’nt output <p></p> tags.
- #textline(node) ⇒ Object
- #toc_headline(node) ⇒ Object
- #toc_out(node) ⇒ Object
- #variable(node) ⇒ Object
- #verse(node) ⇒ Object
Methods included from VisitorUtils
Constructor Details
#initialize(root, template = nil, cm_opt = {}) ⇒ HtmlVisitor
Returns a new instance of HtmlVisitor.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/org-parse/visitor.rb', line 50 def initialize(root, template = nil, cm_opt = {}) @body = @title = @add_to_head = '' @root = root @ul_stack = [] super() template = TEMPLATE unless template @erb = ERB.new(::File.read(template)) @p_tag_flag = true @list_level = 0 @indent_stack = [0] @body = '' @curr_level = 0 @para_level = 0 = cm_opt end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
48 49 50 |
# File 'lib/org-parse/visitor.rb', line 48 def body @body end |
Instance Method Details
#blockquote(node) ⇒ Object
290 291 292 293 294 295 296 297 |
# File 'lib/org-parse/visitor.rb', line 290 def blockquote(node) indent = get_indent # @p_tag_flag = false @para_level += 1 body = exec_children(node) @para_level -= 1 %Q|#{indent}<blockquote>\n#{body.chomp}\n#{indent}</blockquote>\n| end |
#blocks(node) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/org-parse/visitor.rb', line 299 def blocks(node) case node.block_name when 'VERSE' verse node when 'EXAMPLE' example node when 'QUOTE' blockquote node when 'HTML' html_quote node when 'COMMENT' '' when 'SRC' src node else puts "not implimented block=>[#{node.block_name}](#{node.inspect})" exec_children(node) end end |
#build ⇒ Object
build the HTML string
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/org-parse/visitor.rb', line 77 def build = @root. @title = exec_list @root.[:title] @language = @root.[:language] @charset = @root.[:charset] @body = '' @footnotes = [] @footnote_idxs = [] @before_text = '' @before_text = exec_list [:text] # .each {|n| @before_text += execute(n)} @add_to_head = [:style] 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 @language = @root.[:language] @erb.result(binding) end |
#clear_vars ⇒ Object
401 402 403 |
# File 'lib/org-parse/visitor.rb', line 401 def clear_vars @flash_vars.clear end |
#close_ul_sec(level) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/org-parse/visitor.rb', line 112 def close_ul_sec(level) str = '' while @ul_stack.last and @ul_stack.last > level indent = section_indent(@ul_stack.last - 1) @ul_stack.pop str += "#{indent}</ul>\n" end str end |
#example(node) ⇒ Object
255 256 257 258 259 260 261 262 263 |
# File 'lib/org-parse/visitor.rb', line 255 def example(node) indent = get_indent this_indent = 0 this_indent = node.children[0].children[0].indent @indent_stack << this_indent body = exec_children(node) @indent_stack.pop %Q|<pre class="example">\n#{ body.chomp }\n</pre>\n| end |
#execute(node) ⇒ Object
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
# File 'lib/org-parse/visitor.rb', line 472 def execute(node) # puts "node:#{node.kind} [#{node.value}]\n" # STDERR.puts node.inspect return '' if node.done? case node.kind when :SECTION section node when :HEADLINE headline node when :TEXTBLOCK textblock node when :WHITELINE, :WHITELINES @p_tag_flag = true if node.verse? "<br>\n" * node.value else "\n" * node.value end when :STRING h node.value when :QUOTE if node.value == "\n" '<br/>' else node.value end when :TEXTLINE textline node when :EMPHASIS "<b>#{ exec_children node }</b>" when :ITALIC "<i>#{ exec_children node }</i>" when :UNDER_LINE %Q|<span style="text-decoration: underline;">#{exec_children node}</span>| when :STRIKE_THROUGH "<del>#{ exec_children node}</del>" when :CODE, :VERBATIM "<code>#{ exec_children node }</code>" 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
452 453 454 455 456 457 458 459 460 |
# File 'lib/org-parse/visitor.rb', line 452 def footnote_define(node) idx = @footnote_idxs.index node.value unless idx idx = @footnote_idxs.size @footnotes_idxs << node.value end @footnotes[idx] = exec_children(node) '' end |
#footnote_link(node) ⇒ Object
442 443 444 445 446 447 448 449 450 |
# File 'lib/org-parse/visitor.rb', line 442 def footnote_link(node) idx = @footnote_idxs.index node.value unless idx idx = @footnote_idxs.size @footnote_idxs << node.value end idx += 1 %Q|<sup><a class="footref" name="fnr.#{idx}" href="#fn.#{idx}">#{idx}</a></sup> | end |
#footnotes ⇒ Object
462 463 464 465 466 467 468 469 470 |
# File 'lib/org-parse/visitor.rb', line 462 def footnotes ret = '' @footnotes.each_index{|idx| n = idx+1 val = @footnotes[idx] ret += %Q|<p class="footnote"><sup><a class="footnum" name="fn.#{n}" href="#fnr.#{n}">#{n}</a></sup> #{val}</p>\n| } ret end |
#get_indent ⇒ Object
106 107 108 109 110 |
# File 'lib/org-parse/visitor.rb', line 106 def get_indent base = section_indent base += (' ' * @list_level) base += (' ' * @para_level) end |
#headline(node) ⇒ Object
200 201 202 203 204 |
# File 'lib/org-parse/visitor.rb', line 200 def headline(node) level = node.level+1 index_str = node.parent.section_no %Q|<h#{level} id="sec-#{index_str}"><span class="section-number-#{level}">#{index_str}</span> #{exec_children(node).chomp} </h#{level}>| end |
#html_escape(s) ⇒ Object Also known as: h
HTML escape
535 536 537 |
# File 'lib/org-parse/visitor.rb', line 535 def html_escape(s) s.to_s.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<") end |
#html_quote(node) ⇒ Object
282 283 284 285 286 287 288 |
# File 'lib/org-parse/visitor.rb', line 282 def html_quote(node) indent = get_indent @indent_stack << node.children[0].children[0].indent body = exec_children(node) @indent_stack.pop body end |
#image_tag(uri, attr = '') ⇒ Object
319 320 321 |
# File 'lib/org-parse/visitor.rb', line 319 def image_tag(uri, attr = '') %Q|<img src="#{uri.sub(/^file:/,'')}"#{attr}/>| end |
#link(node) ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/org-parse/visitor.rb', line 323 def link(node) if node.caption.nil? mk_link(node) else %Q|<div class="figure"> <p>#{mk_link(node)}</p> <p>#{node.caption}</p> </div> | end end |
#list_item(node) ⇒ Object
list_item
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/org-parse/visitor.rb', line 418 def list_item(node) indent = get_indent if node.children.empty? if node.type == :DL_START "#{indent}<dt>#{exec_list(node.dt)}</dt>\n#{indent}<dd>#{exec_list(node.value).chomp}</dd>\n" else "#{indent}<li>#{exec_list(node.value).chomp}</li>\n" end else @list_level += 1 @p_tag_flag = false str = get_indent + exec_list(node.value) str += exec_children node if node.type == :DL_START str = "#{indent}<dt>#{exec_list(node.dt)}</dt>\n#{indent}<dd>\n#{str.chomp}\n#{indent}</dd>\n" else str = "#{indent}<li>\n#{str.chomp}\n#{indent}</li>\n" end @p_tag_flag = true @list_level -= 1 str end end |
#lists(node) ⇒ Object
405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/org-parse/visitor.rb', line 405 def lists(node) = { :UNORDERED_LIST => ['<ul>', '</ul>'], :ORDERED_LIST => ['<ol>', '</ol>'], :DEFINITION_LIST => ['<dl>', '</dl>'], } indent = get_indent @list_level += 1 body = exec_children node @list_level -= 1 "#{indent}#{tags[node.kind][0]}\n#{body.chomp}\n#{indent}#{tags[node.kind][1]}\n" end |
#make_image_regs(exts) ⇒ Object
66 67 68 |
# File 'lib/org-parse/visitor.rb', line 66 def make_image_regs(exts) @image_file_reg = /\.(#{exts.join('|')})$/ end |
#mk_link(node) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/org-parse/visitor.rb', line 335 def mk_link(node) a_attrs = ['href', 'name', 'target', 'charset', 'hreflang', 'type', 'rel', 'rev', 'tabindex', 'accesskey', 'shape', 'coords'] a_attr = '' img_attr = '' node..each do |k, v| if a_attrs.include? k a_attr = ' ' if a_attr.empty? a_attr += "#{k}=#{v}" else img_attr = ' ' if img_attr.empty? img_attr += "#{k}=#{v} " end end desc = exec_children node if desc.empty? if node.uri =~ @image_file_reg image_tag(node.uri, a_attr+img_attr) else %Q|<a href="#{node.uri.sub(/^file:/,'')}"#{a_attr+img_attr}>node.uri</a>| end else if desc =~ @image_file_reg desc = image_tag desc, img_attr else a_attr += img_attr end %Q|<a href="#{node.uri.sub(/^file:/,'')}"#{a_attr+img_attr}>#{desc}</a>| end end |
#rm_html_tag(str) ⇒ Object
remove HTML tags
71 72 73 74 |
# File 'lib/org-parse/visitor.rb', line 71 def rm_html_tag(str) str.sub!(/<[^<>]*>/,"") while /<[^<>]*>/ =~ str str end |
#section(node) ⇒ Object
171 172 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/visitor.rb', line 171 def section(node) curr_level = node.headline.level @curr_level = curr_level indent = section_indent(curr_level - 1) idx_no = node.section_no if curr_level > [:H] str = close_ul_sec(curr_level) if @ul_stack.last and @ul_stack.last == curr_level str += %Q|#{indent} <li id="sec-#{idx_no}">#{exec_children node}</li>\n| else str += "#{indent}<ul>\n" @ul_stack << curr_level str += %Q|#{indent} <li id="sec-#{idx_no}">#{exec_children node}</li>\n| end str else %Q|#{close_ul_sec(curr_level).chomp} #{indent}<div id="outline-container-#{idx_no}" class="outline-#{curr_level+1}"> #{indent} #{headline node.headline} #{indent} <div class="outline-text-#{@curr_level+1}" id="text-#{idx_no}"> #{exec_children node} #{close_ul_sec(curr_level).chomp} #{indent} </div> #{indent}</div> | end end |
#section_indent(level = nil) ⇒ Object
101 102 103 104 |
# File 'lib/org-parse/visitor.rb', line 101 def section_indent(level = nil) level = @curr_level if level.nil? ' ' * level end |
#src(node) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/org-parse/visitor.rb', line 265 def src(node) if [:uv] text = exec_children(node) syntax = node.syntax syntax = 'lisp' if syntax == 'emacs-lisp' theme = node.syntax_theme theme = 'amy' if theme.empty? begin Uv.parse( text, "xhtml", syntax, true, theme) rescue "uv can't parse #{syntax}/#{theme} <br>\n" + example(node) end else example node end end |
#table(node) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/org-parse/visitor.rb', line 368 def table(node) indent = get_indent ret = indent + %Q|<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">\n| ret += indent + ' ' + %Q|<caption></caption>\n| #ret += indent + ' ' + %Q|<colgroup><col align="left" /><col align="left" /><col align="left" /></colgroup>\n| ret += indent + ' ' + "<tbody>\n" ret += exec_list node.hrows ret += exec_children node ret += indent + ' ' + "</tbody>\n" ret += indent + "</table>" end |
#table_of_contents ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/org-parse/visitor.rb', line 122 def table_of_contents toc = "<ul>\n" @root.children.each {|node| if node.kind == :SECTION toc += toc_out node end } toc += "</ul>\n" return '' if toc == "<ul>\n</ul>\n" ret ="<div id=\"table-of-contents\">\n <h2>Table of Contents</h2>\n <div id=\"text-table-of-contents\">\n\#{toc}\n </div>\n</div>\n" end |
#table_row(node) ⇒ Object
380 381 382 383 384 385 386 387 388 389 |
# File 'lib/org-parse/visitor.rb', line 380 def table_row(node) indent = get_indent + " " ret = "" s = node.is_head? ? '<th>' : '<td>' e = node.is_head? ? '</th>' : '</td>' node.children.each {|col| ret += s + exec_list(col) + e } indent + "<tr>\n " + indent + ret + "\n" + indent + "</tr>\n" end |
#textblock(node) ⇒ Object
paragraph if @p_tag_flag == false then we do’nt output <p></p> tags.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/org-parse/visitor.rb', line 208 def textblock(node) if node.verse? or node.example? or node.html? or node.src? exec_children node elsif @p_tag_flag indent = get_indent @para_level += 1 str = "#{indent}<p>\n#{exec_children(node).chomp}\n#{indent}</p>\n" @para_level -= 1 str else str = exec_children node @p_tag_flag = true # next paragraph has <p> str end end |
#textline(node) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/org-parse/visitor.rb', line 224 def textline(node) if node.example? or (node.src? and ![:uv]) indent = ' ' * @indent_stack.last return h node.value.sub(/^#{indent}/,'') elsif node.src? and [:uv] return node.value elsif node.html? return node.value else indent = get_indent str = exec_children(node).sub(/^\s*/, '') if node.verse? n = node.indent - @indent_stack.last n = 0 if n < 0 indent = ' ' * n indent + str.gsub(/\n/, "<br/>\n") else indent + str end end end |
#toc_headline(node) ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'lib/org-parse/visitor.rb', line 161 def toc_headline(node) str = '' node.children.each do |item| unless item.kind == :FN_LINK str += execute item end end str end |
#toc_out(node) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/org-parse/visitor.rb', line 141 def toc_out(node) curr_level = node.headline.level idx_no = node.section_no str = toc_headline node.headline ret = %Q|<li><a href="#sec-#{idx_no}">#{idx_no} #{str}</a>| has_child = false node.children.each {|node| if node.kind == :SECTION ret += "\n<ul>\n" unless has_child has_child = true ret += toc_out node end } if has_child ret += "</ul>\n" ret += "</li>\n" end ret end |
#variable(node) ⇒ Object
391 392 393 394 395 396 397 398 399 |
# File 'lib/org-parse/visitor.rb', line 391 def variable(node) fvs = ['CAPTION', 'ATTR_HTML', 'TEXT'] if fvs.include? node.name @flash_vars[node.name] = node.value else [node.name] = node.value end '' end |
#verse(node) ⇒ Object
246 247 248 249 250 251 252 253 |
# File 'lib/org-parse/visitor.rb', line 246 def verse(node) pre = get_indent + %Q|<p class="verse">\n| post = get_indent + "</p>\n" @indent_stack << node.indent body = exec_children node @indent_stack.pop pre + body.chomp + "\n" + post end |