module Inkcite::Renderer

Constants

COLON
EQUAL
SEMI_COLON
SLASH
SPACE

Public Class Methods

fix_illegal_characters(value, context) click to toggle source
# File lib/inkcite/renderer.rb, line 46
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

  # Remove unicode line break characters
  value.gsub!(/\u2028/, '')

  # Nuclear option to remove invisible unicode characters injected
  # when pasting text from MS Word or Google Docs.
  value.gsub!(/[^\P{C}\n]+/u, '')

  value
end
hex(color) click to toggle source
# File lib/inkcite/renderer.rb, line 83
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) click to toggle source

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.

# File lib/inkcite/renderer.rb, line 100
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) click to toggle source

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.

# File lib/inkcite/renderer.rb, line 124
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) click to toggle source
# File lib/inkcite/renderer.rb, line 136
def self.quote val
  %Q("#{val}")
end
render(str, context) click to toggle source
# File lib/inkcite/renderer.rb, line 140
def self.render str, context

  Parser.each(str) do |tag|

    # Record to the context the most recent tag being processed in case
    # there are errors associated with it.
    context.last_rendered_markup = 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) click to toggle source
# File lib/inkcite/renderer.rb, line 167
def self.render_styles styles
  join_hash(styles, COLON, SEMI_COLON)
end

Private Class Methods

default_renderer() click to toggle source
# File lib/inkcite/renderer.rb, line 179
def self.default_renderer
  @default_renderer ||= Property.new
end
renderers() click to toggle source
# File lib/inkcite/renderer.rb, line 183
def self.renderers

  # Dynamic renderers for custom behavior and tags.
  @renderers ||= {
      :'++'               => Increment.new,
      :a                  => Link.new,
      :background         => Background.new,
      :button             => Button.new,
      :carousel           => Carousel.new,
      :'carousel-img'     => Carousel::Image.new,
      :div                => Div.new,
      :facebook           => Social::Facebook.new,
      :fireworks          => Fireworks.new,
      :footnote           => Footnote.new,
      :footnotes          => Footnotes.new,
      :google             => GoogleAnalytics.new,
      :img                => Image.new,
      :'in-browser'       => InBrowser.new,
      :include            => Partial.new,
      :instagram          => Social::Instagram.new,
      :li                 => List.new,
      :like               => Like.new,
      :litmus             => LitmusAnalytics.new,
      :lorem              => Lorem.new,
      :'mobile-img'       => MobileImage.new,
      :'mobile-only'      => MobileOnly.new,
      :'mobile-style'     => MobileStyle.new,
      :'mobile-toggle-on' => MobileToggleOn.new,
      :ol                 => List.new,
      :pintrest           => Social::Pintrest.new,
      :preheader          => Preheader.new,
      :r                  => Trademark.new('&reg;'),
      :redacted           => Redacted.new,
      :slant              => Slant.new,
      :snow               => Snow.new,
      :span               => Span.new,
      :sparkle            => Sparkle.new,
      :sup                => Sup.new,
      :table              => Table.new,
      :td                 => Td.new,
      :tm                 => Trademark.new('&trade;'),
      :'topic'            => Topic.new,
      :'topic-list'       => TopicList.new,
      :twitter            => Social::Twitter.new,
      :ul                 => List.new,
      :'video-preview'    => VideoPreview.new
  }

end