Class: Plurimath::Html::Parse

Inherits:
Parslet::Parser
  • Object
show all
Defined in:
lib/plurimath/html/parse.rb

Instance Method Summary collapse

Instance Method Details

#array_to_expression(array, name = nil) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/plurimath/html/parse.rb', line 128

def array_to_expression(array, name = nil)
  initial_type = array.first.class
  array.reduce do |expr, tag|
    expr = str_to_expression(expr, name) if expr.is_a?(initial_type)
    expr | str_to_expression(tag, name)
  end
end

#case_insensitive_string(value) ⇒ Object



207
208
209
210
211
# File 'lib/plurimath/html/parse.rb', line 207

def case_insensitive_string(value)
  value.chars
    .map { |char| char.match?(/[A-Za-z]/) ? match["#{char.downcase}#{char.upcase}"] : str(char) }
    .reduce(:>>)
end

#decimal_markerObject



142
143
144
# File 'lib/plurimath/html/parse.rb', line 142

def decimal_marker
  str(Plurimath.configuration.decimal)
end

#html_entityObject



160
161
162
163
164
# File 'lib/plurimath/html/parse.rb', line 160

def html_entity
  (str("&#x") >> match["0-9a-fA-F"].repeat(1) >> str(";")) |
    (str("&#") >> match["0-9"].repeat(1) >> str(";")) |
    (str("&") >> match["a-zA-Z"] >> match["a-zA-Z0-9"].repeat >> str(";"))
end

#html_tag_name(tag_name) ⇒ Object



188
189
190
191
192
# File 'lib/plurimath/html/parse.rb', line 188

def html_tag_name(tag_name)
  return match["a-zA-Z"] >> match["a-zA-Z0-9:._-"].repeat unless tag_name

  case_insensitive_string(tag_name) >> tag_name_boundary
end

#matching_close_tagObject



182
183
184
185
186
# File 'lib/plurimath/html/parse.rb', line 182

def matching_close_tag
  dynamic do |_source, context|
    parse_tag(:close, context.captures[:html_tag_name].to_s)
  end
end

#parse_sub_sup_tags(tag_names, transform_name = tag_names) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/plurimath/html/parse.rb', line 166

def parse_sub_sup_tags(tag_names, transform_name = tag_names)
  Array(tag_names).map do |tag_name|
    parse_tag(:open, tag_name) >>
      sequence.as(:"#{transform_name}_value") >>
      parse_tag(:close, tag_name)
  end.reduce(:|)
end

#parse_tag(opts, tag_name = nil, capture_name: nil) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/plurimath/html/parse.rb', line 146

def parse_tag(opts, tag_name = nil, capture_name: nil)
  tag = str("<")
  tag = tag >> str("/") if opts == :close
  name_expression = html_tag_name(tag_name)
  name_expression = name_expression.capture(capture_name) if capture_name
  tag = tag >> name_expression
  tag = tag >> tag_attributes if opts == :open
  tag >> str(">")
end

#parse_void_tag(tag_name) ⇒ Object



156
157
158
# File 'lib/plurimath/html/parse.rb', line 156

def parse_void_tag(tag_name)
  str("<") >> html_tag_name(tag_name) >> tag_attributes >> str(">")
end

#quoted_attribute_valueObject



202
203
204
205
# File 'lib/plurimath/html/parse.rb', line 202

def quoted_attribute_value
  (str('"') >> match['^"'].repeat >> str('"')) |
    (str("'") >> match["^'"].repeat >> str("'"))
end

#str_to_expression(string, name) ⇒ Object



136
137
138
139
140
# File 'lib/plurimath/html/parse.rb', line 136

def str_to_expression(string, name)
  return str(string) if name.nil?

  str(string).as(name)
end

#tag_attributesObject



198
199
200
# File 'lib/plurimath/html/parse.rb', line 198

def tag_attributes
  (quoted_attribute_value | match["^<>"]).repeat
end

#tag_name_boundaryObject



194
195
196
# File 'lib/plurimath/html/parse.rb', line 194

def tag_name_boundary
  match["\\s/>"].present?
end

#wrapped_tag(expression) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/plurimath/html/parse.rb', line 174

def wrapped_tag(expression)
  scope do
    parse_tag(:open, capture_name: :html_tag_name) >>
      expression >>
      matching_close_tag
  end
end