Class: Inky::Core

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

Constant Summary

Constants included from ComponentFactory

Inky::ComponentFactory::IGNORED_ON_PASSTHROUGH, Inky::ComponentFactory::INTERIM_TH_TAG, Inky::ComponentFactory::INTERIM_TH_TAG_REGEX

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.



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

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] || ::Inky.configuration.column_count

  self.component_tags = components.values
end

Instance Attribute Details

#column_countObject

Returns the value of attribute column_count.



7
8
9
# File 'lib/inky.rb', line 7

def column_count
  @column_count
end

#component_lookupObject

Returns the value of attribute component_lookup.



7
8
9
# File 'lib/inky.rb', line 7

def component_lookup
  @component_lookup
end

#component_tagsObject

Returns the value of attribute component_tags.



7
8
9
# File 'lib/inky.rb', line 7

def component_tags
  @component_tags
end

#componentsObject

Returns the value of attribute components.



7
8
9
# File 'lib/inky.rb', line 7

def components
  @components
end

Class Method Details

.extract_raws(string) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/inky.rb', line 58

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



71
72
73
74
75
76
77
78
79
# File 'lib/inky.rb', line 71

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(html_string) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/inky.rb', line 33

def release_the_kraken(html_string)
  if html_string.encoding.name == "ASCII-8BIT"
    html_string.force_encoding('utf-8') # transform_doc barfs if encoding is ASCII-8bit
  end
  html_string = html_string.gsub(/doctype/i, 'DOCTYPE')
  raws, str = Inky::Core.extract_raws(html_string)
  parse_cmd = str =~ /<html/i ? :parse : :fragment
  html = Nokogiri::HTML.public_send(parse_cmd, str)
  transform_doc(html)
  string = html.to_html
  string.gsub!(INTERIM_TH_TAG_REGEX, 'th')
  Inky::Core.re_inject_raws(string, raws)
end

#transform_doc(elem) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/inky.rb', line 47

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