Sha256: 0d170df983931e2a27d5ab8e90f2207e94204b63b96bd3a525ceca03c5235e68

Contents?: true

Size: 1.68 KB

Versions: 7

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module BootstrapEmail
  module Converter
    class Base
      attr_reader :doc

      def initialize(doc)
        @doc = doc
        @cached_templates = {}
      end

      def self.build(doc, *args)
        new(doc).build(*args)
      end

      private

      def template(file, locals_hash = {})
        locals_hash[:classes] = locals_hash[:classes].to_s.split.join(' ')
        locals_hash[:content] ||= nil
        if @cached_templates[file]
          string = @cached_templates[file]
        else
          path = File.expand_path("../../../core/templates/#{file}.html", __dir__)
          string = File.read(path).chop # read and remove trailing newline
          @cached_templates[file] = string
        end
        locals_hash.each do |key, value|
          string = string.sub("{{ #{key} }}", value.to_s)
        end
        string
      end

      def each_node(css_lookup, &blk)
        # sort by youngest child and traverse backwards up the tree
        doc.css(css_lookup).sort_by { |n| n.ancestors.size }.reverse!.each(&blk)
      end

      def add_class(node, class_name)
        node['class'] ||= ''
        # remove double spaces and strip white space from ends
        node['class'] = "#{node['class']} #{class_name}".gsub(/\s+/, ' ').strip
      end

      def margin?(node)
        margin_top?(node) || margin_bottom?(node)
      end

      def margin_top?(node)
        node['class'].to_s.match?(/m[ty]{1}-(lg-)?\d+/)
      end

      def margin_bottom?(node)
        node['class'].to_s.match?(/m[by]{1}-(lg-)?\d+/)
      end

      def table?(node)
        node.name == 'table'
      end

      def td?(node)
        node.name == 'td'
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bootstrap-email-1.5.1 lib/bootstrap-email/converters/base.rb
bootstrap-email-1.5.0 lib/bootstrap-email/converters/base.rb
bootstrap-email-1.4.1 lib/bootstrap-email/converters/base.rb
bootstrap-email-1.4.0 lib/bootstrap-email/converters/base.rb
bootstrap-email-1.3.1 lib/bootstrap-email/converters/base.rb
bootstrap-email-1.3.0 lib/bootstrap-email/converters/base.rb
bootstrap-email-1.2.0 lib/bootstrap-email/converters/base.rb