bootstrap-email 1.1.7 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1145fbba3e6a8db2d8fe571b8f5a4d05ff78ba0ddd4c40dea9ce317e06e5f1fe
4
- data.tar.gz: f2aa2c03962d575448af829b381f18cfde6bacee0a2caf03b66a9ae0f218824f
3
+ metadata.gz: c82d69b4546e02daeeff5fc09df8e91c3c372f6da42e39fd96b438bd0776a7c3
4
+ data.tar.gz: 8cc3f250c82b779ddb99ae0d9618523b716d8110e0cbd3b639a71255158c181d
5
5
  SHA512:
6
- metadata.gz: 150353d9f8a03a0b8fe0434013c5c814a97dc2eb1b03287517174c95d9df30fa22e1564d3dc8d2fc7cc9bc33734da0d845341cc4d8e07bfee1ca23db84490a9e
7
- data.tar.gz: ce8b05c478d2bda5a46518d90a2bb3c8c6c8a1fe01bf197524b95c2b3182d5ca9f9b24e08bce6022f3af68779d7b087c54605dbe3b861b32bd601a1f551f5e8d
6
+ metadata.gz: 7154cdcef0287e4fbcb37f117f5c73ae4dbf15b11a28b287b13b082d1ac05b63d5010b664a230cf907e7eafb8c27901db5e584db8a65fa7eb7ea1101cf42c6a1
7
+ data.tar.gz: bc5f07bdcb6ffca350685b5b39114d121ed27e33d4e18b24ce624143a4bd29f7210e405958634e1b86bc0ae6acdd5b70c259b5a77c805a3085d345a392f412c0
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.7
1
+ 1.2.0
@@ -2,10 +2,10 @@
2
2
 
3
3
  module BootstrapEmail
4
4
  class Compiler
5
- attr_accessor :type, :doc, :premailer
5
+ attr_accessor :type, :config, :doc, :premailer
6
6
 
7
7
  def initialize(input, type: :string, options: {})
8
- BootstrapEmail.load_options(options)
8
+ self.config = BootstrapEmail::Config.new(options)
9
9
  self.type = type
10
10
  case type
11
11
  when :rails
@@ -41,11 +41,11 @@ module BootstrapEmail
41
41
  end
42
42
 
43
43
  def sass_load_paths
44
- SassC.load_paths << BootstrapEmail.config.sass_load_paths
44
+ SassC.load_paths << config.sass_load_paths
45
45
  end
46
46
 
47
47
  def build_premailer_doc(html)
48
- css_string = BootstrapEmail::SassCache.compile('bootstrap-email', style: :expanded)
48
+ css_string = BootstrapEmail::SassCache.compile('bootstrap-email', config, style: :expanded)
49
49
  self.premailer = Premailer.new(
50
50
  html,
51
51
  with_html_string: true,
@@ -86,7 +86,7 @@ module BootstrapEmail
86
86
  end
87
87
 
88
88
  def configure_html!
89
- BootstrapEmail::Converter::HeadStyle.build(doc)
89
+ BootstrapEmail::Converter::HeadStyle.build(doc, config)
90
90
  BootstrapEmail::Converter::AddMissingMetaTags.build(doc)
91
91
  BootstrapEmail::Converter::VersionComment.build(doc)
92
92
  end
@@ -2,51 +2,64 @@
2
2
 
3
3
  module BootstrapEmail
4
4
  class Config
5
- attr_writer :sass_email_location, # path to main sass file
6
- :sass_head_location, # path to head sass file
7
- :sass_load_paths, # array of directories for loading sass imports
8
- :sass_cache_location, # path to tmp folder for sass cache
9
- :sass_log_enabled # turn on or off sass log when caching new sass
10
-
11
- def load_options(options)
5
+ def initialize(options = {})
6
+ @config_store = BootstrapEmail::ConfigStore.new(options)
12
7
  file = File.expand_path('bootstrap-email.config.rb', Dir.pwd)
13
8
  if options[:config_path]
14
9
  require_relative options[:config_path]
15
10
  elsif File.exist?(file)
16
11
  require_relative file
17
12
  end
18
- options.each { |name, value| instance_variable_set("@#{name}", value) }
19
13
  end
20
14
 
21
- def sass_location_for(type:)
22
- ivar = instance_variable_get("@sass_#{type.sub('bootstrap-', '')}_location")
23
- return ivar if ivar
15
+ def sass_string_for(type:)
16
+ # look for custom sass string
17
+ sub_type = type.sub('bootstrap-', '')
18
+ string = config_for_option("sass_#{sub_type}_string")
19
+ return string unless string.nil?
20
+
21
+ # look for custom sass path
22
+ path = config_for_option("sass_#{sub_type}_location")
23
+ return File.read(path) unless path.nil?
24
24
 
25
+ # look up and return others if found in default locations
25
26
  lookup_locations = ["#{type}.scss", "app/assets/stylesheets/#{type}.scss"]
26
27
  locations = lookup_locations.map { |location| File.expand_path(location, Dir.pwd) }.select { |location| File.exist?(location) }
27
- locations.first if locations.any?
28
+ File.read(locations.first) if locations.any?
28
29
  end
29
30
 
30
31
  def sass_load_paths
31
32
  paths_array = [SassCache::SASS_DIR]
32
- @sass_load_paths ||= []
33
- paths_array.concat(@sass_load_paths)
33
+ custom_load_paths = config_for_option(:sass_load_paths) || []
34
+ paths_array.concat(custom_load_paths)
34
35
  end
35
36
 
36
37
  def sass_cache_location
37
- @sass_cache_location ||= begin
38
- if defined?(::Rails) && ::Rails.root
39
- ::Rails.root.join('tmp', 'cache', 'bootstrap-email', '.sass-cache')
40
- elsif File.writable?(Dir.pwd)
41
- File.join(Dir.pwd, '.sass-cache', 'bootstrap-email')
42
- else
43
- File.join(Dir.tmpdir, '.sass-cache', 'bootstrap-email')
44
- end
38
+ option = config_for_option(:sass_cache_location)
39
+ return option unless option.nil?
40
+
41
+ if defined?(::Rails) && ::Rails.root
42
+ ::Rails.root.join('tmp', 'cache', 'bootstrap-email', '.sass-cache')
43
+ elsif File.writable?(Dir.pwd)
44
+ File.join(Dir.pwd, '.sass-cache', 'bootstrap-email')
45
+ else
46
+ File.join(Dir.tmpdir, '.sass-cache', 'bootstrap-email')
45
47
  end
46
48
  end
47
49
 
48
50
  def sass_log_enabled?
49
- defined?(@sass_log_enabled) ? @sass_log_enabled : true
51
+ option = config_for_option(:sass_log_enabled)
52
+ option.nil? ? true : option
53
+ end
54
+
55
+ private
56
+
57
+ def config_for_option(option)
58
+ if @config_store.did_set?(option)
59
+ @config_store.public_send(option)
60
+ else
61
+ BootstrapEmail.static_config.public_send(option)
62
+ end
50
63
  end
51
64
  end
52
65
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BootstrapEmail
4
+ class ConfigStore
5
+ OPTIONS = [
6
+ :sass_email_location, # path to main sass file
7
+ :sass_head_location, # path to head sass file
8
+ :sass_email_string, # main sass file passed in as a string
9
+ :sass_head_string, # head sass file passed in as a string
10
+ :sass_load_paths, # array of directories for loading sass imports
11
+ :sass_cache_location, # path to tmp folder for sass cache
12
+ :sass_log_enabled # turn on or off sass log when caching new sass
13
+ ].freeze
14
+
15
+ attr_reader(*OPTIONS)
16
+
17
+ OPTIONS.each do |option|
18
+ define_method("#{option}=") do |value|
19
+ instance_variable_set("@#{option}", value)
20
+ end
21
+ end
22
+
23
+ def initialize(options = [])
24
+ options.each { |name, value| instance_variable_set("@#{name}", value) if OPTIONS.include?(name) }
25
+ end
26
+
27
+ def did_set?(option)
28
+ instance_variable_defined?("@#{option}")
29
+ end
30
+ end
31
+ end
@@ -10,8 +10,8 @@ module BootstrapEmail
10
10
  @cached_templates = {}
11
11
  end
12
12
 
13
- def self.build(doc)
14
- new(doc).build
13
+ def self.build(doc, *args)
14
+ new(doc).build(*args)
15
15
  end
16
16
 
17
17
  private
@@ -3,7 +3,8 @@
3
3
  module BootstrapEmail
4
4
  module Converter
5
5
  class HeadStyle < Base
6
- def build
6
+ def build(config)
7
+ @config = config
7
8
  doc.at_css('head').add_child(bootstrap_email_head)
8
9
  end
9
10
 
@@ -18,7 +19,7 @@ module BootstrapEmail
18
19
  end
19
20
 
20
21
  def purged_css_from_head
21
- default, custom = BootstrapEmail::SassCache.compile('bootstrap-head').split('/*! allow_purge_after */')
22
+ default, custom = BootstrapEmail::SassCache.compile('bootstrap-head', @config).split('/*! allow_purge_after */')
22
23
  # get each CSS declaration
23
24
  custom.scan(/\w*\.[\w\-]*[\s\S\n]+?(?=})}{1}/).each do |group|
24
25
  # get the first class for each comma separated CSS declaration
@@ -4,22 +4,20 @@ module BootstrapEmail
4
4
  class SassCache
5
5
  SASS_DIR = File.expand_path('../../core', __dir__)
6
6
 
7
- def self.compile(type, style: :compressed)
8
- new(type, style).compile
7
+ def self.compile(type, config, style: :compressed)
8
+ new(type, config, style).compile
9
9
  end
10
10
 
11
- attr_accessor :type, :style, :file_path, :config_file, :checksum
11
+ attr_accessor :type, :config, :style, :file_path, :sass_config, :checksum, :cache_dir
12
12
 
13
- def initialize(type, style)
13
+ def initialize(type, config, style)
14
14
  self.type = type
15
+ self.config = config
15
16
  self.style = style
16
17
  self.file_path = "#{SASS_DIR}/#{type}"
17
- self.config_file = load_config
18
+ self.sass_config = load_sass_config
18
19
  self.checksum = checksum_files
19
- end
20
-
21
- def cache_dir
22
- BootstrapEmail.config.sass_cache_location
20
+ self.cache_dir = config.sass_cache_location
23
21
  end
24
22
 
25
23
  def compile
@@ -30,18 +28,18 @@ module BootstrapEmail
30
28
 
31
29
  private
32
30
 
33
- def load_config
34
- path = BootstrapEmail.config.sass_location_for(type: type)
35
- replace_config(File.read(path)) if path
31
+ def load_sass_config
32
+ sass_string = config.sass_string_for(type: type)
33
+ replace_config(sass_string) if sass_string
36
34
  end
37
35
 
38
- def replace_config(config_file)
39
- config_file.gsub("//= @import #{type};", "@import '#{file_path}';")
36
+ def replace_config(sass_config)
37
+ sass_config.gsub("//= @import #{type};", "@import '#{file_path}';")
40
38
  end
41
39
 
42
40
  def checksum_files
43
- checksums = config_file.nil? ? [] : [Digest::SHA1.hexdigest(config_file)]
44
- BootstrapEmail.config.sass_load_paths.each do |load_path|
41
+ checksums = sass_config.nil? ? [] : [Digest::SHA1.hexdigest(sass_config)]
42
+ config.sass_load_paths.each do |load_path|
45
43
  Dir.glob(File.join(load_path, '**', '*.scss'), base: __dir__).each do |path|
46
44
  checksums << Digest::SHA1.file(File.expand_path(path, __dir__)).hexdigest
47
45
  end
@@ -55,11 +53,11 @@ module BootstrapEmail
55
53
  end
56
54
 
57
55
  def compile_and_cache_scss(cache_path)
58
- file = config_file || File.read("#{file_path}.scss")
56
+ file = sass_config || File.read("#{file_path}.scss")
59
57
  css = SassC::Engine.new(file, style: style).render
60
58
  FileUtils.mkdir_p("#{cache_dir}/#{checksum}") unless File.directory?("#{cache_dir}/#{checksum}")
61
59
  File.write(cache_path, css)
62
- puts "New css file cached for #{type}" if BootstrapEmail.config.sass_log_enabled?
60
+ puts "New css file cached for #{type}" if config.sass_log_enabled?
63
61
  end
64
62
  end
65
63
  end
@@ -2,28 +2,20 @@
2
2
 
3
3
  module BootstrapEmail
4
4
  class << self
5
- def config
6
- @config ||= BootstrapEmail::Config.new
7
- @config
8
- end
9
-
10
- def load_options(options)
11
- @config ||= BootstrapEmail::Config.new
12
- @config.load_options(options)
13
- @config
5
+ def static_config
6
+ @static_config ||= BootstrapEmail::ConfigStore.new
14
7
  end
15
8
 
16
9
  def configure
17
- @config ||= BootstrapEmail::Config.new
18
- yield @config
10
+ yield static_config
19
11
  end
20
12
 
21
13
  def reset_config!
22
- remove_instance_variable :@config if defined?(@config)
14
+ remove_instance_variable :@static_config if defined?(@static_config)
23
15
  end
24
16
 
25
17
  def clear_sass_cache!
26
- FileUtils.rm_rf(BootstrapEmail.config.sass_cache_location)
18
+ FileUtils.rm_rf(BootstrapEmail::Config.new.sass_cache_location)
27
19
  end
28
20
  end
29
21
  end
@@ -16,6 +16,7 @@ rescue LoadError; end
16
16
 
17
17
  require 'action_mailer' if defined?(Rails)
18
18
 
19
+ require_relative 'bootstrap-email/config_store'
19
20
  require_relative 'bootstrap-email/config'
20
21
  require_relative 'bootstrap-email/setup'
21
22
  require_relative 'bootstrap-email/erb'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bootstrap-email
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.7
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stuart Yamartino
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-17 00:00:00.000000000 Z
11
+ date: 2022-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: htmlbeautifier
@@ -119,6 +119,7 @@ files:
119
119
  - lib/bootstrap-email/bootstrap_email_cli.rb
120
120
  - lib/bootstrap-email/compiler.rb
121
121
  - lib/bootstrap-email/config.rb
122
+ - lib/bootstrap-email/config_store.rb
122
123
  - lib/bootstrap-email/converters/add_missing_meta_tags.rb
123
124
  - lib/bootstrap-email/converters/alert.rb
124
125
  - lib/bootstrap-email/converters/align.rb