redel 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/AUTHORS +1 -0
- data/LICENSE +674 -0
- data/README.md +72 -0
- data/Rakefile +29 -0
- data/lib/redel/grammar_simple.rb +229 -0
- data/lib/redel/helpers.rb +44 -0
- data/lib/redel/image.rb +18 -0
- data/lib/redel/output_html.rb +231 -0
- data/lib/redel/parser.rb +164 -0
- data/lib/redel/xhtml.rb +296 -0
- data/lib/redel.rb +4 -0
- data/redel.gemspec +22 -0
- data/test/tc_html.rb +26 -0
- data/test/tc_parser.rb +65 -0
- metadata +71 -0
data/lib/redel/parser.rb
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# Please see the LICENSE file for copyright and distribution information
|
|
2
|
+
require 'strscan'
|
|
3
|
+
|
|
4
|
+
module Redel
|
|
5
|
+
class OpenTag
|
|
6
|
+
attr_reader :type, :level
|
|
7
|
+
attr_accessor :data, :close
|
|
8
|
+
def initialize(type,level)
|
|
9
|
+
@type = type
|
|
10
|
+
@data = nil
|
|
11
|
+
@close = -1
|
|
12
|
+
@level = level
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
class CloseTag
|
|
16
|
+
attr_reader :type, :open, :position_in_markup, :level
|
|
17
|
+
def initialize(type,open,position_in_markup,level)
|
|
18
|
+
@type = type
|
|
19
|
+
@open = open
|
|
20
|
+
@position_in_markup = position_in_markup # in bytes
|
|
21
|
+
@level = level
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
class TagError < StandardError; end
|
|
26
|
+
class TagSkipError < StandardError; end
|
|
27
|
+
class TagContentSkipError < StandardError; end
|
|
28
|
+
|
|
29
|
+
class Output
|
|
30
|
+
def finish_prepare
|
|
31
|
+
end
|
|
32
|
+
def finish_output
|
|
33
|
+
''
|
|
34
|
+
end
|
|
35
|
+
def string(data)
|
|
36
|
+
data
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
class Grammar
|
|
41
|
+
attr_reader :tree # a list which pretends to be a tree :-)
|
|
42
|
+
|
|
43
|
+
class Pattern
|
|
44
|
+
attr_reader :type, :pstart, :pend, :bol
|
|
45
|
+
def initialize(type, pstart, pend, bol=false)
|
|
46
|
+
@type = type
|
|
47
|
+
@pstart = pstart
|
|
48
|
+
@pend = pend
|
|
49
|
+
@bol = bol
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def initialize
|
|
54
|
+
@tree = []
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def self::parse(text)
|
|
58
|
+
new.parse(text)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def parse(text) #{{{
|
|
62
|
+
gparse(text,grammar('ROOT'))
|
|
63
|
+
self
|
|
64
|
+
end #}}}
|
|
65
|
+
|
|
66
|
+
# outputs either create text from opening and closing tags (Output), or have a
|
|
67
|
+
# method render that gets the tree
|
|
68
|
+
def output(output) #{{{
|
|
69
|
+
return output.render(@tree) if output.respond_to?(:render)
|
|
70
|
+
out = +''
|
|
71
|
+
skip = {}
|
|
72
|
+
@tree.each_with_index do |c,i|
|
|
73
|
+
next if skip[i]
|
|
74
|
+
begin
|
|
75
|
+
name, data = case c
|
|
76
|
+
when String; ['string',c]
|
|
77
|
+
when OpenTag; ['mO' + c.type,c.data]
|
|
78
|
+
when CloseTag; ['mC' + c.type,@tree[c.open].data]
|
|
79
|
+
end
|
|
80
|
+
next unless output.respond_to?(name,true)
|
|
81
|
+
met = output.method(name)
|
|
82
|
+
out << case met.arity
|
|
83
|
+
when 1; met.call(data)
|
|
84
|
+
when 2; met.call(c,data)
|
|
85
|
+
when 4; met.call(c,i,@tree,data)
|
|
86
|
+
else ''
|
|
87
|
+
end.to_s
|
|
88
|
+
rescue TagSkipError
|
|
89
|
+
skip[c.close] = true if c.is_a?(OpenTag)
|
|
90
|
+
rescue TagContentSkipError
|
|
91
|
+
(i + 1).upto(c.close-1) { |j| skip[j] = true } if c.is_a?(OpenTag)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
out << output.finish_output.to_s
|
|
95
|
+
end #}}}
|
|
96
|
+
|
|
97
|
+
def prepare(output) #{{{
|
|
98
|
+
@tree.each do |c|
|
|
99
|
+
next unless c.is_a?(OpenTag) && output.respond_to?('mP' + c.type,true)
|
|
100
|
+
begin
|
|
101
|
+
output.send('mP' + c.type,c.data)
|
|
102
|
+
rescue TagSkipError, TagContentSkipError
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
output.finish_prepare
|
|
106
|
+
self
|
|
107
|
+
end #}}}
|
|
108
|
+
|
|
109
|
+
private
|
|
110
|
+
|
|
111
|
+
def grammar(name) #{{{
|
|
112
|
+
self.class.const_defined?(name) ? self.class.const_get(name) : []
|
|
113
|
+
end #}}}
|
|
114
|
+
|
|
115
|
+
def gparse(text,grammar,position_in_markup=0,level=0) #{{{
|
|
116
|
+
return if text.nil?
|
|
117
|
+
s = StringScanner.new(text)
|
|
118
|
+
until s.eos?
|
|
119
|
+
success = false
|
|
120
|
+
grammar.each do |pat|
|
|
121
|
+
next unless s.match?(pat.pstart)
|
|
122
|
+
pos = s.pos
|
|
123
|
+
bol = s.beginning_of_line?
|
|
124
|
+
ts = s.scan(pat.pstart)
|
|
125
|
+
te = if s.eos?
|
|
126
|
+
'' =~ pat.pend ? '' : nil
|
|
127
|
+
else
|
|
128
|
+
(pat.bol && !bol) ? nil : s.scan_until(pat.pend)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
if te.nil?
|
|
132
|
+
s.pos = pos
|
|
133
|
+
next
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
ti = te.sub(pat.pend,'')
|
|
137
|
+
te = te[ti.length..-1]
|
|
138
|
+
@tree << ot = OpenTag.new(pat.type,level)
|
|
139
|
+
tpos = @tree.length-1
|
|
140
|
+
begin
|
|
141
|
+
inner = respond_to?('m' + pat.type,true) ? send('m' + pat.type,ts,ti,te) : ti
|
|
142
|
+
rescue TagError
|
|
143
|
+
@tree.pop
|
|
144
|
+
s.pos = pos
|
|
145
|
+
next
|
|
146
|
+
rescue TagSkipError
|
|
147
|
+
@tree.pop
|
|
148
|
+
success = true
|
|
149
|
+
next
|
|
150
|
+
end
|
|
151
|
+
gparse(inner,grammar('G' + pat.type),position_in_markup+pos+ts.bytesize,level+1)
|
|
152
|
+
@tree << CloseTag.new(pat.type,tpos,position_in_markup+s.pos,level)
|
|
153
|
+
ot.close = @tree.length-1
|
|
154
|
+
success = true
|
|
155
|
+
break
|
|
156
|
+
end
|
|
157
|
+
unless success
|
|
158
|
+
@tree << +'' unless @tree.last.is_a?(String)
|
|
159
|
+
@tree.last << s.getch
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end #}}}
|
|
163
|
+
end
|
|
164
|
+
end
|
data/lib/redel/xhtml.rb
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
module Redel; module XHTML
|
|
2
|
+
def unweb(string) XHTML::unweb(string) end
|
|
3
|
+
def web(string) XHTML::web(string) end
|
|
4
|
+
def amp(string) XHTML::amp(string) end
|
|
5
|
+
def ent2ncr(string) XHTML::ent2ncr(string) end
|
|
6
|
+
|
|
7
|
+
def XHTML::unwebxml(string)
|
|
8
|
+
"#{string}".gsub(/"/,"\"").gsub(/>/,">").gsub(/</,"<").gsub(/'/,"'").gsub(/&/,"&").gsub(/ /,' ').gsub(/\r/,'')
|
|
9
|
+
end
|
|
10
|
+
def XHTML::unweb(string)
|
|
11
|
+
tstring = string.dup
|
|
12
|
+
"#{tstring}".gsub!(/&(amp;)+/,'&')
|
|
13
|
+
"#{tstring}".gsub!(/&(gt;)+/,'>')
|
|
14
|
+
"#{tstring}".gsub!(/&(lt;)+/,'<')
|
|
15
|
+
"#{tstring}".gsub!(/&(apos;)+/,''')
|
|
16
|
+
"#{tstring}".gsub!(/&(quot;)+/,'"')
|
|
17
|
+
"#{tstring}".gsub(/"/,"\"").gsub(/>/,">").gsub(/</,"<").gsub(/'/,"'").gsub(/&/,"&").gsub(/ /,' ').gsub(/\r/,'')
|
|
18
|
+
end
|
|
19
|
+
def XHTML::webxml(string)
|
|
20
|
+
"#{string}".gsub(/\&/,"&").gsub(/\"/,""").gsub(/>/,">").gsub(/</,"<").gsub(/'/,"'")
|
|
21
|
+
end
|
|
22
|
+
def XHTML::web(string)
|
|
23
|
+
"#{string}".gsub(/\&(?!amp;|quot;|apos;|gt;|lt;)/,"&").gsub(/\"/,""").gsub(/>/,">").gsub(/</,"<").gsub(/'/,"'")
|
|
24
|
+
end
|
|
25
|
+
def XHTML::amp(string)
|
|
26
|
+
"#{string}".gsub(/\&(?!amp;)/,"&")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def XHTML::ent2ncr(string)
|
|
30
|
+
reps = {
|
|
31
|
+
'"' => '"',
|
|
32
|
+
'&' => '&',
|
|
33
|
+
'⁄' => '/',
|
|
34
|
+
'<' => '<',
|
|
35
|
+
'>' => '>',
|
|
36
|
+
'\|' => '¦',
|
|
37
|
+
' ' => ' ',
|
|
38
|
+
'¡' => '¡',
|
|
39
|
+
'¢' => '¢',
|
|
40
|
+
'£' => '£',
|
|
41
|
+
'¤' => '¤',
|
|
42
|
+
'¥' => '¥',
|
|
43
|
+
'¦' => '¦',
|
|
44
|
+
'&brkbar;' => '¦',
|
|
45
|
+
'§' => '§',
|
|
46
|
+
'¨' => '¨',
|
|
47
|
+
'¨' => '¨',
|
|
48
|
+
'©' => '©',
|
|
49
|
+
'ª' => 'ª',
|
|
50
|
+
'«' => '«',
|
|
51
|
+
'¬' => '¬',
|
|
52
|
+
'­' => '­',
|
|
53
|
+
'®' => '®',
|
|
54
|
+
'¯' => '¯',
|
|
55
|
+
'&hibar;' => '¯',
|
|
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
|
+
'Α' => 'Α',
|
|
145
|
+
'Β' => 'Β',
|
|
146
|
+
'Γ' => 'Γ',
|
|
147
|
+
'Δ' => 'Δ',
|
|
148
|
+
'Ε' => 'Ε',
|
|
149
|
+
'Ζ' => 'Ζ',
|
|
150
|
+
'Η' => 'Η',
|
|
151
|
+
'Θ' => 'Θ',
|
|
152
|
+
'Ι' => 'Ι',
|
|
153
|
+
'Κ' => 'Κ',
|
|
154
|
+
'Λ' => 'Λ',
|
|
155
|
+
'Μ' => 'Μ',
|
|
156
|
+
'Ν' => 'Ν',
|
|
157
|
+
'Ξ' => 'Ξ',
|
|
158
|
+
'Ο' => 'Ο',
|
|
159
|
+
'Π' => 'Π',
|
|
160
|
+
'Ρ' => 'Ρ',
|
|
161
|
+
'Σ' => 'Σ',
|
|
162
|
+
'Τ' => 'Τ',
|
|
163
|
+
'Υ' => 'Υ',
|
|
164
|
+
'Φ' => 'Φ',
|
|
165
|
+
'Χ' => 'Χ',
|
|
166
|
+
'Ψ' => 'Ψ',
|
|
167
|
+
'Ω' => 'Ω',
|
|
168
|
+
'α' => 'α',
|
|
169
|
+
'β' => 'β',
|
|
170
|
+
'γ' => 'γ',
|
|
171
|
+
'δ' => 'δ',
|
|
172
|
+
'ε' => 'ε',
|
|
173
|
+
'ζ' => 'ζ',
|
|
174
|
+
'η' => 'η',
|
|
175
|
+
'θ' => 'θ',
|
|
176
|
+
'ι' => 'ι',
|
|
177
|
+
'κ' => 'κ',
|
|
178
|
+
'λ' => 'λ',
|
|
179
|
+
'μ' => 'μ',
|
|
180
|
+
'ν' => 'ν',
|
|
181
|
+
'ξ' => 'ξ',
|
|
182
|
+
'ο' => 'ο',
|
|
183
|
+
'π' => 'π',
|
|
184
|
+
'ρ' => 'ρ',
|
|
185
|
+
'ς' => 'ς',
|
|
186
|
+
'σ' => 'σ',
|
|
187
|
+
'τ' => 'τ',
|
|
188
|
+
'υ' => 'υ',
|
|
189
|
+
'φ' => 'φ',
|
|
190
|
+
'χ' => 'χ',
|
|
191
|
+
'ψ' => 'ψ',
|
|
192
|
+
'ω' => 'ω',
|
|
193
|
+
'ϑ' => 'ϑ',
|
|
194
|
+
'ϒ' => 'ϒ',
|
|
195
|
+
'ϖ' => 'ϖ',
|
|
196
|
+
' ' => ' ',
|
|
197
|
+
' ' => ' ',
|
|
198
|
+
' ' => ' ',
|
|
199
|
+
'‌' => '‌',
|
|
200
|
+
'‍' => '‍',
|
|
201
|
+
'‎' => '‎',
|
|
202
|
+
'‏' => '‏',
|
|
203
|
+
'–' => '–',
|
|
204
|
+
'—' => '—',
|
|
205
|
+
'‘' => '‘',
|
|
206
|
+
'’' => '’',
|
|
207
|
+
'‚' => '‚',
|
|
208
|
+
'“' => '“',
|
|
209
|
+
'”' => '”',
|
|
210
|
+
'„' => '„',
|
|
211
|
+
'†' => '†',
|
|
212
|
+
'‡' => '‡',
|
|
213
|
+
'•' => '•',
|
|
214
|
+
'…' => '…',
|
|
215
|
+
'‰' => '‰',
|
|
216
|
+
'′' => '′',
|
|
217
|
+
'″' => '″',
|
|
218
|
+
'‹' => '‹',
|
|
219
|
+
'›' => '›',
|
|
220
|
+
'‾' => '‾',
|
|
221
|
+
'⁄' => '⁄',
|
|
222
|
+
'€' => '€',
|
|
223
|
+
'ℑ' => 'ℑ',
|
|
224
|
+
'℘' => '℘',
|
|
225
|
+
'ℜ' => 'ℜ',
|
|
226
|
+
'™' => '™',
|
|
227
|
+
'ℵ' => 'ℵ',
|
|
228
|
+
'↵' => '↵',
|
|
229
|
+
'⇐' => '⇐',
|
|
230
|
+
'⇑' => '⇑',
|
|
231
|
+
'⇒' => '⇒',
|
|
232
|
+
'⇓' => '⇓',
|
|
233
|
+
'⇔' => '⇔',
|
|
234
|
+
'∀' => '∀',
|
|
235
|
+
'∂' => '∂',
|
|
236
|
+
'∃' => '∃',
|
|
237
|
+
'∅' => '∅',
|
|
238
|
+
'∇' => '∇',
|
|
239
|
+
'∈' => '∈',
|
|
240
|
+
'∉' => '∉',
|
|
241
|
+
'∋' => '∋',
|
|
242
|
+
'∏' => '∏',
|
|
243
|
+
'∑' => '∑',
|
|
244
|
+
'∑' => '∑',
|
|
245
|
+
'−' => '−',
|
|
246
|
+
'∗' => '∗',
|
|
247
|
+
'√' => '√',
|
|
248
|
+
'∝' => '∝',
|
|
249
|
+
'∞' => '∞',
|
|
250
|
+
'∠' => '∠',
|
|
251
|
+
'∧' => '∧',
|
|
252
|
+
'∨' => '∨',
|
|
253
|
+
'∩' => '∩',
|
|
254
|
+
'∪' => '∪',
|
|
255
|
+
'∫' => '∫',
|
|
256
|
+
'∴' => '∴',
|
|
257
|
+
'∼' => '∼',
|
|
258
|
+
'≅' => '≅',
|
|
259
|
+
'≈' => '≈',
|
|
260
|
+
'≠' => '≠',
|
|
261
|
+
'≡' => '≡',
|
|
262
|
+
'≤' => '≤',
|
|
263
|
+
'≥' => '≥',
|
|
264
|
+
'⊂' => '⊂',
|
|
265
|
+
'⊃' => '⊃',
|
|
266
|
+
'⊄' => '⊄',
|
|
267
|
+
'⊆' => '⊆',
|
|
268
|
+
'⊇' => '⊇',
|
|
269
|
+
'⊕' => '⊕',
|
|
270
|
+
'⊗' => '⊗',
|
|
271
|
+
'⊥' => '⊥',
|
|
272
|
+
'⋅' => '⋅',
|
|
273
|
+
'⌈' => '⌈',
|
|
274
|
+
'⌉' => '⌉',
|
|
275
|
+
'⌊' => '⌊',
|
|
276
|
+
'⌋' => '⌋',
|
|
277
|
+
'⟨' => '〈',
|
|
278
|
+
'⟩' => '〉',
|
|
279
|
+
'←' => '←',
|
|
280
|
+
'↑' => '↑',
|
|
281
|
+
'→' => '→',
|
|
282
|
+
'↓' => '↓',
|
|
283
|
+
'↔' => '↔',
|
|
284
|
+
'◊' => '◊',
|
|
285
|
+
'♠' => '♠',
|
|
286
|
+
'♣' => '♣',
|
|
287
|
+
'♥' => '♥',
|
|
288
|
+
'♦' => '♦'
|
|
289
|
+
}
|
|
290
|
+
string.gsub(/\&\w+;/) do|m|
|
|
291
|
+
r = reps[m]
|
|
292
|
+
r.nil? ? m : r
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
end; end
|
data/lib/redel.rb
ADDED
data/redel.gemspec
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Gem::Specification.new do |s|
|
|
2
|
+
s.name = "redel"
|
|
3
|
+
s.version = "3.0.0"
|
|
4
|
+
s.platform = Gem::Platform::RUBY
|
|
5
|
+
s.license = "GPL-3.0-or-later"
|
|
6
|
+
s.summary = "Recursive delimiter parser for markup languages"
|
|
7
|
+
|
|
8
|
+
s.description = "Base for grammars that consist of nested patterns with a start and an end, and for their outputs. Originally the wiki parser of CEWebS."
|
|
9
|
+
|
|
10
|
+
s.files = Dir['{lib/**/*}'] + %w(LICENSE Rakefile redel.gemspec README.md AUTHORS)
|
|
11
|
+
s.require_path = 'lib'
|
|
12
|
+
s.extra_rdoc_files = ['README.md']
|
|
13
|
+
s.test_files = Dir['test/tc_*.rb']
|
|
14
|
+
|
|
15
|
+
s.required_ruby_version = '>=3.2'
|
|
16
|
+
|
|
17
|
+
s.authors = ['Juergen eTM Mangler']
|
|
18
|
+
s.email = 'juergen.mangler@gmail.com'
|
|
19
|
+
s.homepage = 'https://github.com/etm/redel'
|
|
20
|
+
|
|
21
|
+
s.add_runtime_dependency 'markus', '~> 4.0'
|
|
22
|
+
end
|
data/test/tc_html.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'minitest/autorun'
|
|
2
|
+
require_relative '../lib/redel'
|
|
3
|
+
|
|
4
|
+
class TestHTML < Minitest::Test
|
|
5
|
+
def simple(text) Redel::Simple.parse(text).output(Redel::HTML.new); end
|
|
6
|
+
|
|
7
|
+
def test_inline
|
|
8
|
+
assert_equal "<div><strong>a</strong> <em>b</em> <c> → </div>", simple("'''a''' ''b'' <c> ->")
|
|
9
|
+
assert_equal '<div><a href="http://a.b/c">d</a> </div>', simple('[http://a.b/c|d]')
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_heading_and_paragraphs
|
|
13
|
+
assert_equal '<h1>A</h1><div>b </div><div> </div><div>c </div>', simple("== A\nb\n\nc")
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_lists
|
|
17
|
+
out = simple("* a\n** b\n")
|
|
18
|
+
assert_equal 2, out.scan('<ul').length
|
|
19
|
+
assert_equal out.scan('<li').length, out.scan('</li>').length
|
|
20
|
+
assert_includes simple("# a\n# b"), 'counter-increment: item 1'
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_code
|
|
24
|
+
assert_includes simple(" a & b\n c"), '  c</div>'
|
|
25
|
+
end
|
|
26
|
+
end
|
data/test/tc_parser.rb
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
require 'minitest/autorun'
|
|
2
|
+
require_relative '../lib/redel'
|
|
3
|
+
|
|
4
|
+
class TestParser < Minitest::Test
|
|
5
|
+
class Language < Redel::Grammar
|
|
6
|
+
P_STRONG = Pattern.new('P_STRONG', /\*/, /\*/)
|
|
7
|
+
P_EM = Pattern.new('P_EM', /_/, /_/)
|
|
8
|
+
P_LINE = Pattern.new('P_LINE', /> /, /\n|\z/, true)
|
|
9
|
+
|
|
10
|
+
ROOT = [ P_LINE, P_STRONG, P_EM ]
|
|
11
|
+
GP_STRONG = [ P_EM ]
|
|
12
|
+
|
|
13
|
+
def mP_LINE(ts,ti,te)
|
|
14
|
+
@tree.last.data = ti.length
|
|
15
|
+
ti
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class Text < Redel::Output
|
|
20
|
+
attr_reader :lines
|
|
21
|
+
def mPP_LINE(data) (@lines ||= []) << data; end
|
|
22
|
+
def mOP_LINE(data) "<p #{data}>"; end
|
|
23
|
+
def mCP_LINE(data) '</p>'; end
|
|
24
|
+
def mOP_STRONG(data) '<b>'; end
|
|
25
|
+
def mCP_STRONG(data) '</b>'; end
|
|
26
|
+
def mOP_EM(data) '<i>'; end
|
|
27
|
+
def mCP_EM(data) '</i>'; end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class Tree
|
|
31
|
+
def render(tree) tree.length; end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def out(text) Language.parse(text).output(Text.new); end
|
|
35
|
+
|
|
36
|
+
def test_nested
|
|
37
|
+
assert_equal 'some <b>bold <i>and italic</i></b> text', out('some *bold _and italic_* text')
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def test_not_allowed_inside
|
|
41
|
+
assert_equal '<i>a *b*</i>', out('_a *b*_')
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def test_unfinished
|
|
45
|
+
assert_equal 'a * b', out('a * b')
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def test_beginning_of_line
|
|
49
|
+
assert_equal "<p 1>a</p>b > c", out("> a\nb > c")
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def test_prepare
|
|
53
|
+
output = Text.new
|
|
54
|
+
Language.parse("> a\n> bcd").prepare(output)
|
|
55
|
+
assert_equal [1,3], output.lines
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_simple
|
|
59
|
+
assert_equal '<b>a</b> <i>b</i>', Redel::Simple.parse("'''a''' ''b''").output(Text.new)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_render
|
|
63
|
+
assert_equal 3, Language.parse('*a*').output(Tree.new)
|
|
64
|
+
end
|
|
65
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: redel
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 3.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Juergen eTM Mangler
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: markus
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '4.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '4.0'
|
|
26
|
+
description: Base for grammars that consist of nested patterns with a start and an
|
|
27
|
+
end, and for their outputs. Originally the wiki parser of CEWebS.
|
|
28
|
+
email: juergen.mangler@gmail.com
|
|
29
|
+
executables: []
|
|
30
|
+
extensions: []
|
|
31
|
+
extra_rdoc_files:
|
|
32
|
+
- README.md
|
|
33
|
+
files:
|
|
34
|
+
- AUTHORS
|
|
35
|
+
- LICENSE
|
|
36
|
+
- README.md
|
|
37
|
+
- Rakefile
|
|
38
|
+
- lib/redel.rb
|
|
39
|
+
- lib/redel/grammar_simple.rb
|
|
40
|
+
- lib/redel/helpers.rb
|
|
41
|
+
- lib/redel/image.rb
|
|
42
|
+
- lib/redel/output_html.rb
|
|
43
|
+
- lib/redel/parser.rb
|
|
44
|
+
- lib/redel/xhtml.rb
|
|
45
|
+
- redel.gemspec
|
|
46
|
+
- test/tc_html.rb
|
|
47
|
+
- test/tc_parser.rb
|
|
48
|
+
homepage: https://github.com/etm/redel
|
|
49
|
+
licenses:
|
|
50
|
+
- GPL-3.0-or-later
|
|
51
|
+
metadata: {}
|
|
52
|
+
rdoc_options: []
|
|
53
|
+
require_paths:
|
|
54
|
+
- lib
|
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '3.2'
|
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: '0'
|
|
65
|
+
requirements: []
|
|
66
|
+
rubygems_version: 3.6.9
|
|
67
|
+
specification_version: 4
|
|
68
|
+
summary: Recursive delimiter parser for markup languages
|
|
69
|
+
test_files:
|
|
70
|
+
- test/tc_html.rb
|
|
71
|
+
- test/tc_parser.rb
|