Class: LMDocstache::Parser
- Inherits:
-
Object
- Object
- LMDocstache::Parser
- Defined in:
- lib/lm_docstache/parser.rb
Constant Summary collapse
- BLOCK_TYPE_PATTERN =
'(#|\^)\s*'- BLOCK_VARIABLE_PATTERN =
'([^\s~=]+)'- BLOCK_OPERATOR_PATTERN =
'\s*(~=|==)\s*'- BLOCK_VALUE_PATTERN =
'([^\}]+?)\s*'- BLOCK_START_PATTERN =
"{{#{BLOCK_TYPE_PATTERN}#{BLOCK_VARIABLE_PATTERN}"\ "#{BLOCK_OPERATOR_PATTERN}#{BLOCK_VALUE_PATTERN}}}"
- BLOCK_CONTENT_PATTERN =
'(.*?)'- BLOCK_CLOSE_PATTERN =
'{{/\s*\k<2>\s*}}'- BLOCK_NAMED_CLOSE_PATTERN =
'{{/\s*%{tag_name}\s*}}'- BLOCK_PATTERN =
"#{BLOCK_START_PATTERN}#{BLOCK_CONTENT_PATTERN}"\ "#{BLOCK_CLOSE_PATTERN}"
- BLOCK_START_MATCHER =
/#{BLOCK_START_PATTERN}/- BLOCK_CLOSE_MATCHER =
/{{\/\s*.+?\s*}}/- BLOCK_MATCHER =
/#{BLOCK_PATTERN}/- VARIABLE_MATCHER =
/{{([^#\^\/].*?)}}/
Instance Attribute Summary collapse
-
#blocks ⇒ Object
readonly
Returns the value of attribute blocks.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#data_sequential_replacement ⇒ Object
readonly
Returns the value of attribute data_sequential_replacement.
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#hide_custom_tags ⇒ Object
readonly
Returns the value of attribute hide_custom_tags.
-
#special_variable_replacements ⇒ Object
readonly
Returns the value of attribute special_variable_replacements.
Instance Method Summary collapse
-
#add_blocks_to_regexp(options) ⇒ Object
Replace
RegepxorStringkeys to have the enclosing blocks. -
#initialize(document, data, options = {}) ⇒ Parser
constructor
Constructor
dataargument is aHashwhere the key is expected to be aStringrepresenting the replacement block value. - #parse_and_update_document! ⇒ Object
Constructor Details
#initialize(document, data, options = {}) ⇒ Parser
Constructor data argument is a Hash where the key is
expected to be a String representing the replacement block value. Hash
key must not contain the {{}} part, but only the pattern characters.
As for the values of the Hash, we have options:
Stringwill be the value that will replace matching string.Array<String>will be an ordered sequence of values that will replace the matched string following document matching order.
Example: { 'full_name' => 'John Doe', 'text|req|Client' => ['John', 'Matt', 'Paul'] }
Constructor options argument is a Hash where keys can be:
The special_variable_replacements option is a Hash where the key is
expected to be either a Regexp or a String representing the pattern
of more specific type of variables that deserves a special treatment. The
key must not contain the {{}} part, but only the pattern characters
inside of it. As for the values of the Hash, it tells the replacement
algorithm what to do with the matched string and there are the options:
false-> in this case the matched variable will be kept without replacementProc-> when aProcinstance 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
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 aProcinstance 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
63 64 65 66 67 68 69 |
# File 'lib/lm_docstache/parser.rb', line 63 def initialize(document, data, = {}) @document = document @data = data.transform_keys(&:to_s).select {|e, v| !v.is_a?(Array) } @data_sequential_replacement = data.transform_keys(&:to_s).select {|e, v| v.is_a?(Array) } @special_variable_replacements = add_blocks_to_regexp(.fetch(:special_variable_replacements, {})) @hide_custom_tags = add_blocks_to_regexp(.fetch(:hide_custom_tags, {})) end |
Instance Attribute Details
#blocks ⇒ Object (readonly)
Returns the value of attribute blocks.
20 21 22 |
# File 'lib/lm_docstache/parser.rb', line 20 def blocks @blocks end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
20 21 22 |
# File 'lib/lm_docstache/parser.rb', line 20 def data @data end |
#data_sequential_replacement ⇒ Object (readonly)
Returns the value of attribute data_sequential_replacement.
21 22 23 |
# File 'lib/lm_docstache/parser.rb', line 21 def data_sequential_replacement @data_sequential_replacement end |
#document ⇒ Object (readonly)
Returns the value of attribute document.
20 21 22 |
# File 'lib/lm_docstache/parser.rb', line 20 def document @document end |
#hide_custom_tags ⇒ Object (readonly)
Returns the value of attribute hide_custom_tags.
20 21 22 |
# File 'lib/lm_docstache/parser.rb', line 20 def @hide_custom_tags end |
#special_variable_replacements ⇒ Object (readonly)
Returns the value of attribute special_variable_replacements.
20 21 22 |
# File 'lib/lm_docstache/parser.rb', line 20 def special_variable_replacements @special_variable_replacements end |
Instance Method Details
#add_blocks_to_regexp(options) ⇒ Object
Replace Regepx or String keys to have the enclosing blocks
72 73 74 75 76 77 78 |
# File 'lib/lm_docstache/parser.rb', line 72 def add_blocks_to_regexp() .inject({}) do |x, (regexp_str, value)| key = regexp_str.is_a?(String) ? Regexp.new("{{#{regexp_str}}}") : /{{#{regexp_str.source}}/ x[key] = value x end end |
#parse_and_update_document! ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/lm_docstache/parser.rb', line 80 def parse_and_update_document! find_blocks replace_conditional_blocks_in_document! replace_data_sequentially_in_document! replace_variables_in_document! end |