Sha256: 461ff8f80d3ece8d8f9822282690b243534bd1b573e2380d63d0bebe8487dfd7

Contents?: true

Size: 1.24 KB

Versions: 10

Compression:

Stored size: 1.24 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']} text-bold" if el['style'] =~ STYLES_REGEXP[:bold]
          el['class'] = "#{el['class']} 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

10 entries across 10 versions & 1 rubygems

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