Sha256: 01af0ad3ab0877054e3381bb641f19251274a7537634a97c2a4d3d470a7ef35e

Contents?: true

Size: 1.87 KB

Versions: 7

Compression:

Stored size: 1.87 KB

Contents

require 'rt/rtparser'

require 'rabbit/element'

module Rabbit

  class RT2RabbitVisitor

    def initialize(rd_visitor)
      @rd_visitor = rd_visitor
    end

    def visit(parsed)
      @rt = parsed
      @header = @rt.header
      @body = @rt.body
      @caption = @rt.config['caption']

      process
    end

    private
    def process
      props = {
        "caption" => @caption
      }
      table = Element::Table.new(props)
      process_header(table)
      process_body(table)
      table
    end

    def process_block(table, targets, block_class, cell_class)
      return if targets.empty?
      block = block_class.new
      targets.each do |r|
        row = Element::TableRow.new
        each_cell(r) do |c|
          tree = ::RD::RDTree.new("=begin\n#{c.value}\n=end\n")
          if tree.root.children.empty?
            elements = []
          else
            elements = tree.root.children[0].accept(@rd_visitor).elements
          end
          cell = cell_class.new(elements)
          setup_text_align(cell, c.align)
          row << cell
        end
        block << row
      end
      table << block
    end

    def process_header(table)
      process_block(table, @header, Element::TableHead, Element::TableHeader)
    end

    def process_body(table)
      process_block(table, @body, Element::TableBody, Element::TableCell)
    end

    def each_cell(ary)
      ary.each do |x|
        if x.is_a?(RT::RTCell)
          yield x
        end
      end
    end

    def setup_text_align(target, align)
      case align
      when :center
        def target.default_align
          Pango::Layout::ALIGN_CENTER
        end
      when :right
        def target.default_align
          Pango::Layout::ALIGN_RIGHT
        end
      else
        def target.default_align
          Pango::Layout::ALIGN_LEFT
        end
      end
      target.align = target.default_align
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rabbit-2.2.0 lib/rabbit/parser/rd/rt/rt2rabbit-lib.rb
rabbit-2.1.9 lib/rabbit/parser/rd/rt/rt2rabbit-lib.rb
rabbit-2.1.8 lib/rabbit/parser/rd/rt/rt2rabbit-lib.rb
rabbit-2.1.7 lib/rabbit/parser/rd/rt/rt2rabbit-lib.rb
rabbit-2.1.6 lib/rabbit/parser/rd/rt/rt2rabbit-lib.rb
rabbit-2.1.5 lib/rabbit/parser/rd/rt/rt2rabbit-lib.rb
rabbit-2.1.4 lib/rabbit/parser/rd/rt/rt2rabbit-lib.rb