class ChannelResearchStationery

Public Class Methods

new(content, options = {}) click to toggle source
# File lib/channel_research_stationery.rb, line 7
def initialize(content, options = {})
  @content = content
  @primary_color = options.delete(:primary_color) || '#6d6e71'
  @options = default_options.merge(options)
end

Public Instance Methods

content_css_string() click to toggle source
# File lib/channel_research_stationery.rb, line 73
def content_css_string
  reset_css + content_styles_css + @options[:extra_css]
end
content_styles_css() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/channel_research_stationery.rb, line 105
def default_options
  {
    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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/channel_research_stationery.rb, line 82
def sass_options
  @options.select {|k,v| [:background_color,:header_background_color,:width,:cta_background_color,:cta_color,:bold_color,:link_color].include?(k)}
end
sass_variables() click to toggle source
# File lib/channel_research_stationery.rb, line 86
def sass_variables
  sass_options.map {|k,v| "$#{k}: #{v};"}.join("\n")
end
template() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/channel_research_stationery.rb, line 69
def template_css_string
  reset_css + template_styles_css
end
template_styles_css() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/channel_research_stationery.rb, line 21
def to_html
  template_and_content
end
to_text() click to toggle source
# File lib/channel_research_stationery.rb, line 13
def to_text
  Premailer.new(to_html,
    {
      warn_level: Premailer::Warnings::SAFE
    }
  ).to_plain_text
end