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



28
29
30
31
# File 'lib/inky/component_factory.rb', line 28

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



24
25
26
# File 'lib/inky/component_factory.rb', line 24

def _combine_classes(elem, extra_classes)
  [elem['class'], extra_classes].join(' ')
end

#_has_class(elem, klass) ⇒ Object



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

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

#_pass_through_attributes(elem) ⇒ Object



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

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



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

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

#_transform_block_grid(component, inner) ⇒ Object



93
94
95
96
# File 'lib/inky/component_factory.rb', line 93

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



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/inky/component_factory.rb', line 37

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



112
113
114
115
116
# File 'lib/inky/component_factory.rb', line 112

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



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/inky/component_factory.rb', line 98

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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/inky/component_factory.rb', line 73

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



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

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



51
52
53
54
# File 'lib/inky/component_factory.rb', line 51

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



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

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



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

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

#_transform_spacer(component, _inner) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/inky/component_factory.rb', line 118

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



134
135
136
137
# File 'lib/inky/component_factory.rb', line 134

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
# 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