Module: Inky::ComponentFactory

Included in:
Core
Defined in:
lib/inky/component_factory.rb

Constant Summary collapse

IGNORED_ON_PASSTHROUGH =
tags.freeze
INTERIM_TH_TAG =

These constants are used to circumvent an issue with JRuby Nokogiri. For more details see github.com/zurb/inky-rb/pull/94

'inky-interim-th'.freeze
INTERIM_TH_TAG_REGEX =
%r{(?<=\<|\<\/)#{Regexp.escape(INTERIM_TH_TAG)}}

Instance Method Summary collapse

Instance Method Details

#_combine_attributes(elem, extra_classes = nil) ⇒ Object



37
38
39
40
# File 'lib/inky/component_factory.rb', line 37

def _combine_attributes(elem, extra_classes = nil)
  classes = _combine_classes(elem, extra_classes)
  [_pass_through_attributes(elem), classes && %{class="#{classes}"}].join
end

#_combine_classes(elem, extra_classes) ⇒ Object



30
31
32
33
34
35
# File 'lib/inky/component_factory.rb', line 30

def _combine_classes(elem, extra_classes)
  existing = elem['class'].to_s.split(' ')
  to_add = extra_classes.to_s.split(' ')

  (existing + to_add).uniq.join(' ')
end

#_has_class(elem, klass) ⇒ Object



26
27
28
# File 'lib/inky/component_factory.rb', line 26

def _has_class(elem, klass)
  elem.attr('class') =~ /(^|\s)#{klass}($|\s)/
end

#_pass_through_attributes(elem) ⇒ Object



20
21
22
23
24
# File 'lib/inky/component_factory.rb', line 20

def _pass_through_attributes(elem)
  elem.attributes.reject { |e| IGNORED_ON_PASSTHROUGH.include?(e.downcase) }.map do |name, value|
    %{#{name}="#{value}" }
  end.join
end

#_target_attribute(elem) ⇒ Object



42
43
44
# File 'lib/inky/component_factory.rb', line 42

def _target_attribute(elem)
  elem.attributes['target'] ? %{ target="#{elem.attributes['target']}"} : ''
end

#_transform_block_grid(component, inner) ⇒ Object



102
103
104
105
# File 'lib/inky/component_factory.rb', line 102

def _transform_block_grid(component, inner)
  classes = _combine_classes(component, "block-grid up-#{component.attr('up')}")
  %{<table class="#{classes}"><tr>#{inner}</tr></table>}
end

#_transform_button(component, inner) ⇒ Object



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

def _transform_button(component, inner)
  expand = _has_class(component, 'expand')
  if component.attr('href')
    target = _target_attribute(component)
    extra = ' align="center" class="float-center"' if expand
    inner = %{<a href="#{component.attr('href')}"#{target}#{extra}>#{inner}</a>}
  end
  inner = "<center>#{inner}</center>" if expand

  classes = _combine_classes(component, 'button')
  expander = '<td class="expander"></td>' if expand
  %{<table class="#{classes}"><tr><td><table><tr><td>#{inner}</td></tr></table></td>#{expander}</tr></table>}
end

#_transform_callout(component, inner) ⇒ Object



121
122
123
124
125
# File 'lib/inky/component_factory.rb', line 121

def _transform_callout(component, inner)
  classes = _combine_classes(component, 'callout-inner')
  attributes = _pass_through_attributes(component)
  %{<table #{attributes}class="callout"><tr><th class="#{classes}">#{inner}</th><th class="expander"></th></tr></table>}
end

#_transform_center(component, _inner) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/inky/component_factory.rb', line 107

def _transform_center(component, _inner)
  # NOTE:  Using children instead of elements because elements.to_a
  # sometimes appears to miss elements that show up in size
  component.elements.each do |child|
    child['align'] = 'center'
    child['class'] = _combine_classes(child, 'float-center')
    items = component.elements.css(".menu-item").to_a.concat(component.elements.css("item").to_a)
    items.each do |item|
      item['class'] = _combine_classes(item, 'float-center')
    end
  end
  component.to_s
end

#_transform_columns(component, inner) ⇒ Object

in inky.js this is factored out into makeClumn. TBD if we need that here.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/inky/component_factory.rb', line 82

def _transform_columns(component, inner)
  col_count = component.parent.elements.size

  small_val = component.attr('small')
  large_val = component.attr('large')

  small_size = small_val || column_count
  large_size = large_val || small_val || (column_count / col_count).to_i

  classes = _combine_classes(component, "small-#{small_size} large-#{large_size} columns")

  classes << ' first' unless component.previous_element
  classes << ' last' unless component.next_element

  subrows = component.elements.css(".row").to_a.concat(component.elements.css("row").to_a)
  expander = %{<th class="expander"></th>} if large_size.to_i == column_count && subrows.empty?

  %{<#{INTERIM_TH_TAG} class="#{classes}" #{_pass_through_attributes(component)}><table><tr><th>#{inner}</th>#{expander}</tr></table></#{INTERIM_TH_TAG}>}
end

#_transform_container(component, inner) ⇒ Object



71
72
73
74
# File 'lib/inky/component_factory.rb', line 71

def _transform_container(component, inner)
  attributes = _combine_attributes(component, 'container')
  %{<table #{attributes} align="center"><tbody><tr><td>#{inner}</td></tr></tbody></table>}
end

#_transform_menu(component, inner) ⇒ Object



60
61
62
63
# File 'lib/inky/component_factory.rb', line 60

def _transform_menu(component, inner)
  attributes = _combine_attributes(component, 'menu')
  %{<table #{attributes}><tr><td><table><tr>#{inner}</tr></table></td></tr></table>}
end

#_transform_menu_item(component, inner) ⇒ Object



65
66
67
68
69
# File 'lib/inky/component_factory.rb', line 65

def _transform_menu_item(component, inner)
  target = _target_attribute(component)
  attributes = _combine_attributes(component, 'menu-item')
  %{<#{INTERIM_TH_TAG} #{attributes}><a href="#{component.attr('href')}"#{target}>#{inner}</a></#{INTERIM_TH_TAG}>}
end

#_transform_row(component, inner) ⇒ Object



76
77
78
79
# File 'lib/inky/component_factory.rb', line 76

def _transform_row(component, inner)
  attributes = _combine_attributes(component, 'row')
  %{<table #{attributes}><tbody><tr>#{inner}</tr></tbody></table>}
end

#_transform_spacer(component, _inner) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/inky/component_factory.rb', line 127

def _transform_spacer(component, _inner)
  classes = _combine_classes(component, 'spacer')
  build_table = ->(size, extra) { %{<table class="#{classes} #{extra}"><tbody><tr><td height="#{size}" style="font-size:#{size}px;line-height:#{size}px;">&#xA0;</td></tr></tbody></table>} }
  size = component.attr('size')
  size_sm = component.attr('size-sm')
  size_lg = component.attr('size-lg')
  if size_sm || size_lg
    html = ''
    html << build_table[size_sm, 'hide-for-large'] if size_sm
    html << build_table[size_lg, 'show-for-large'] if size_lg
    html
  else
    build_table[size || 16, nil]
  end
end

#_transform_wrapper(component, inner) ⇒ Object



143
144
145
146
# File 'lib/inky/component_factory.rb', line 143

def _transform_wrapper(component, inner)
  attributes = _combine_attributes(component, 'wrapper')
  %{<table #{attributes} align="center"><tr><td class="wrapper-inner">#{inner}</td></tr></table>}
end

#component_factory(elem) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/inky/component_factory.rb', line 3

def component_factory(elem)
  transform_method = :"_transform_#{component_lookup[elem.name]}"
  return unless respond_to?(transform_method)

  inner = elem.children.map(&:to_s).join
  send(transform_method, elem, inner)
end