Class: LMDocstache::HideCustomTags

Inherits:
Object
  • Object
show all
Defined in:
lib/lm_docstache/hide_custom_tags.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document:, hide_custom_tags: {}) ⇒ HideCustomTags

The hide_custom_tags options is a Hash of Regexp or String keys representing the pattern you expect to keep at the document but replacing the content to use font color equal to document background color or white. For the Hash values we can have:

  • false -> In this case we don’t change the text content.

  • Proc -> When a Proc instance is provided, it’s expected it to be able to receive the matched string and to return the string that will be used as replacement.

  • any other value that will be turned into a string -> in this case, this will be the value that will replace the matched string



16
17
18
19
# File 'lib/lm_docstache/hide_custom_tags.rb', line 16

def initialize(document:, hide_custom_tags: {})
  @document = document
  @hide_custom_tags = hide_custom_tags
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



3
4
5
# File 'lib/lm_docstache/hide_custom_tags.rb', line 3

def document
  @document
end

#hide_custom_tagsObject (readonly)

Returns the value of attribute hide_custom_tags.



3
4
5
# File 'lib/lm_docstache/hide_custom_tags.rb', line 3

def hide_custom_tags
  @hide_custom_tags
end

Instance Method Details

#hide_custom_tags!Object

Find all run nodes matching hide custom tags Regexp’s options you defined, split it in multiple runs and replace font color to document background color or white in the matching tag run node. Replace content if you have defined any replacement value.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lm_docstache/hide_custom_tags.rb', line 24

def hide_custom_tags!
  hide_custom_tags.each do |full_pattern, value|
    paragraphs = document.css('w|p')
    while paragraph = paragraphs.shift do
      next unless paragraph.text =~ full_pattern
      run_nodes = paragraph.css('w|r')
      while run_node = run_nodes.shift
        next unless run_node.at_css('w|t')
        next unless run_node.text =~ full_pattern
        tag_contents = split_tag_content(run_node.text, full_pattern)
        replacement_nodes = []
        tag_contents[:content_list].each_with_index do |content, idx|
          remainder_run_node = run_node.clone
          replace_content(remainder_run_node, content)
          matched_tag = tag_contents[:matched_tags][idx]
          replacement_nodes << remainder_run_node
          if matched_tag
            run_node_with_match = run_node.clone
            replace_style(run_node_with_match)
            matched_content = matched_tag
            if value
              matched_content = value.is_a?(Proc) ?
                   value.call(matched_tag) :
                   value.to_s
            end
            replace_content(run_node_with_match, matched_content)
            replacement_nodes << run_node_with_match
          end
        end
        run_node.add_next_sibling(Nokogiri::XML::NodeSet.new(document, replacement_nodes))
        run_node.unlink
      end
    end
  end
end