Class: ChannelResearchStationery

Inherits:
Object
  • Object
show all
Defined in:
lib/channel_research_stationery.rb

Instance Method Summary collapse

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, options = {})
  @content = content
  @primary_color = options.delete(:primary_color) || '#6d6e71'
  @options = default_options.merge(options)
end

Instance Method Details

#content_css_stringObject



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_cssObject



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_cssObject



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_optionsObject



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 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_contentObject



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_cssObject



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_optionsObject



82
83
84
# 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_variablesObject



86
87
88
# File 'lib/channel_research_stationery.rb', line 86

def sass_variables
  sass_options.map {|k,v| "$#{k}: #{v};"}.join("\n")
end

#templateObject



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_contentObject



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_stringObject



69
70
71
# File 'lib/channel_research_stationery.rb', line 69

def template_css_string
  reset_css + template_styles_css
end

#template_styles_cssObject



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_cssObject



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_htmlObject



21
22
23
# File 'lib/channel_research_stationery.rb', line 21

def to_html
  template_and_content
end

#to_textObject



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