require_relative 'dom' module OLD class RoffBuilder include DOM attr_accessor :title, :section, :date, :source, :manual, :hr_width, :name def initialize(elem) @elem = elem end def build() notable_tags =["p", "hr", "blockquote", "div/blockquote", "br", "table"] query = notable_tags.join('|') paragraphs = xpath(@elem, query).map do |node| html2roff(node) end body = paragraphs.join("") <<-"EOS" .TH "#{@title}" #{@section} "#{@date.strftime("%Y-%m-%d")}" "#{@source}" "#{@manual}" .SH NAME #{@name} #{body} EOS end def span2roff(e, child) child end def is_section_title?(string) # regard as a section title when: # * inner text ends width ':' # like "Description:", "Item #:" ... # * inner text contains characteristic words title_patterns = [ /^Item #:$/i, /^Special Containment Procedures:$/i, /^Object class:$/i, /^Status/i, /^Description:$/i, /^[a-zA-Z ]*Log/i, /^Addendum/i, /^Appendix/i, /^Notes [0-9]+/i, /^Document.*[0-9]+/i ] title_patterns.any? {|pattern| string =~ pattern } end def table2roff(table) rows = xpath("table, tr") column = xpath(rows.first, "td").length separater = "^" content = rows.map { |tr| row = xpath(tr, "td").map { |td| elem2roff(td, false) + separater }.join("") }.join("\n") < 0, :order => 0}) if is_text?(e) return text(e) + (text(e)[-1] == '.' ? " ": "") else elem2roff(e, context) end end end end