Class: HTML2AsciiMath::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/html2asciimath/converter.rb

Overview

This class is responsible for converting HTML math expressions to AsciiMath.

It runs two small parsers: first HTMLparser deals with HTML syntax, and then HTMLTextParser processes textual content found between HTML elements. Thanks to this two-phase processing, HTMLTextParser receives input which is already decoded (i.e. without any HTML entities), which in turn allows to keep its grammar simple.

Examples:

html_string = "<i>x</i>+<i>y</i>"
Converter.new(html_string).transform # => "x + y"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Converter

Returns a new instance of Converter.



21
22
23
# File 'lib/html2asciimath/converter.rb', line 21

def initialize(str)
  @html_parser = HTMLParser.new(str, self)
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



18
19
20
# File 'lib/html2asciimath/converter.rb', line 18

def ast
  @ast
end

#ast_stackObject (readonly)

Returns the value of attribute ast_stack.



18
19
20
# File 'lib/html2asciimath/converter.rb', line 18

def ast_stack
  @ast_stack
end

#html_parserObject (readonly)

Returns the value of attribute html_parser.



18
19
20
# File 'lib/html2asciimath/converter.rb', line 18

def html_parser
  @html_parser
end

#variable_modeObject

Returns the value of attribute variable_mode.



19
20
21
# File 'lib/html2asciimath/converter.rb', line 19

def variable_mode
  @variable_mode
end

Instance Method Details

#close_groupObject



33
34
35
# File 'lib/html2asciimath/converter.rb', line 33

def close_group
  push ast_stack.pop
end

#open_groupObject



29
30
31
# File 'lib/html2asciimath/converter.rb', line 29

def open_group
  ast_stack.push AST.new
end

#parseObject



46
47
48
49
50
51
52
53
# File 'lib/html2asciimath/converter.rb', line 46

def parse
  return if @ast

  @ast = AST.new
  @ast_stack = [@ast]
  @variable_mode = false
  html_parser.parse
end

#push(*objs) ⇒ Object



37
38
39
# File 'lib/html2asciimath/converter.rb', line 37

def push(*objs)
  ast_stack.last.push(*objs)
end

#to_asciimathObject



41
42
43
44
# File 'lib/html2asciimath/converter.rb', line 41

def to_asciimath
  parse
  ast.to_asciimath
end

#transformObject



25
26
27
# File 'lib/html2asciimath/converter.rb', line 25

def transform
  to_asciimath
end