Sha256: 4b081c6b231de38b2e9dbfaf332aa8c20771e2526872a60e93c6e0a4852d5d1d

Contents?: true

Size: 993 Bytes

Versions: 10

Compression:

Stored size: 993 Bytes

Contents

# frozen_string_literal: true

module DocTemplate
  module Tags
    class TablePreserveAlignmentTag < BaseTag
      STYLE_RE = /text-align:(left|center|right)/i.freeze
      TAG_NAME = 'table-preserve-alignment'

      def parse(node, _options = {})
        if (table = find_table node)
          # inside cells for each `p` with `text-align` css param we add specific class
          table.xpath('.//p').each do |el|
            if (m = STYLE_RE.match el['style'])
              el['style'] = el['style'].sub STYLE_RE, ''
              el['class'] = "text-#{m[1]}"
            end
          end

          @content = table.to_s
          replace_tag table
        end

        node.remove

        self
      end

      private

      def find_table(node)
        while (node = node.next_sibling)
          return node if node.name.casecmp('table').zero?
        end
      end
    end

    Template.register_tag(Tags::TablePreserveAlignmentTag::TAG_NAME, TablePreserveAlignmentTag)
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lcms-engine-0.5.5 lib/doc_template/tags/table_preserve_alignment_tag.rb
lcms-engine-0.5.4 lib/doc_template/tags/table_preserve_alignment_tag.rb
lcms-engine-0.5.3 lib/doc_template/tags/table_preserve_alignment_tag.rb
lcms-engine-0.5.2 lib/doc_template/tags/table_preserve_alignment_tag.rb
lcms-engine-0.5.1 lib/doc_template/tags/table_preserve_alignment_tag.rb
lcms-engine-0.5.0 lib/doc_template/tags/table_preserve_alignment_tag.rb
lcms-engine-0.4.2 lib/doc_template/tags/table_preserve_alignment_tag.rb
lcms-engine-0.4.1 lib/doc_template/tags/table_preserve_alignment_tag.rb
lcms-engine-0.4.0 lib/doc_template/tags/table_preserve_alignment_tag.rb
lcms-engine-0.3.1 lib/doc_template/tags/table_preserve_alignment_tag.rb