Method: PlainText::ParseRule#initialize

Defined in:
lib/plain_text/parse_rule.rb

#initialize(rule = nil, name: nil) {|inprm| ... } ⇒ ParseRule

Constructor

The main argument is a single or an Array of Proc or Regexp. Alternatively, a block can be given. If Regexp(s) is given, it should include grouping (to enclose the entire Regexp usually). If not, grouping is added forcibly.

Note that the method (private method #add_grouping) wrongly recognizes patterns like /[(x]/ to contain grouping. Also, it does not raise warning when more than one grouping is defined. In fact, multiple groupings might be useful in some cases, such as,

/(\n{2,})([^\n]*\S)([[:blank:]]*={2,}\n{2,})/

would produce, when applied, a series of

[Paragraph, Boundary("\n\n"), Paragraph::Title, Boundary("==\n\n")]

Just make sure the number of groupings is an odd number, though.

Optionally, when a non-Array argument or block is given, a name can be specified as the human-readable name for the rule.

Parameters:

  • name (String, Symbol) (defaults to: nil)
  • rule (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (rule):

Yields:

  • (inprm)

    Block to register.

Yield Parameters:

  • inprm (String, Array<Part, Paragraph, Boundary>, Part)

    Input String/Part/Array to apply the rule to.

Yield Returns:



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/plain_text/parse_rule.rb', line 130

def initialize(rule=nil, name: nil, &rule_block)
  if defined?(rule.rules) && defined?(rule.names)
    # ParseRule given
    @rules = rule.rules.clone.map{|i| i.clone rescue i} # Deep copy
    @names = rule.names.clone.map{|i| i.clone rescue i} # Deep copy
    return
  end

  if defined? rule.to_ary
    # Array given
    @rules = rule
    @names = Array.new(@rules.size)
    return
  end

  @rules = []
  @names = []
  push(rule, name: name, &rule_block)
end