redel 3.1.0 → 3.1.3
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/image.rb +2 -2
- data/lib/redel/output_html.rb +5 -19
- data/lib/redel/parser.rb +15 -4
- data/redel.gemspec +2 -2
- data/test/tc_parser.rb +29 -0
- metadata +3 -4
- data/lib/redel/helpers.rb +0 -44
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3db6c3e3ef22a24fdaa0efd5ae957f882f5860a2d1a360422591601ef3e7ed7f
|
|
4
|
+
data.tar.gz: 4cf62fc72f28ed9802d2d28897cd29262a088918e21a96c6554814c65229aae1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 26c41124318b255cdccb89bc760b90c2ed4ad2aa4b1d45f5230c09c79c33e419077efe7c7d78c9f8a694333e90232d82a8d04ca39166245d3b7209f323eda9b4
|
|
7
|
+
data.tar.gz: a2eb00a2ff546065a9ce977d3f0d81ec7a8b15301ddd829aa343be5e7d2170723e6ea40533421d1b70a84d02ffa674b5ca109fe04bfd8d160e2a9caaa8883822
|
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/image.rb
CHANGED
|
@@ -9,9 +9,9 @@ module ReDel
|
|
|
9
9
|
out, status = Open3.capture2('identify','-format','%w %h\n','-',:stdin_data => blob,:binmode => true,:err => File::NULL)
|
|
10
10
|
status.success? ? out.lines.first.to_s.split.map(&:to_i) : nil
|
|
11
11
|
end #}}}
|
|
12
|
-
# png, fitting into width x height, never enlarged
|
|
12
|
+
# png, fitting into width x height, never enlarged. Resized in linear light, so that thin lines keep their weight
|
|
13
13
|
def self::shrink(blob,width,height) #{{{
|
|
14
|
-
out, status = Open3.capture2('convert','-','-resize',"#{width.to_i}x#{height.to_i}>",'png:-',:stdin_data => blob,:binmode => true,:err => File::NULL)
|
|
14
|
+
out, status = Open3.capture2('convert','-','-colorspace','RGB','-resize',"#{width.to_i}x#{height.to_i}>",'-colorspace','sRGB','png:-',:stdin_data => blob,:binmode => true,:err => File::NULL)
|
|
15
15
|
status.success? ? out : nil
|
|
16
16
|
end #}}}
|
|
17
17
|
end
|
data/lib/redel/output_html.rb
CHANGED
|
@@ -4,13 +4,7 @@ require 'tmpdir'
|
|
|
4
4
|
require_relative 'parser'
|
|
5
5
|
require_relative 'xhtml'
|
|
6
6
|
require_relative 'image'
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
begin
|
|
10
|
-
require 'itextomml'
|
|
11
|
-
rescue LoadError
|
|
12
|
-
# optional, formulas are shown as TeX source without it
|
|
13
|
-
end
|
|
7
|
+
require 'markus'
|
|
14
8
|
|
|
15
9
|
module ReDel
|
|
16
10
|
# Base for outputs that create markup.
|
|
@@ -19,8 +13,6 @@ module ReDel
|
|
|
19
13
|
# content of the pattern should be. Patterns without a method only consist of their
|
|
20
14
|
# content. All texts are already escaped.
|
|
21
15
|
class Markup < MarkUS
|
|
22
|
-
include Helpers
|
|
23
|
-
|
|
24
16
|
def self::inherited(subclass) #{{{
|
|
25
17
|
super
|
|
26
18
|
subclass.template :tree do
|
|
@@ -142,7 +134,7 @@ module ReDel
|
|
|
142
134
|
space_ if blank
|
|
143
135
|
after(node,nil,1) if blank.to_i > 0 && node.level == 3
|
|
144
136
|
end
|
|
145
|
-
blank.to_i.times { div_ {
|
|
137
|
+
blank.to_i.times { div_ { space_ } }
|
|
146
138
|
end #}}}
|
|
147
139
|
# after a paragraph, nil if there is nothing to separate it from
|
|
148
140
|
def blank_lines(data,node) #{{{
|
|
@@ -204,15 +196,9 @@ module ReDel
|
|
|
204
196
|
|
|
205
197
|
def P_FORMULA1(data) formula(data); end
|
|
206
198
|
def P_FORMULA2(data,node) formula(data); end
|
|
199
|
+
# TeX with its delimiters ($...$ or \[...\]), rendered in the browser (Temml), readable without it
|
|
207
200
|
def formula(tex) #{{{
|
|
208
|
-
|
|
209
|
-
mathml = Itex2MML::Parser.new.filter(tex.gsub(/\\_/,"\\text{_}")) rescue nil
|
|
210
|
-
end
|
|
211
|
-
if mathml
|
|
212
|
-
span_ { mathml }
|
|
213
|
-
else
|
|
214
|
-
span_(:class => 'monospace') { XHTML::web(tex) }
|
|
215
|
-
end
|
|
201
|
+
span_(:class => 'formula monospace') { XHTML::web(tex) }
|
|
216
202
|
end #}}}
|
|
217
203
|
|
|
218
204
|
def P_GLE(data) #{{{
|
|
@@ -222,7 +208,7 @@ module ReDel
|
|
|
222
208
|
File.binread(name)
|
|
223
209
|
end
|
|
224
210
|
width, height = Image::size(png)
|
|
225
|
-
title =
|
|
211
|
+
title = h_!(data[/title[ \t]+["']([^"']+)["']/,1])
|
|
226
212
|
img_ :src => "data:image/png;base64,#{[png].pack('m0')}", :width => width, :height => height, :alt => title, :title => title
|
|
227
213
|
rescue SystemCallError => e
|
|
228
214
|
puts e.message # gle not installed, or an error in the plot
|
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Gem::Specification.new do |s|
|
|
2
2
|
s.name = "redel"
|
|
3
|
-
s.version = "3.1.
|
|
3
|
+
s.version = "3.1.3"
|
|
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"
|
|
@@ -18,5 +18,5 @@ Gem::Specification.new do |s|
|
|
|
18
18
|
s.email = 'juergen.mangler@gmail.com'
|
|
19
19
|
s.homepage = 'https://github.com/etm/redel'
|
|
20
20
|
|
|
21
|
-
s.add_runtime_dependency 'markus', '~> 4.
|
|
21
|
+
s.add_runtime_dependency 'markus', '~> 4.1'
|
|
22
22
|
end
|
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
|
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.1.
|
|
4
|
+
version: 3.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Juergen eTM Mangler
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - "~>"
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: '4.
|
|
18
|
+
version: '4.1'
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - "~>"
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: '4.
|
|
25
|
+
version: '4.1'
|
|
26
26
|
description: Base for grammars that consist of nested patterns with a start and an
|
|
27
27
|
end, and for their outputs. Originally the wiki parser of CEWebS.
|
|
28
28
|
email: juergen.mangler@gmail.com
|
|
@@ -37,7 +37,6 @@ files:
|
|
|
37
37
|
- Rakefile
|
|
38
38
|
- lib/redel.rb
|
|
39
39
|
- lib/redel/grammar_simple.rb
|
|
40
|
-
- lib/redel/helpers.rb
|
|
41
40
|
- lib/redel/image.rb
|
|
42
41
|
- lib/redel/output_html.rb
|
|
43
42
|
- lib/redel/parser.rb
|
data/lib/redel/helpers.rb
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
# Please see the LICENSE file for copyright and distribution information
|
|
2
|
-
require 'markus'
|
|
3
|
-
require 'cgi'
|
|
4
|
-
|
|
5
|
-
module ReDel
|
|
6
|
-
# Additions for classes that inherit from MarkUS.
|
|
7
|
-
#
|
|
8
|
-
# String parameters (div_ "text") are escaped by markus. Markup has to be returned
|
|
9
|
-
# from a block (div_ { html }) or added with text_.
|
|
10
|
-
module Helpers
|
|
11
|
-
# add markup as it is
|
|
12
|
-
def text_(markup) #{{{
|
|
13
|
-
@__markus_buffer << markup.to_s
|
|
14
|
-
nil
|
|
15
|
-
end #}}}
|
|
16
|
-
|
|
17
|
-
# everything the block adds is returned instead
|
|
18
|
-
def capture #{{{
|
|
19
|
-
start = @__markus_buffer.length
|
|
20
|
-
yield
|
|
21
|
-
@__markus_buffer.slice!(start..-1).join
|
|
22
|
-
end #}}}
|
|
23
|
-
|
|
24
|
-
# markus creates <br></br>, which are two line breaks for browsers
|
|
25
|
-
%w{br hr img input meta link param}.each do |name|
|
|
26
|
-
define_method("#{name}_") do |attrs={}|
|
|
27
|
-
mode, @__markus_mode = @__markus_mode, :xml
|
|
28
|
-
element_! name, attrs
|
|
29
|
-
@__markus_mode = mode
|
|
30
|
-
nil
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# for values inside of attributes
|
|
35
|
-
def h(value); CGI.escapeHTML(value.to_s); end
|
|
36
|
-
|
|
37
|
-
def space_; text_ ' '; end
|
|
38
|
-
|
|
39
|
-
def javascript_(code) #{{{
|
|
40
|
-
script_(:type => 'text/javascript') { "//<![CDATA[\n#{code}\n//]]>" }
|
|
41
|
-
nil
|
|
42
|
-
end #}}}
|
|
43
|
-
end
|
|
44
|
-
end
|