Module: Inkcite::Renderer

Defined in:
lib/inkcite/renderer.rb,
lib/inkcite/renderer/td.rb,
lib/inkcite/renderer/div.rb,
lib/inkcite/renderer/base.rb,
lib/inkcite/renderer/like.rb,
lib/inkcite/renderer/link.rb,
lib/inkcite/renderer/snow.rb,
lib/inkcite/renderer/span.rb,
lib/inkcite/renderer/image.rb,
lib/inkcite/renderer/lorem.rb,
lib/inkcite/renderer/style.rb,
lib/inkcite/renderer/table.rb,
lib/inkcite/renderer/button.rb,
lib/inkcite/renderer/social.rb,
lib/inkcite/renderer/element.rb,
lib/inkcite/renderer/partial.rb,
lib/inkcite/renderer/sparkle.rb,
lib/inkcite/renderer/footnote.rb,
lib/inkcite/renderer/property.rb,
lib/inkcite/renderer/redacted.rb,
lib/inkcite/renderer/increment.rb,
lib/inkcite/renderer/preheader.rb,
lib/inkcite/renderer/background.rb,
lib/inkcite/renderer/image_base.rb,
lib/inkcite/renderer/in_browser.rb,
lib/inkcite/renderer/responsive.rb,
lib/inkcite/renderer/table_base.rb,
lib/inkcite/renderer/mobile_only.rb,
lib/inkcite/renderer/mobile_image.rb,
lib/inkcite/renderer/mobile_style.rb,
lib/inkcite/renderer/mobile_toggle.rb,
lib/inkcite/renderer/video_preview.rb,
lib/inkcite/renderer/container_base.rb,
lib/inkcite/renderer/special_effect.rb,
lib/inkcite/renderer/google_analytics.rb,
lib/inkcite/renderer/litmus_analytics.rb

Defined Under Namespace

Classes: Background, Base, Button, ContainerBase, Div, Element, Footnote, Footnotes, GoogleAnalytics, Image, ImageBase, InBrowser, Increment, Like, Link, LitmusAnalytics, Lorem, MobileImage, MobileOnly, MobileStyle, MobileToggleOn, Partial, Preheader, Property, Redacted, Responsive, Snow, Social, Span, Sparkle, SpecialEffect, Style, Table, TableBase, Td, VideoPreview

Class Method Summary collapse

Class Method Details

.fix_illegal_characters(value, context) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/inkcite/renderer.rb', line 41

def self.fix_illegal_characters value, context

  # These special characters cause rendering problems in a variety
  # of email clients.  Convert them to the correct unicode characters.
  # https://www.campaignmonitor.com/blog/post/1810/why-are-all-my-apostrophes-mis

  if context.text?
    value.gsub!(/[–—]/, '-')
    value.gsub!(/™/, '(tm)')
    value.gsub!(/®/, '(r)')
    value.gsub!(/[‘’`]/, "'")
    value.gsub!(/[“”]/, '"')
    value.gsub!(/…/, '...')

  else
    value.gsub!(/–/, '–')
    value.gsub!(/—/, '—')
    value.gsub!(/™/, '™')
    value.gsub!(/®/, '®')
    value.gsub!(/[‘’`]/, '’')
    value.gsub!(/“/, '“')
    value.gsub!(/”/, '”')
    value.gsub!(/é/, 'é')
    value.gsub!(/…/, '…')

  end

  value
end

.hex(color) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/inkcite/renderer.rb', line 71

def self.hex color

  # Convert #rgb into #rrggbb
  if !color.blank? && color.length == 4 && color.start_with?('#')
    red = color[1]
    green = color[2]
    blue = color[3]
    color = "##{red}#{red}#{green}#{green}#{blue}#{blue}"
  end

  color
end

.join_hash(hash, equal = EQUAL, sep = SPACE) ⇒ Object

Joins the key-value-pairs of the provided hash into a readable string destined for HTML or CSS style declarations. For example, { :bgcolor => ‘“#fff”’ } would become bgcolor=“#fff” using the default equality and space delimiters.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/inkcite/renderer.rb', line 88

def self.join_hash hash, equal=EQUAL, sep=SPACE

  pairs = []

  hash.keys.sort.each do |att|
    val = hash[att]
    next if val.blank?

    # First add the attribute name - e.g. "padding" or "bgcolor"
    pair = "#{att}"

    # Only append the value if the attribute value is a non-boolean.
    # e.g. support boolean attributes via booleans ":nowrap => true"
    pair << "#{equal}#{val}" unless val == true

    pairs << pair
  end

  pairs.join(sep)
end

.px(val) ⇒ Object

Applies a “px” extension to unlabeled integer values. If a labeled value is detected (e.g. 2em) or a non-integer value is provided (e.g. “normal”) then the value is returned directly.



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/inkcite/renderer.rb', line 112

def self.px val

  # Quick abort if a non-integer value has been provided.  This catches
  # cases like 3em and normal.  When provided, the value is not converted
  # to pixels and instead is returned directly.
  return val if val && val.to_i.to_s != val.to_s

  val = val.to_i
  val = "#{val}px" unless val == 0
  val
end

.quote(val) ⇒ Object



124
125
126
# File 'lib/inkcite/renderer.rb', line 124

def self.quote val
  %Q("#{val}")
end

.render(str, context) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/inkcite/renderer.rb', line 128

def self.render str, context

  Parser.each(str) do |tag|

    # Split the string into the tag and it's attributes.
    name, opts = tag.split(SPACE, 2)

    # Convert the options string (e.g. color=#ff9900 border=none) into parameters.
    opts = Parser.parameters(opts)

    # Strip off the leading slash (/) if there is one.  Renderers are
    open_tag = (name.starts_with?(SLASH) ? name[1..-1] : name).to_sym

    # Choose a renderer either from the dynamic set or use the default one that
    # simply renders from the property values.
    renderer = renderers[open_tag] || default_renderer

    renderer.render name, opts, context

  end

end

.render_styles(styles) ⇒ Object



151
152
153
# File 'lib/inkcite/renderer.rb', line 151

def self.render_styles styles
  join_hash(styles, COLON, SEMI_COLON)
end