Sha256: a88590935692186cb807982b4132af937dd7109f617b6230de755e616bdb89c3

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module DocTemplate
  module Tags
    class InsetTag < BlockTag
      STYLES_REGEXP = {
        bold: /font-weight:[6-9]00/i,
        italic: /font-style:italic/i
      }.freeze
      TAG_NAME = 'inset'

      def parse(node, opts = {})
        nodes = block_nodes(node) { |n| preserve_styles n, opts }
        content = parse_nested nodes.map(&:to_html).join, opts
        nodes.each(&:remove)
        @content =
          if gdoc?(opts)
            %(#{content}<p class="do-not-strip"></p>)
          else
            %(<div class="o-ld-inset">#{content}</div>)
          end
        replace_tag node
        self
      end

      private

      def preserve_styles(node, opts)
        add_css_class(node, 'o-ld-inset') if gdoc?(opts)
        node.children.each do |el|
          el['class'] = el['class'].to_s + ' text-bold' if el['style'] =~ STYLES_REGEXP[:bold]
          el['class'] = el['class'].to_s + ' text-italic' if el['style'] =~ STYLES_REGEXP[:italic]
        end
        node
      end

      def add_css_class(el, *classes)
        existing = (el[:class] || '').split(/\s+/)
        el[:class] = existing.concat(classes).uniq.join(' ')
      end
    end
  end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lcms-engine-0.1.4 lib/doc_template/tags/inset_tag.rb
lcms-engine-0.3.0 lib/doc_template/tags/inset_tag.rb
lcms-engine-0.1.3 lib/doc_template/tags/inset_tag.rb
lcms-engine-0.2.0 lib/doc_template/tags/inset_tag.rb
lcms-engine-0.1.2 lib/doc_template/tags/inset_tag.rb
lcms-engine-0.1.0 lib/doc_template/tags/inset_tag.rb