Class: Inky::Core

Inherits:
Object
  • Object
show all
Includes:
ComponentFactory
Defined in:
lib/inky.rb

Constant Summary

Constants included from ComponentFactory

ComponentFactory::IGNORED_ON_PASSTHROUGH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ComponentFactory

#_combine_attributes, #_combine_classes, #_has_class, #_pass_through_attributes, #_target_attribute, #_transform_block_grid, #_transform_button, #_transform_callout, #_transform_center, #_transform_columns, #_transform_container, #_transform_menu, #_transform_menu_item, #_transform_row, #_transform_spacer, #_transform_wrapper, #component_factory

Constructor Details

#initialize(options = {}) ⇒ Core

Returns a new instance of Core.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/inky.rb', line 8

def initialize(options = {})
  self.components = {
    button: 'button',
    row: 'row',
    columns: 'columns',
    container: 'container',
    inky: 'inky',
    block_grid: 'block-grid',
    menu: 'menu',
    center: 'center',
    callout: 'callout',
    spacer: 'spacer',
    wrapper: 'wrapper',
    menu_item: 'item'
  }.merge(options[:components] || {})

  self.component_lookup = components.invert

  self.column_count = options[:column_count] || 12

  self.component_tags = components.values
end

Instance Attribute Details

#column_countObject

Returns the value of attribute column_count.



5
6
7
# File 'lib/inky.rb', line 5

def column_count
  @column_count
end

#component_lookupObject

Returns the value of attribute component_lookup.



5
6
7
# File 'lib/inky.rb', line 5

def component_lookup
  @component_lookup
end

#component_tagsObject

Returns the value of attribute component_tags.



5
6
7
# File 'lib/inky.rb', line 5

def component_tags
  @component_tags
end

#componentsObject

Returns the value of attribute components.



5
6
7
# File 'lib/inky.rb', line 5

def components
  @components
end

Class Method Details

.extract_raws(string) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/inky.rb', line 54

def self.extract_raws(string)
  raws = []
  i = 0
  regex = %r(< *raw *>(.*?)</ *raw *>)
  str = string
  while raw = str.match(regex)
    raws[i] = raw[1]
    str = str.sub(regex, "###RAW#{i}###")
    i += 1
  end
  [raws, str]
end

.re_inject_raws(string, raws) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/inky.rb', line 67

def self.re_inject_raws(string, raws)
  str = string
  raws.each_with_index do |val, i|
    str = str.sub("###RAW#{i}###", val)
  end
  # If we're in rails, these should be considered safe strings
  str = str.html_safe if str.respond_to?(:html_safe)
  str
end

Instance Method Details

#release_the_kraken(xml_string) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/inky.rb', line 31

def release_the_kraken(xml_string)
  xml_string = xml_string.gsub(/doctype/i, 'DOCTYPE')
  raws, str = Inky::Core.extract_raws(xml_string)
  parse_cmd = str =~ /<html/i ? :parse : :fragment
  html = Nokogiri::HTML.public_send(parse_cmd, str)
  html.elements.each do |elem|
    transform_doc(elem)
  end
  string = html.to_html(encoding: 'US-ASCII')
  Inky::Core.re_inject_raws(string, raws)
end

#transform_doc(elem) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/inky.rb', line 43

def transform_doc(elem)
  if elem.respond_to?(:children)
    elem.children.each do |child|
      transform_doc(child)
    end
    component = component_factory(elem)
    elem.replace(component) if component
  end
  elem
end