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
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
- #graphviz(node) ⇒ 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.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/org-parse/visitor.rb', line 68 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 @options = cm_opt end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
66 67 68 |
# File 'lib/org-parse/visitor.rb', line 66 def body @body end |
Instance Method Details
#blockquote(node) ⇒ Object
311 312 313 314 315 316 317 318 |
# File 'lib/org-parse/visitor.rb', line 311 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
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/org-parse/visitor.rb', line 331 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 'DOT' graphviz 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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/org-parse/visitor.rb', line 95 def build @options = @root. @title = exec_list @root.[:title] @language = @root.[:language] @charset = @root.[:charset] @body = '' @footnotes = [] @footnote_idxs = [] @before_text = '' @before_text = exec_list @options[:text] # .each {|n| @before_text += execute(n)} @add_to_head = @options[: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
435 436 437 |
# File 'lib/org-parse/visitor.rb', line 435 def clear_vars @flash_vars.clear end |
#close_ul_sec(level) ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/org-parse/visitor.rb', line 130 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
276 277 278 279 280 281 282 283 284 |
# File 'lib/org-parse/visitor.rb', line 276 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
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 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'lib/org-parse/visitor.rb', line 506 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
486 487 488 489 490 491 492 493 494 |
# File 'lib/org-parse/visitor.rb', line 486 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
476 477 478 479 480 481 482 483 484 |
# File 'lib/org-parse/visitor.rb', line 476 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
496 497 498 499 500 501 502 503 504 |
# File 'lib/org-parse/visitor.rb', line 496 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
124 125 126 127 128 |
# File 'lib/org-parse/visitor.rb', line 124 def get_indent base = section_indent base += (' ' * @list_level) base += (' ' * @para_level) end |
#graphviz(node) ⇒ Object
320 321 322 323 324 325 326 327 328 329 |
# File 'lib/org-parse/visitor.rb', line 320 def graphviz(node) text = exec_children(node) g = GraphViz::Parser.parse_str(text, {}) fname = node.filename fname = File.join(@options[:dot_path], node.filename) unless @options[:dot_path].empty? g.output(node.type => fname) ub = '' ub = @options[:dot_url_base] if @options[:dot_url_base] %Q|<img src="#{ub + node.filename}" />| end |
#headline(node) ⇒ Object
217 218 219 220 221 222 223 224 225 |
# File 'lib/org-parse/visitor.rb', line 217 def headline(node) level = node.level+1 index_str = node.parent.section_no if @options[:num] %Q|<h#{level} id="sec-#{index_str}"><span class="section-number-#{level}">#{index_str}</span> #{exec_children(node).chomp} </h#{level}>| else %Q|<h#{level} id="sec-#{index_str}">#{exec_children(node).chomp} </h#{level}>| end end |
#html_escape(s) ⇒ Object Also known as: h
HTML escape
569 570 571 |
# File 'lib/org-parse/visitor.rb', line 569 def html_escape(s) s.to_s.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<") end |
#html_quote(node) ⇒ Object
303 304 305 306 307 308 309 |
# File 'lib/org-parse/visitor.rb', line 303 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
353 354 355 |
# File 'lib/org-parse/visitor.rb', line 353 def image_tag(uri, attr = '') %Q|<img src="#{uri.sub(/^file:/,'')}"#{attr}/>| end |
#link(node) ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/org-parse/visitor.rb', line 357 def link(node) if node..nil? mk_link(node) else %Q|<div class="figure"> <p>#{mk_link(node)}</p> <p>#{node.}</p> </div> | end end |
#list_item(node) ⇒ Object
list_item
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/org-parse/visitor.rb', line 452 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
439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/org-parse/visitor.rb', line 439 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}#{[node.kind][0]}\n#{body.chomp}\n#{indent}#{[node.kind][1]}\n" end |
#make_image_regs(exts) ⇒ Object
84 85 86 |
# File 'lib/org-parse/visitor.rb', line 84 def make_image_regs(exts) @image_file_reg = /\.(#{exts.join('|')})$/ end |
#mk_link(node) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/org-parse/visitor.rb', line 369 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
89 90 91 92 |
# File 'lib/org-parse/visitor.rb', line 89 def rm_html_tag(str) str.sub!(/<[^<>]*>/,"") while /<[^<>]*>/ =~ str str end |
#section(node) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/org-parse/visitor.rb', line 188 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 > @options[: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
119 120 121 122 |
# File 'lib/org-parse/visitor.rb', line 119 def section_indent(level = nil) level = @curr_level if level.nil? ' ' * level end |
#src(node) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/org-parse/visitor.rb', line 286 def src(node) if @options[: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
402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/org-parse/visitor.rb', line 402 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
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/org-parse/visitor.rb', line 140 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 =<<"EOS" <div id="text-table-of-contents"> #{toc} </div> EOS end |
#table_row(node) ⇒ Object
414 415 416 417 418 419 420 421 422 423 |
# File 'lib/org-parse/visitor.rb', line 414 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.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/org-parse/visitor.rb', line 229 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
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/org-parse/visitor.rb', line 245 def textline(node) if node.example? or (node.src? and !@options[:uv]) indent = ' ' * @indent_stack.last return h node.value.sub(/^#{indent}/,'') elsif node.src? and @options[: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
178 179 180 181 182 183 184 185 186 |
# File 'lib/org-parse/visitor.rb', line 178 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
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/org-parse/visitor.rb', line 156 def toc_out(node) curr_level = node.headline.level idx_no = node.section_no idx_label = node.section_no + ' ' idx_label = '' unless @options[:num] str = toc_headline node.headline ret = %Q|<li><a href="#sec-#{idx_no}">#{idx_label}#{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
425 426 427 428 429 430 431 432 433 |
# File 'lib/org-parse/visitor.rb', line 425 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 |
#verse(node) ⇒ Object
267 268 269 270 271 272 273 274 |
# File 'lib/org-parse/visitor.rb', line 267 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 |