Class: Guileless::Parser
Instance Attribute Summary collapse
Instance Method Summary
collapse
#escape_ampersand, #parse_attribute_name, #parse_attribute_value, #parse_attribute_value_double_quote, #parse_attribute_value_single_quote, #parse_closing_tag, #parse_closing_tag_name, #parse_comment, #parse_left_angled_quote, #parse_linebreak, #parse_tag, #parse_tag_name, #parse_text, #start_tag
Methods included from TagLibrary
#block_level_tags, #closing, #html_tags, #paragraph_container_tags
before_transition, #state, #transition, transitions
Constructor Details
#initialize(input) ⇒ Parser
Returns a new instance of Parser.
14
15
16
|
# File 'lib/guileless/parser.rb', line 14
def initialize(input)
@input = input
end
|
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer.
6
7
8
|
# File 'lib/guileless/parser.rb', line 6
def buffer
@buffer
end
|
#char_count ⇒ Object
Returns the value of attribute char_count.
6
7
8
|
# File 'lib/guileless/parser.rb', line 6
def char_count
@char_count
end
|
Returns the value of attribute input.
6
7
8
|
# File 'lib/guileless/parser.rb', line 6
def input
@input
end
|
#stream ⇒ Object
Returns the value of attribute stream.
6
7
8
|
# File 'lib/guileless/parser.rb', line 6
def stream
@stream
end
|
#tag_name ⇒ Object
Returns the value of attribute tag_name.
6
7
8
|
# File 'lib/guileless/parser.rb', line 6
def tag_name
@tag_name
end
|
#tag_stack ⇒ Object
Returns the value of attribute tag_stack.
6
7
8
|
# File 'lib/guileless/parser.rb', line 6
def tag_stack
@tag_stack
end
|
Instance Method Details
#add_tag_to_stack ⇒ Object
65
66
67
|
# File 'lib/guileless/parser.rb', line 65
def add_tag_to_stack
@tag_stack << tag_name
end
|
#close_tag ⇒ Object
69
70
71
|
# File 'lib/guileless/parser.rb', line 69
def close_tag
unwind_tag_stack(tag_name)
end
|
87
88
89
90
91
|
# File 'lib/guileless/parser.rb', line 87
def filtered_input
input.gsub(/\r/, '')
end
|
#finish_tag ⇒ Object
59
60
61
62
63
|
# File 'lib/guileless/parser.rb', line 59
def finish_tag
if block_level_tags.include?(tag_name)
flush_buffer
end
end
|
#flush_buffer ⇒ Object
49
50
51
52
53
54
55
56
57
|
# File 'lib/guileless/parser.rb', line 49
def flush_buffer
if wrap_in_paragraph?
if buffer.buffered? && char_count > 0
buffer.wrap("<p>", "</p>")
end
end
@char_count = 0
buffer.flush
end
|
#last_block_level_tag ⇒ Object
79
80
81
|
# File 'lib/guileless/parser.rb', line 79
def last_block_level_tag
tag_stack.reverse.select{|t| block_level_tags.include?(t) }.first
end
|
#parse! ⇒ Object
23
24
25
26
27
|
# File 'lib/guileless/parser.rb', line 23
def parse!
reset!
read while !stream.empty?
transition(:end)
end
|
#read ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/guileless/parser.rb', line 29
def read
char = stream.fetch
value, next_state = Array(self.send("parse_#{state}".to_sym, char))
if value === nil
buffer.add char
elsif value
buffer.add value
end
transition(next_state) if next_state
end
|
#reset_tag_name ⇒ Object
83
84
85
|
# File 'lib/guileless/parser.rb', line 83
def reset_tag_name
@tag_name = ""
end
|
#to_html ⇒ Object
18
19
20
21
|
# File 'lib/guileless/parser.rb', line 18
def to_html
parse!
buffer.to_s
end
|
#unwind_tag_stack(tag) ⇒ Object
73
74
75
76
77
|
# File 'lib/guileless/parser.rb', line 73
def unwind_tag_stack(tag)
if tag_stack.include?(tag)
last_tag = tag_stack.pop while last_tag != tag
end
end
|
#wrap_in_paragraph? ⇒ Boolean
42
43
44
45
46
47
|
# File 'lib/guileless/parser.rb', line 42
def wrap_in_paragraph?
state == :text &&
char_count > 0 &&
buffer.buffered? &&
(!last_block_level_tag || paragraph_container_tags.include?(last_block_level_tag))
end
|