Class: HtmlBeautifier::Parser
- Inherits:
-
Object
- Object
- HtmlBeautifier::Parser
- Defined in:
- lib/htmlbeautifier/parser.rb
Class Method Summary collapse
Instance Method Summary collapse
- #dispatch(scanner, receiver) ⇒ Object
-
#initialize(&blk) ⇒ Parser
constructor
A new instance of Parser.
- #map(pattern, method) ⇒ Object
- #scan(subject, receiver) ⇒ Object
Constructor Details
#initialize(&blk) ⇒ Parser
Returns a new instance of Parser.
16 17 18 19 20 21 |
# File 'lib/htmlbeautifier/parser.rb', line 16 def initialize(&blk) @maps = [] if block_given? self.instance_eval(&blk) end end |
Class Method Details
.debug(match, method) ⇒ Object
10 11 12 13 14 |
# File 'lib/htmlbeautifier/parser.rb', line 10 def self.debug(match, method) if defined? @debug_block @debug_block.call(match, method) end end |
.debug_block(&blk) ⇒ Object
6 7 8 |
# File 'lib/htmlbeautifier/parser.rb', line 6 def self.debug_block(&blk) @debug_block = blk end |
Instance Method Details
#dispatch(scanner, receiver) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/htmlbeautifier/parser.rb', line 34 def dispatch(scanner, receiver) @maps.each do |pattern, method| if scanner.scan(pattern) params = [] i = 1 while scanner[i] params << scanner[i] i += 1 end params = [scanner[0]] if params.empty? self.class.debug(scanner[0], method) receiver.__send__(method, *params) return end end raise "Unmatched sequence #{match.inspect}" end |
#map(pattern, method) ⇒ Object
23 24 25 |
# File 'lib/htmlbeautifier/parser.rb', line 23 def map(pattern, method) @maps << [pattern, method] end |
#scan(subject, receiver) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/htmlbeautifier/parser.rb', line 27 def scan(subject, receiver) scanner = StringScanner.new(subject) until scanner.eos? dispatch(scanner, receiver) end end |