Sha256: 3274c684fc984d9483415f9f0364fde812e87a5f3da91d59a74c385e4d39b16e

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

module BootstrapEmail
  module Converter
    class Base
      attr_reader :doc
      def initialize(doc)
        @doc = doc
        @cached_templates = {}
      end

      def self.build(doc)
        new(doc).build
      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

5 entries across 5 versions & 1 rubygems

Version Path
bootstrap-email-1.1.4 lib/bootstrap-email/converters/base.rb
bootstrap-email-1.1.3 lib/bootstrap-email/converters/base.rb
bootstrap-email-1.1.2 lib/bootstrap-email/converters/base.rb
bootstrap-email-1.1.1 lib/bootstrap-email/converters/base.rb
bootstrap-email-1.1.0 lib/bootstrap-email/converters/base.rb