module Sablon class HTMLConverter class Node def accept(visitor) visitor.visit(self) end def self.node_name @node_name ||= name.split('::').last end end class Collection < Node attr_reader :nodes def initialize(nodes) @nodes = nodes end def accept(visitor) super @nodes.each do |node| node.accept(visitor) end end def to_docx nodes.map(&:to_docx).join end end class Root < Collection def to_a nodes end def grep(pattern) visitor = GrepVisitor.new(pattern) accept(visitor) visitor.result end end class Paragraph < Node attr_accessor :style, :runs def initialize(style, runs) @style, @runs = style, runs end PATTERN = <<-XML.gsub("\n", "") %s %s XML def to_docx PATTERN % [style, ppr_docx, runs.to_docx] end def accept(visitor) super runs.accept(visitor) end private def ppr_docx end end class ListParagraph < Paragraph LIST_STYLE = <<-XML.gsub("\n", "") XML attr_accessor :numid, :ilvl def initialize(style, runs, numid, ilvl) super style, runs @numid = numid @ilvl = ilvl end private def ppr_docx LIST_STYLE % [@ilvl, numid] end end class Text < Node attr_reader :string def initialize(string) @string = string end def to_docx "#{style_docx}#{normalized_string}" end private def style_docx end def normalized_string string.tr("\u00A0", ' ') end end class Bold < Text def style_docx '' end end class Italic < Text def style_docx '' end end class Newline < Node def to_docx "" end end end end