redel 3.1.0 → 3.1.2
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 +4 -4
- data/lib/redel/grammar_simple.rb +7 -4
- data/lib/redel/parser.rb +15 -4
- data/redel.gemspec +1 -1
- data/test/tc_parser.rb +29 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d1faeae8175d784d5bd899012c40b208a7352b10f4017341dc47b8020a1e5fae
|
|
4
|
+
data.tar.gz: bdb2bab92250860fc5e005a15771dae62be2ef303e49c42e144af1fb3435f5fe
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 77a1704a44b018942eea028364beaa89aad7fdffa53b8eb3d152e34aa343a8c590b56437b76192a509329e7e090b909ba160724d69f4f5e07114000cc8eb5384
|
|
7
|
+
data.tar.gz: 1efc52fdbd32e86cc2979615a3706d3da1d3483148836b0817ea5ec3f41f59f72064ae795d5af37bb3a9606b401fae31ff3d15bc6250bfe817711fbdf7f84a08
|
data/lib/redel/grammar_simple.rb
CHANGED
|
@@ -4,6 +4,9 @@ require_relative 'xhtml'
|
|
|
4
4
|
|
|
5
5
|
module ReDel
|
|
6
6
|
class Simple < Grammar
|
|
7
|
+
# end of a link, its text can contain [brackets]
|
|
8
|
+
LINKEND = /\A(?>[^\[\]\n]+|\[[^\[\]\n]*\])*\K\]|\]/
|
|
9
|
+
|
|
7
10
|
#### Patterns ####
|
|
8
11
|
#### Name of pattern has to be the first parameter to pattern ####
|
|
9
12
|
#### According to the names the methods are selected ####
|
|
@@ -25,9 +28,9 @@ module ReDel
|
|
|
25
28
|
P_GLE = Pattern.new("P_GLE", /gle[ \t]*\{/, /^\}[ \t]*(\n|\z)/, true)
|
|
26
29
|
P_EM = Pattern.new("P_EM", /''/, /''/)
|
|
27
30
|
P_LINK = Pattern.new("P_LINK", /\[\[/, /\]\]/)
|
|
28
|
-
P_LINK_CEWEBS = Pattern.new("P_LINK_CEWEBS", /\[CEWebS:\/\/|\[cewebs:\/\//,
|
|
29
|
-
P_LINK_MAIL = Pattern.new("P_LINK_MAIL", /\[mailto:/,
|
|
30
|
-
P_LINK_WEB = Pattern.new("P_LINK_WEB", /\[(http|https|ftp):\/\//,
|
|
31
|
+
P_LINK_CEWEBS = Pattern.new("P_LINK_CEWEBS", /\[CEWebS:\/\/|\[cewebs:\/\//, LINKEND)
|
|
32
|
+
P_LINK_MAIL = Pattern.new("P_LINK_MAIL", /\[mailto:/, LINKEND)
|
|
33
|
+
P_LINK_WEB = Pattern.new("P_LINK_WEB", /\[(http|https|ftp):\/\//, LINKEND)
|
|
31
34
|
P_STRONG = Pattern.new("P_STRONG", /'''/, /'''/)
|
|
32
35
|
P_RAWHIDE = Pattern.new("P_RAWHIDE", /\/\*\*/, /\*\*\/|\z/)
|
|
33
36
|
P_RAW = Pattern.new("P_RAW", /\/\*/, /\*\//)
|
|
@@ -106,7 +109,7 @@ module ReDel
|
|
|
106
109
|
end
|
|
107
110
|
def mP_LINK(ts,ti,te)
|
|
108
111
|
comp = ti.split(/\|/,2)
|
|
109
|
-
t = comp[0].split(/#/,2)
|
|
112
|
+
t = comp[0].to_s.split(/#/,2)
|
|
110
113
|
@tree.last.data = Array.new
|
|
111
114
|
raise TagError if t[0].nil?
|
|
112
115
|
@tree.last.data[0] = t[0]
|
data/lib/redel/parser.rb
CHANGED
|
@@ -41,12 +41,13 @@ module ReDel
|
|
|
41
41
|
attr_reader :tree # a list which pretends to be a tree :-)
|
|
42
42
|
|
|
43
43
|
class Pattern
|
|
44
|
-
attr_reader :type, :pstart, :pend, :bol
|
|
44
|
+
attr_reader :type, :pstart, :pend, :bol, :anchored
|
|
45
45
|
def initialize(type, pstart, pend, bol=false)
|
|
46
46
|
@type = type
|
|
47
47
|
@pstart = pstart
|
|
48
48
|
@pend = pend
|
|
49
49
|
@bol = bol
|
|
50
|
+
@anchored = pend.source.match?(/(?<!\[)\^/)
|
|
50
51
|
end
|
|
51
52
|
end
|
|
52
53
|
|
|
@@ -115,6 +116,8 @@ module ReDel
|
|
|
115
116
|
def gparse(text,grammar,position_in_markup=0,level=0) #{{{
|
|
116
117
|
return if text.nil?
|
|
117
118
|
s = StringScanner.new(text)
|
|
119
|
+
# patterns without an end after a position, so that it is only searched once
|
|
120
|
+
endless = Hash.new(text.bytesize+1)
|
|
118
121
|
until s.eos?
|
|
119
122
|
success = false
|
|
120
123
|
grammar.each do |pat|
|
|
@@ -124,11 +127,17 @@ module ReDel
|
|
|
124
127
|
ts = s.scan(pat.pstart)
|
|
125
128
|
te = if s.eos?
|
|
126
129
|
'' =~ pat.pend ? '' : nil
|
|
130
|
+
elsif (pat.bol && !bol) || s.pos >= endless[pat]
|
|
131
|
+
nil
|
|
127
132
|
else
|
|
128
|
-
|
|
133
|
+
found = s.scan_until(pat.pend)
|
|
134
|
+
# ^ also matches at the current position, so it has to be searched every time
|
|
135
|
+
endless[pat] = s.pos if found.nil? && !pat.anchored
|
|
136
|
+
found
|
|
129
137
|
end
|
|
130
138
|
|
|
131
|
-
|
|
139
|
+
# a match that consumes nothing would be repeated forever
|
|
140
|
+
if te.nil? || s.pos == pos
|
|
132
141
|
s.pos = pos
|
|
133
142
|
next
|
|
134
143
|
end
|
|
@@ -148,7 +157,9 @@ module ReDel
|
|
|
148
157
|
success = true
|
|
149
158
|
next
|
|
150
159
|
end
|
|
151
|
-
|
|
160
|
+
# inner can be shorter than ti, e.g. without the spaces at the start
|
|
161
|
+
skipped = inner ? ti.byterindex(inner).to_i : 0
|
|
162
|
+
gparse(inner,grammar('G' + pat.type),position_in_markup+pos+ts.bytesize+skipped,level+1)
|
|
152
163
|
@tree << CloseTag.new(pat.type,tpos,position_in_markup+s.pos,level)
|
|
153
164
|
ot.close = @tree.length-1
|
|
154
165
|
success = true
|
data/redel.gemspec
CHANGED
data/test/tc_parser.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require 'minitest/autorun'
|
|
2
|
+
require 'timeout'
|
|
2
3
|
require_relative '../lib/redel'
|
|
3
4
|
|
|
4
5
|
class TestParser < Minitest::Test
|
|
@@ -59,6 +60,34 @@ class TestParser < Minitest::Test
|
|
|
59
60
|
assert_equal '<b>a</b> <i>b</i>', ReDel::Simple.parse("'''a''' ''b''").output(Text.new)
|
|
60
61
|
end
|
|
61
62
|
|
|
63
|
+
class Parts < ReDel::Grammar
|
|
64
|
+
P_PART = Pattern.new('P_PART', //, /(?=#)|\z/)
|
|
65
|
+
ROOT = [ P_PART ]
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def test_match_without_progress
|
|
69
|
+
assert_equal 'a#b', Timeout.timeout(5) { Parts.parse('a#b').output(Text.new) }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
class Stripped < ReDel::Grammar
|
|
73
|
+
P_PART = Pattern.new('P_PART', /</, />/)
|
|
74
|
+
P_STRONG = Pattern.new('P_STRONG', /\*/, /\*/)
|
|
75
|
+
ROOT = [ P_PART ]
|
|
76
|
+
GP_PART = [ P_STRONG ]
|
|
77
|
+
def mP_PART(ts,ti,te) ti.strip; end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def test_position_in_markup
|
|
81
|
+
strong = Stripped.parse('ü< *a* >').tree.find { |e| e.is_a?(ReDel::CloseTag) && e.type == 'P_STRONG' }
|
|
82
|
+
assert_equal 'ü< *a*'.bytesize, strong.position_in_markup
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def test_link_with_brackets
|
|
86
|
+
link = lambda { |text| ReDel::Simple.parse(text).output(ReDel::HTML.new) }
|
|
87
|
+
assert_includes link.call('[http://a.b/c|d [e] f] g'), '<a href="http://a.b/c">d [e] f</a> g'
|
|
88
|
+
assert_includes link.call('[http://a.b/c|d [e] f'), '<a href="http://a.b/c">d [e</a> f'
|
|
89
|
+
end
|
|
90
|
+
|
|
62
91
|
def test_render
|
|
63
92
|
assert_equal 3, Language.parse('*a*').output(Tree.new)
|
|
64
93
|
end
|