# -*- coding: utf-8 -*- require 'rubygems' require 'erb' require 'uv' module OrgParse module VisitorUtils def execute(node) puts "not implimented => #{node.inspect}" end def exec_list(nodes) ret = '' nodes.each{|n| ret += execute(n) } ret end def exec_children(node) exec_list node.children end end class OrgVisitor @@image_exts = ['jpeg', 'jpg', 'png', 'gif', 'bmp', 'tiff', 'srg'] def initialize make_image_regs @@image_exts end def make_image_regs(exts) @image_file_reg = /\.(#{exts.join('|')})$/ end end # todo: # \\- ­ \\-  # -- – -- # --- — --- # ... … \ldots # output HTML format class HtmlVisitor < OrgVisitor include VisitorUtils TEMPLATE = ::File.join(OrgParse::LIBPATH , 'org-parse', 'templates', 'single.html.erb') attr_reader :body 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 def make_image_regs(exts) @image_file_reg = /\.(#{exts.join('|')})$/ end # remove HTML tags def rm_html_tag(str) str.sub!(/<[^<>]*>/,"") while /<[^<>]*>/ =~ str str end # build the HTML string def build @options = @root.options @title = exec_list @root.options[:title] @language = @root.options[:language] @charset = @root.options[: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.options[:language] @erb.result(binding) end def section_indent(level = nil) level = @curr_level if level.nil? ' ' * level end def get_indent base = section_indent base += (' ' * @list_level) base += (' ' * @para_level) end 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}\n" end str end def table_of_contents toc = "\n" return '' if toc == "\n" ret =<<"EOS"

Table of Contents

#{toc}
EOS end def toc_out(node) curr_level = node.headline.level idx_no = node.section_no str = toc_headline node.headline ret = %Q|
  • #{idx_no} #{str}| has_child = false node.children.each {|node| if node.kind == :SECTION ret += "\n\n" ret += "
  • \n" end ret end def toc_headline(node) str = '' node.children.each do |item| unless item.kind == :FN_LINK str += execute item end end str end 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}
  • #{exec_children node}
  • \n| else str += "#{indent}