redel 3.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c632614a09f0281efd217557eb046217430e45d0805dbfc33565383049081864
4
- data.tar.gz: '087a9634e4cd8f828fb67de10ea089af83ad8adbfe7bed374f5d08fab198af73'
3
+ metadata.gz: d1faeae8175d784d5bd899012c40b208a7352b10f4017341dc47b8020a1e5fae
4
+ data.tar.gz: bdb2bab92250860fc5e005a15771dae62be2ef303e49c42e144af1fb3435f5fe
5
5
  SHA512:
6
- metadata.gz: a48b94bc9a805e8d694fa4454fc523bd227dcf488c9e2b8c77eb188757063f1403b22ad58029e0c4002caac68617f122aefa775da643d1b688f00d38fff306eb
7
- data.tar.gz: fb113d924d81571d524bbed1db73307a7d523953436c96bb76629f16d47259ae0461b11c80af84c843884337c4fcf8fe9cec55e33781e3e852bdd135a2a79324
6
+ metadata.gz: 77a1704a44b018942eea028364beaa89aad7fdffa53b8eb3d152e34aa343a8c590b56437b76192a509329e7e090b909ba160724d69f4f5e07114000cc8eb5384
7
+ data.tar.gz: 1efc52fdbd32e86cc2979615a3706d3da1d3483148836b0817ea5ec3f41f59f72064ae795d5af37bb3a9606b401fae31ff3d15bc6250bfe817711fbdf7f84a08
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
- # Redel
1
+ # ReDel
2
2
 
3
- A **re**cursive **del**imiter parser for markup languages. It accepts any input: whatever
3
+ A **Re**cursive **Del**imiter Parser for markup languages. It accepts any input: whatever
4
4
  it does not recognize is plain text.
5
5
 
6
- Redel started as the parser of Didaktiki, the wiki of CEWebS, and was extracted from there.
6
+ ReDel started as the parser of Didaktiki, the wiki of CEWebS, and was extracted from there.
7
7
  Some traces are left, e.g. `CEWebS://` links in the simple grammar.
8
8
 
9
9
  It contains a grammar for simple wiki texts (headings, lists, emphasis, links, code, ...), and
@@ -12,7 +12,7 @@ an output that creates HTML. The text has to be HTML-escaped (`&`, `<`, `>`, `"`
12
12
  ```ruby
13
13
  require 'redel'
14
14
 
15
- Redel::Simple.parse("some '''bold''' text").output(Redel::HTML.new)
15
+ ReDel::Simple.parse("some '''bold''' text").output(ReDel::HTML.new)
16
16
  ```
17
17
 
18
18
  Formulas are shown as TeX without the gem `itextomml`. Plots need `gle` and ImageMagick.
@@ -22,7 +22,7 @@ Formulas are shown as TeX without the gem `itextomml`. Plots need `gle` and Imag
22
22
  ```ruby
23
23
  require 'redel'
24
24
 
25
- class Language < Redel::Grammar
25
+ class Language < ReDel::Grammar
26
26
  P_STRONG = Pattern.new('P_STRONG', /\*/, /\*/)
27
27
  P_EM = Pattern.new('P_EM', /_/, /_/)
28
28
 
@@ -30,7 +30,7 @@ class Language < Redel::Grammar
30
30
  GP_STRONG = [ P_EM ]
31
31
  end
32
32
 
33
- class Text < Redel::Output
33
+ class Text < ReDel::Output
34
34
  def mOP_STRONG(data) '<b>'; end
35
35
  def mCP_STRONG(data) '</b>'; end
36
36
  def mOP_EM(data) '<i>'; end
@@ -57,16 +57,34 @@ Language.parse('some *bold _and italic_* text').output(Text.new)
57
57
  * `mP` + name is called by `prepare`, for everything that has to be known before the output
58
58
  starts: `Language.parse(text).prepare(output).output(output)`
59
59
  * Outputs with a method `render` get the whole tree instead.
60
- * For markup inherit from `Redel::Markup`, or from `Redel::HTML` in order to
60
+ * For markup inherit from `ReDel::Markup`, or from `ReDel::HTML` in order to
61
61
  change or extend it. There is one method per pattern, which uses
62
62
  [markus](https://github.com/etm/markus) and yields for its content:
63
63
 
64
64
  ```ruby
65
- class Mine < Redel::HTML
65
+ class Mine < ReDel::HTML
66
66
  def P_STRONG(data) b_(:class => 'mine') { yield }; end
67
67
  end
68
68
  ```
69
69
 
70
+ ## Why not a PEG parser?
71
+
72
+ Robustness. Wiki texts are written by people, and contain mistakes. A PEG describes valid
73
+ input: a text that does not match is a syntax error, unless the grammar has fallback rules
74
+ for everything that can go wrong.
75
+
76
+ ReDel needs no such rules. A start without an end is plain text, and as the end of a pattern
77
+ is searched before its content is parsed, a mistake inside of a pattern can not break the
78
+ pattern around it. With the language from above:
79
+
80
+ ```ruby
81
+ Language.parse('*a _b* c_').output(Text.new)
82
+ # => <b>a _b</b> c_
83
+ ```
84
+
85
+ The price: the first end wins, even if it is part of a nested pattern. For languages where
86
+ mistakes should be reported, e.g. programming languages, a PEG parser is the better choice.
87
+
70
88
  ## License
71
89
 
72
90
  GPL-3.0-or-later, see LICENSE.
@@ -2,8 +2,11 @@ require 'open-uri'
2
2
  require_relative 'parser'
3
3
  require_relative 'xhtml'
4
4
 
5
- module Redel
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/helpers.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  require 'markus'
3
3
  require 'cgi'
4
4
 
5
- module Redel
5
+ module ReDel
6
6
  # Additions for classes that inherit from MarkUS.
7
7
  #
8
8
  # String parameters (div_ "text") are escaped by markus. Markup has to be returned
data/lib/redel/image.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # Please see the LICENSE file for copyright and distribution information
2
2
  require 'open3'
3
3
 
4
- module Redel
4
+ module ReDel
5
5
  # ImageMagick, without needing a native gem
6
6
  module Image
7
7
  # [width,height] or nil if it is not an image
@@ -12,7 +12,7 @@ rescue LoadError
12
12
  # optional, formulas are shown as TeX source without it
13
13
  end
14
14
 
15
- module Redel
15
+ module ReDel
16
16
  # Base for outputs that create markup.
17
17
  #
18
18
  # There is one method per pattern: P_X(data) or P_X(data,node). It yields, where the
data/lib/redel/parser.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # Please see the LICENSE file for copyright and distribution information
2
2
  require 'strscan'
3
3
 
4
- module Redel
4
+ module ReDel
5
5
  class OpenTag
6
6
  attr_reader :type, :level
7
7
  attr_accessor :data, :close
@@ -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
- (pat.bol && !bol) ? nil : s.scan_until(pat.pend)
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
- if te.nil?
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
- gparse(inner,grammar('G' + pat.type),position_in_markup+pos+ts.bytesize,level+1)
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/lib/redel/xhtml.rb CHANGED
@@ -1,4 +1,4 @@
1
- module Redel; module XHTML
1
+ module ReDel; module XHTML
2
2
  def unweb(string) XHTML::unweb(string) end
3
3
  def web(string) XHTML::web(string) end
4
4
  def amp(string) XHTML::amp(string) end
data/redel.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "redel"
3
- s.version = "3.0.0"
3
+ s.version = "3.1.2"
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.license = "GPL-3.0-or-later"
6
6
  s.summary = "Recursive delimiter parser for markup languages"
data/test/tc_html.rb CHANGED
@@ -2,7 +2,7 @@ require 'minitest/autorun'
2
2
  require_relative '../lib/redel'
3
3
 
4
4
  class TestHTML < Minitest::Test
5
- def simple(text) Redel::Simple.parse(text).output(Redel::HTML.new); end
5
+ def simple(text) ReDel::Simple.parse(text).output(ReDel::HTML.new); end
6
6
 
7
7
  def test_inline
8
8
  assert_equal "<div><strong>a</strong> <em>b</em> &lt;c&gt; &#8594;&#160;</div>", simple("'''a''' ''b'' &lt;c&gt; -&gt;")
data/test/tc_parser.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  require 'minitest/autorun'
2
+ require 'timeout'
2
3
  require_relative '../lib/redel'
3
4
 
4
5
  class TestParser < Minitest::Test
5
- class Language < Redel::Grammar
6
+ class Language < ReDel::Grammar
6
7
  P_STRONG = Pattern.new('P_STRONG', /\*/, /\*/)
7
8
  P_EM = Pattern.new('P_EM', /_/, /_/)
8
9
  P_LINE = Pattern.new('P_LINE', /> /, /\n|\z/, true)
@@ -16,7 +17,7 @@ class TestParser < Minitest::Test
16
17
  end
17
18
  end
18
19
 
19
- class Text < Redel::Output
20
+ class Text < ReDel::Output
20
21
  attr_reader :lines
21
22
  def mPP_LINE(data) (@lines ||= []) << data; end
22
23
  def mOP_LINE(data) "<p #{data}>"; end
@@ -56,7 +57,35 @@ class TestParser < Minitest::Test
56
57
  end
57
58
 
58
59
  def test_simple
59
- assert_equal '<b>a</b> <i>b</i>', Redel::Simple.parse("'''a''' ''b''").output(Text.new)
60
+ assert_equal '<b>a</b> <i>b</i>', ReDel::Simple.parse("'''a''' ''b''").output(Text.new)
61
+ end
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'
60
89
  end
61
90
 
62
91
  def test_render
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redel
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juergen eTM Mangler