class DocTemplate::Tags::ColumnsTag
Constants
- ALIGNMENT_RE
- SPLIT_SYMBOL
- TAG_NAME
- TEMPLATE
Public Instance Methods
parse(node, opts = {})
click to toggle source
# File lib/doc_template/tags/columns_tag.rb, line 13 def parse(node, opts = {}) @opts = opts @images = [] @tags = [] data = fetch_content node rows = substitute_images(data).map do |row| row.map do |td| td = substitute_tags td handle_alignment_for td end end @content = parse_template({ rows: rows }, TEMPLATE) if rows.any? replace_tag node self end
Private Instance Methods
fetch_content(node)
click to toggle source
Going down the DOM tree until the end tag. Placing own markers for nested tags and images to revert them back later on
# File lib/doc_template/tags/columns_tag.rb, line 50 def fetch_content(node) nodes = block_nodes(node) do |n| fetch_images n add_tags_from n end nodes.map(&:remove) # Handles alignment. # Content team places ` r;` to make right alignment for the next column # Just swap position of the ` r` and `;` to simplify the alogorithm data = nodes .map { |n| n.content.squish } .join .gsub(' r;', ';align-right ') data .split(SPLIT_SYMBOL) .map(&:strip) .reject(&:blank?) .in_groups_of(@opts[:value].to_i, '') end
fetch_images(node)
click to toggle source
# File lib/doc_template/tags/columns_tag.rb, line 73 def fetch_images(node) node.xpath('.//img').each do |img| @images << { src: img['src'], style: img['style'] } img.replace "{image: #{@images.size - 1}}" end node end
handle_alignment_for(td)
click to toggle source
# File lib/doc_template/tags/columns_tag.rb, line 84 def handle_alignment_for(td) {}.tap do |result| result[:content] = td.sub ALIGNMENT_RE, '' result[:css_class] = 'text-right' if td =~ ALIGNMENT_RE end end
substitute_images(data)
click to toggle source
# File lib/doc_template/tags/columns_tag.rb, line 91 def substitute_images(data) re = /{image: (\d)+}/ data.map do |row| row.map do |td| if (m = re.match td) && (img = @images[m[1].to_i]).present? td = td.sub re, %(<img src="#{img[:src]}" style="#{img[:style]}" />) end td end end end