Module: Inky::ComponentFactory
- Included in:
- Core
- Defined in:
- lib/inky/component_factory.rb
Constant Summary collapse
- IGNORED_ON_PASSTHROUGH =
.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
- #_combine_attributes(elem, extra_classes = nil) ⇒ Object
- #_combine_classes(elem, extra_classes) ⇒ Object
- #_has_class(elem, klass) ⇒ Object
- #_pass_through_attributes(elem) ⇒ Object
- #_target_attribute(elem) ⇒ Object
- #_transform_block_grid(component, inner) ⇒ Object
- #_transform_button(component, inner) ⇒ Object
- #_transform_callout(component, inner) ⇒ Object
- #_transform_center(component, _inner) ⇒ Object
-
#_transform_columns(component, inner) ⇒ Object
in inky.js this is factored out into makeClumn.
- #_transform_container(component, inner) ⇒ Object
- #_transform_h_line(component, _inner) ⇒ Object
- #_transform_menu(component, inner) ⇒ Object
- #_transform_menu_item(component, inner) ⇒ Object
- #_transform_row(component, inner) ⇒ Object
- #_transform_spacer(component, _inner) ⇒ Object
- #_transform_wrapper(component, inner) ⇒ Object
- #component_factory(elem) ⇒ Object
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
103 104 105 106 |
# File 'lib/inky/component_factory.rb', line 103 def _transform_block_grid(component, inner) classes = _combine_classes(component, "block-grid up-#{component.attr('up')}") %{<table class="#{classes}"><tbody><tr>#{inner}</tr></tbody></table>} end |
#_transform_button(component, inner) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/inky/component_factory.rb', line 46 def (component, inner) = _has_class(component, 'expand') attributes = _pass_through_attributes(component) if component.attr('href') target = _target_attribute(component) extra = ' align="center" class="float-center"' if inner = %{<a #{attributes}href="#{component.attr('href')}"#{target}#{extra}>#{inner}</a>} end inner = "<center>#{inner}</center>" if classes = _combine_classes(component, 'button') = '<td class="expander"></td>' if %{<table class="#{classes}"><tbody><tr><td><table><tbody><tr><td>#{inner}</td></tr></tbody></table></td>#{}</tr></tbody></table>} end |
#_transform_callout(component, inner) ⇒ Object
122 123 124 125 126 |
# File 'lib/inky/component_factory.rb', line 122 def _transform_callout(component, inner) classes = _combine_classes(component, 'callout-inner') attributes = _pass_through_attributes(component) %{<table #{attributes}class="callout"><tbody><tr><th class="#{classes}">#{inner}</th><th class="expander"></th></tr></tbody></table>} end |
#_transform_center(component, _inner) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/inky/component_factory.rb', line 108 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.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/inky/component_factory.rb', line 83 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) = %{<th class="expander"></th>} if large_size.to_i == column_count && subrows.empty? %{<#{INTERIM_TH_TAG} class="#{classes}" #{_pass_through_attributes(component)}><table><tbody><tr><th>#{inner}</th>#{}</tr></tbody></table></#{INTERIM_TH_TAG}>} end |
#_transform_container(component, inner) ⇒ Object
72 73 74 75 |
# File 'lib/inky/component_factory.rb', line 72 def _transform_container(component, inner) attributes = _combine_attributes(component, 'container') %{<table #{attributes} align="center"><tbody><tr><td>#{inner}</td></tr></tbody></table>} end |
#_transform_h_line(component, _inner) ⇒ Object
144 145 146 147 148 |
# File 'lib/inky/component_factory.rb', line 144 def _transform_h_line(component, _inner) classes = _combine_classes(component, 'h-line') attributes = _pass_through_attributes(component) %{<table #{attributes}class="#{classes}"><tr><th> </th></tr></table>} end |
#_transform_menu(component, inner) ⇒ Object
61 62 63 64 |
# File 'lib/inky/component_factory.rb', line 61 def (component, inner) attributes = _combine_attributes(component, 'menu') %{<table #{attributes}><tbody><tr><td><table><tbody><tr>#{inner}</tr></tbody></table></td></tr></tbody></table>} end |
#_transform_menu_item(component, inner) ⇒ Object
66 67 68 69 70 |
# File 'lib/inky/component_factory.rb', line 66 def (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
77 78 79 80 |
# File 'lib/inky/component_factory.rb', line 77 def _transform_row(component, inner) attributes = _combine_attributes(component, 'row') %{<table #{attributes}><tbody><tr>#{inner}</tr></tbody></table>} end |
#_transform_spacer(component, _inner) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/inky/component_factory.rb', line 128 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;"> </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
150 151 152 153 |
# File 'lib/inky/component_factory.rb', line 150 def _transform_wrapper(component, inner) attributes = _combine_attributes(component, 'wrapper') %{<table #{attributes} align="center"><tbody><tr><td class="wrapper-inner">#{inner}</td></tr></tbody></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 |