Module: Inky::ComponentFactory

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

Constant Summary collapse

IGNORED_ON_PASSTHROUGH =
tags.freeze

Instance Method Summary collapse

Instance Method Details

#_combine_attributes(elem, extra_classes = nil) ⇒ Object



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

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



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

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



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

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

#_pass_through_attributes(elem) ⇒ Object



15
16
17
18
19
# File 'lib/inky/component_factory.rb', line 15

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



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

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

#_transform_block_grid(component, inner) ⇒ Object



97
98
99
100
# File 'lib/inky/component_factory.rb', line 97

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



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

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



116
117
118
119
120
# File 'lib/inky/component_factory.rb', line 116

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



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/inky/component_factory.rb', line 102

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.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/inky/component_factory.rb', line 77

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?

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

#_transform_container(component, inner) ⇒ Object



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

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



55
56
57
58
# File 'lib/inky/component_factory.rb', line 55

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



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

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

#_transform_row(component, inner) ⇒ Object



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

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

#_transform_spacer(component, _inner) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/inky/component_factory.rb', line 122

def _transform_spacer(component, _inner)
  classes = _combine_classes(component, 'spacer')
  build_table = ->(size, extra) { %{<table class="#{classes} #{extra}"><tbody><tr><td height="#{size}px" 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



138
139
140
141
# File 'lib/inky/component_factory.rb', line 138

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