Class: ChannelResearchStationery
- Inherits:
-
Object
- Object
- ChannelResearchStationery
- Defined in:
- lib/channel_research_stationery.rb
Instance Method Summary collapse
- #content_css_string ⇒ Object
- #content_styles_css ⇒ Object
- #content_with_inline_css ⇒ Object
- #default_options ⇒ Object
-
#initialize(content, options = {}) ⇒ ChannelResearchStationery
constructor
A new instance of ChannelResearchStationery.
- #placeholder_content ⇒ Object
- #reset_css ⇒ Object
- #sass_options ⇒ Object
- #sass_variables ⇒ Object
- #template ⇒ Object
- #template_and_content ⇒ Object
- #template_css_string ⇒ Object
- #template_styles_css ⇒ Object
- #template_with_inline_css ⇒ Object
- #to_html ⇒ Object
- #to_text ⇒ Object
Constructor Details
#initialize(content, options = {}) ⇒ ChannelResearchStationery
Returns a new instance of ChannelResearchStationery.
7 8 9 10 11 |
# File 'lib/channel_research_stationery.rb', line 7 def initialize(content, = {}) @content = content @primary_color = .delete(:primary_color) || '#6d6e71' @options = .merge() end |
Instance Method Details
#content_css_string ⇒ Object
73 74 75 |
# File 'lib/channel_research_stationery.rb', line 73 def content_css_string reset_css + content_styles_css + @options[:extra_css] end |
#content_styles_css ⇒ Object
97 98 99 100 101 102 |
# File 'lib/channel_research_stationery.rb', line 97 def content_styles_css css_path = File.join( __dir__, "./generators/templates/content_styles.sass" ) sass_styles = File.open(css_path, "rb").read sass = sass_variables + "\n" + sass_styles SassC::Engine.new(sass, style: :compressed).render end |
#content_with_inline_css ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/channel_research_stationery.rb', line 47 def content_with_inline_css Premailer.new(@content, { warn_level: Premailer::Warnings::SAFE, css_string: content_css_string, with_html_string: true } ).to_inline_css end |
#default_options ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/channel_research_stationery.rb', line 105 def { header_background_color: @primary_color, link_color: @primary_color, bold_color: @primary_color, cta_background_color: @primary_color, background_color: '#ffffff', cta_color: '#ffffff', width: '700px', badge_left: false, badge_right: false, title: false, subtitle: false, extra_css: '' } end |
#placeholder_content ⇒ Object
57 58 59 60 |
# File 'lib/channel_research_stationery.rb', line 57 def placeholder_content contents = File.open("lib/generators/templates/placeholder_contents/#{@options['placeholder']}").read Haml::Engine.new(contents).render end |
#reset_css ⇒ Object
77 78 79 80 |
# File 'lib/channel_research_stationery.rb', line 77 def reset_css css_path = File.join( __dir__, "./generators/templates/reset.css" ) File.open(css_path).read end |
#sass_options ⇒ Object
82 83 84 |
# File 'lib/channel_research_stationery.rb', line 82 def @options.select {|k,v| [:background_color,:header_background_color,:width,:cta_background_color,:cta_color,:bold_color,:link_color].include?(k)} end |
#sass_variables ⇒ Object
86 87 88 |
# File 'lib/channel_research_stationery.rb', line 86 def sass_variables .map {|k,v| "$#{k}: #{v};"}.join("\n") end |
#template ⇒ Object
62 63 64 65 66 67 |
# File 'lib/channel_research_stationery.rb', line 62 def template template_path = File.join( __dir__, "./generators/templates/template.html.haml" ) contents = File.open(template_path).read engine = Haml::Engine.new(contents) engine.render(Object.new, { options: @options }) end |
#template_and_content ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/channel_research_stationery.rb', line 25 def template_and_content template = Nokogiri::HTML(template_with_inline_css) content = Nokogiri::HTML(content_with_inline_css) template.at('style').remove template.at('div#content') << content.at('body') # Add content-editable bits template.xpath('//*[@class="contenteditable"]').each do |node| node['contenteditable'] = 'true' end return template end |
#template_css_string ⇒ Object
69 70 71 |
# File 'lib/channel_research_stationery.rb', line 69 def template_css_string reset_css + template_styles_css end |
#template_styles_css ⇒ Object
90 91 92 93 94 95 |
# File 'lib/channel_research_stationery.rb', line 90 def template_styles_css css_path = File.join( __dir__, "./generators/templates/template_styles.sass" ) sass_styles = File.open(css_path, "rb").read sass = sass_variables + "\n" + sass_styles SassC::Engine.new(sass, style: :compressed).render end |
#template_with_inline_css ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/channel_research_stationery.rb', line 37 def template_with_inline_css Premailer.new(template, { warn_level: Premailer::Warnings::SAFE, css_string: template_css_string, with_html_string: true } ).to_inline_css end |
#to_html ⇒ Object
21 22 23 |
# File 'lib/channel_research_stationery.rb', line 21 def to_html template_and_content end |
#to_text ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/channel_research_stationery.rb', line 13 def to_text Premailer.new(to_html, { warn_level: Premailer::Warnings::SAFE } ).to_plain_text end |