24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/rubb/parser.rb', line 24
def parse(code, current_tag_name, ={})
return nil if code.length == 0
ignore_bbcode_in_children = false
node = case current_tag_name
when 'h1'
Node::Simple.new(:html_tag_name => 'h1')
when 'h2'
Node::Simple.new(:html_tag_name => 'h2')
when 'h3'
Node::Simple.new(:html_tag_name => 'h3')
when 'h4'
Node::Simple.new(:html_tag_name => 'h4')
when 'h5'
Node::Simple.new(:html_tag_name => 'h5')
when 'h6'
Node::Simple.new(:html_tag_name => 'h6')
when 'b'
Node::Simple.new(:html_tag_name => 'strong')
when 'i'
Node::Simple.new(:html_tag_name => 'em')
when 'u'
Node::Simple.new(:html_tag_name => 'ins')
when 's'
Node::Simple.new(:html_tag_name => 'del')
when 'code'
ignore_bbcode_in_children = true
Node::Simple.new(:html_tag_name => 'pre')
when 'ul', 'ol', 'li', 'table', 'tr', 'th', 'td'
Node::Simple.new(:html_tag_name => current_tag_name)
when 'size'
Node::Styled.new(:html_tag_name => 'span',
:style_hash => {'font-size' => ([:param] ? [:param].to_f.to_s + 'px' : nil)})
when 'color'
Node::Styled.new(:html_tag_name => 'span',
:style_hash => {'color' => ([:param] ? [:param].to_s : nil)})
when 'left'
Node::Styled.new(:html_tag_name => 'div', :style_hash => {'text-align' => 'left'})
when 'center'
Node::Styled.new(:html_tag_name => 'div', :style_hash => {'text-align' => 'center'})
when 'right'
Node::Styled.new(:html_tag_name => 'div', :style_hash => {'text-align' => 'right'})
when 'quote'
Node::Quote.new(:who => ([:param] ? [:param].to_s : ''))
when 'url'
ignore_bbcode_in_children = true if [:param].nil?
Node::URL.new(:url => ([:param] ? [:param].to_s : ''))
when 'email'
ignore_bbcode_in_children = true if [:param].nil?
Node::Email.new(:email => ([:param] ? [:param].to_s : ''))
when 'img'
ignore_bbcode_in_children = true
if([:param] && (dimensions = [:param].to_s.split('x')).size >= 2)
Node::Image.new(:width => dimensions[0].to_i, :height => dimensions[1].to_i)
else
Node::Image.new
end
else
Node.new
end
until code.empty?
match_data = code.match(TEXT_REGEXP)
if(match_data)
code.replace(match_data.post_match)
case current_tag_name
when 'ul', 'ol', 'table', 'tr' else
node << Node::Text.new(:text => match_data[1], :ignore_whitespace => ignore_bbcode_in_children)
end
else
match_data = code.match(BB_REGEXP)
if(match_data)
tag = match_data[1].sub(/\s*=\s*/,' = ').split(' ')
tag_name = tag.first.downcase
code.replace(match_data.post_match)
if(current_tag_name && tag_name == ('/' + current_tag_name))
return node
else
case current_tag_name
when 'ul', 'ol' if(tag_name == 'li')
node << parse(code, tag_name)
end
when 'table'
if(tag_name == 'tr')
node << parse(code, tag_name)
end
when 'tr'
if(tag_name == 'th' || tag_name == 'td')
node << parse(code, tag_name)
end
else if(ignore_bbcode_in_children)
node << Node::Text.new(:text => match_data[0], :ignore_whitespace => true)
else
case tag_name
when 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'b', 'i', 'u', 's', 'code', 'left', 'center', 'right', 'ul', 'ol', 'table'
node << parse(code, tag_name)
when 'url', 'email', 'img', 'size', 'color', 'quote'
if(tag[1] == '=' && tag.size > 2) param = remove_wrapping_quotes(tag[2..(tag.size)].join(' '))
node << parse(code, tag_name, {:param => param})
else
node << parse(code, tag_name)
end
else
node << Node::Text.new(:text => match_data[0])
end
end
end
end
end
end
end
node
end
|