Module: Guileless::ParseMethods

Included in:
Parser
Defined in:
lib/guileless/parse_methods.rb

Instance Method Summary collapse

Instance Method Details

#escape_ampersandObject



4
5
6
7
8
# File 'lib/guileless/parse_methods.rb', line 4

def escape_ampersand
  value = stream.peek?(/[\w\d]+;/) ? "&" : "&"
  @char_count += value.length
  value
end

#parse_attribute_name(char) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/guileless/parse_methods.rb', line 16

def parse_attribute_name(char)
  case
  when char !=~ /[\w\-]/
    stream.reinject char
    [false, :tag]
  when char == "=" && stream.peek?("'")
    stream.discard
    ["='", :attribute_value_single_quote]
  when char == "=" && stream.peek?("\"")
    stream.discard
    ["=\"", :attribute_value_double_quote]
  when char == "="
    [char, :attribute_value]
  end
end

#parse_attribute_value(char) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/guileless/parse_methods.rb', line 32

def parse_attribute_value(char)
  case
  when char !=~ /[\w\d]/
    stream.reinject char
    [false, :tag]
  end
end

#parse_attribute_value_double_quote(char) ⇒ Object



47
48
49
50
51
52
# File 'lib/guileless/parse_methods.rb', line 47

def parse_attribute_value_double_quote(char)
  case
  when char == "\""
    [char, :tag]
  end
end

#parse_attribute_value_single_quote(char) ⇒ Object



40
41
42
43
44
45
# File 'lib/guileless/parse_methods.rb', line 40

def parse_attribute_value_single_quote(char)
  case
  when char == "'"
    [char, :tag]
  end
end

#parse_closing_tag(char) ⇒ Object



71
72
73
74
75
76
# File 'lib/guileless/parse_methods.rb', line 71

def parse_closing_tag(char)
  case
  when char == ">"
    [char, :text]
  end
end

#parse_closing_tag_name(char) ⇒ Object



89
90
91
# File 'lib/guileless/parse_methods.rb', line 89

def parse_closing_tag_name(char)
  parse_tag_name(char, :closing_tag)
end

#parse_comment(char) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/guileless/parse_methods.rb', line 54

def parse_comment(char)
  case
  when char == "-" && stream.peek?("->")
    stream.discard(2)
    ["-->", :text]
  end
end

#parse_left_angled_quote(char) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/guileless/parse_methods.rb', line 93

def parse_left_angled_quote(char)
  case

  # Comment
  when stream.peek?("!--")
    stream.discard(3)
    ["<!--", :comment]

  # Opening tag
  when stream.peek?(html_tags)
    start_tag
    [char, :tag_name]

  # Closing tag
  when stream.peek?(closing(html_tags))
    start_tag
    stream.discard
    ["</", :closing_tag_name]

  # Escape left angled bracket
  else
    @char_count += 4
    "&lt;"
  end
end

#parse_linebreakObject



119
120
121
122
123
124
125
126
127
# File 'lib/guileless/parse_methods.rb', line 119

def parse_linebreak
  if stream.peek?("\n")
    flush_buffer
    stream.strip_whitespace
    false
  else
    "<br>"
  end
end

#parse_tag(char) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/guileless/parse_methods.rb', line 62

def parse_tag(char)
  case
  when char =~ /\w/
    [char, :attribute_name]
  when char == ">"
    [char, :text]
  end
end

#parse_tag_name(char, next_state = :tag) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/guileless/parse_methods.rb', line 78

def parse_tag_name(char, next_state=:tag)
  case
  when char =~ /\w/
    @tag_name += char
    char
  else
    stream.reinject char
    [false, next_state]
  end
end

#parse_text(char) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/guileless/parse_methods.rb', line 129

def parse_text(char)
  case

  # Comment
  when char == "<"
    parse_left_angled_quote(char)

  # Escape right angled bracket
  when char == ">"
    @char_count += 4
    "&gt;"

  # Escape ampersands
  when char == "&"
    escape_ampersand

  # Line break
  when char == "\n"
    parse_linebreak

  when char !=~ /\s/
    @char_count += 1
    char

  end
end

#start_tagObject



10
11
12
13
14
# File 'lib/guileless/parse_methods.rb', line 10

def start_tag
  if stream.peek?(block_level_tags) || stream.peek?(closing(block_level_tags))
    flush_buffer
  end
end