rsyntaxtree 0.8.6 → 0.8.8
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/bin/rsyntaxtree +4 -1
- data/lib/rsyntaxtree/element.rb +3 -3
- data/lib/rsyntaxtree/svg_graph.rb +24 -13
- data/lib/rsyntaxtree/tree_graph.rb +22 -13
- data/lib/rsyntaxtree/version.rb +1 -1
- data/lib/rsyntaxtree.rb +20 -19
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1d573e0201c4374f3fc497b12eb34856fcedeb3513b7d17e403c3d85dbb4d1c
|
4
|
+
data.tar.gz: 60b26c37bd71b6f89f6a048a8e594945783d2486def18e4b9b170e89a68bf91c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9bc21b39ead52c96d917d799354b84b1c99c1f9d0a7078611e2213086d31d16c3bbb416ce856c874a0563a018b2df8452da1bdaad056348c86905d7a914a40f
|
7
|
+
data.tar.gz: e44c03daee5602becb706539f4035d8272d6096d1a26a7e79b0b0c0e439e579a24b85d04717bc1794704c19df24f47d0f95b6f2f05c66abe955f568ac52b189c
|
data/bin/rsyntaxtree
CHANGED
@@ -37,6 +37,8 @@ EOS
|
|
37
37
|
:default => "on"
|
38
38
|
opt :autosub, "Put subscript numbers to nodes: on or off",
|
39
39
|
:default => "off"
|
40
|
+
opt :transparent, "Make background transparent: on or off",
|
41
|
+
:default => "off"
|
40
42
|
end
|
41
43
|
|
42
44
|
Optimist::die :outdir, "must be an exsting directory path" unless FileTest::directory?(opts[:outdir])
|
@@ -48,8 +50,9 @@ Optimist::die :fontsize, "must be in the range of 8-26" unless opts[:fontsize] >
|
|
48
50
|
Optimist::die :color, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:color]
|
49
51
|
Optimist::die :symmetrize, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:symmetrize]
|
50
52
|
Optimist::die :autosub, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:autosub]
|
51
|
-
Optimist::die :margin, "must be in the range of
|
53
|
+
Optimist::die :margin, "must be in the range of 0-5" if opts[:margin] < 1 || opts[:margin] > 5
|
52
54
|
Optimist::die :vheight, "must be in the range of 0.5-5.0" if opts[:vheight] < 0.5 || opts[:vheight] > 5.0
|
55
|
+
Optimist::die :transparent, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:transparent]
|
53
56
|
|
54
57
|
string_opts = {}
|
55
58
|
opts.each do |key, value|
|
data/lib/rsyntaxtree/element.rb
CHANGED
@@ -23,9 +23,9 @@ class Element
|
|
23
23
|
@level = level # Element level in the tree (0=top etc...)
|
24
24
|
@width = 0 # Width of the element in pixels
|
25
25
|
@indent = 0 # Drawing offset
|
26
|
-
|
27
|
-
if /\A.+\^\z/ =~ content
|
28
|
-
@content = content.gsub("^"){""}
|
26
|
+
content = content.strip
|
27
|
+
if /\A.+\^\z/ =~ content
|
28
|
+
@content = content.gsub("^"){""} # The actual element content
|
29
29
|
@triangle = true # draw triangle instead of stright bar when in auto mode
|
30
30
|
else
|
31
31
|
@content = content.gsub("^"){""}.strip # The actual element content
|
@@ -18,17 +18,20 @@ require 'graph'
|
|
18
18
|
|
19
19
|
class SVGGraph < Graph
|
20
20
|
|
21
|
-
def initialize(e_list, metrics, symmetrize, color, leafstyle, multibyte, fontstyle, font, font_cjk, font_size)
|
21
|
+
def initialize(e_list, metrics, symmetrize, color, leafstyle, multibyte, fontstyle, font, font_cjk, font_size, margin, transparent)
|
22
22
|
|
23
23
|
# Store class-specific parameters
|
24
24
|
@font = multibyte ? font_cjk : font
|
25
25
|
@font_size = font_size
|
26
|
+
@transparent = transparent
|
27
|
+
|
26
28
|
case fontstyle
|
27
29
|
when /(?:sans|cjk)/
|
28
30
|
@fontstyle = "sans-serif"
|
29
31
|
when /(?:serif|math)/
|
30
32
|
@fontstyle = "serif"
|
31
33
|
end
|
34
|
+
@margin = margin.to_i
|
32
35
|
|
33
36
|
super(e_list, metrics, symmetrize, color, leafstyle, multibyte, @font, @font_size)
|
34
37
|
|
@@ -38,17 +41,35 @@ class SVGGraph < Graph
|
|
38
41
|
@tree_data = String.new
|
39
42
|
end
|
40
43
|
|
44
|
+
def get_left_most(tree_data)
|
45
|
+
xs = @tree_data.scan(/x1?=['"]([^'"]+)['"]/).map{|m| m.first.to_i}
|
46
|
+
xs.min
|
47
|
+
end
|
48
|
+
|
41
49
|
def svg_data
|
42
50
|
parse_list
|
51
|
+
lm = get_left_most(@tree_data)
|
52
|
+
width = @width - lm + @margin * 2
|
53
|
+
height = @height + @margin * 2
|
54
|
+
|
43
55
|
header =<<EOD
|
44
56
|
<?xml version="1.0" standalone="no"?>
|
45
57
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
|
46
58
|
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
47
|
-
<svg width="#{
|
59
|
+
<svg width="#{width}" height="#{height}" viewBox="#{-@margin + lm}, -#{@margin}, #{@width - lm + @margin * 2}, #{@height + @margin * 2}" version="1.1" xmlns="http://www.w3.org/2000/svg">
|
60
|
+
EOD
|
61
|
+
|
62
|
+
rect =<<EOD
|
63
|
+
<rect x="#{-@margin + lm}" y="-#{@margin}" width="#{@width - lm + @margin * 2}" height="#{@height + @margin * 2}" stroke="none" fill="white" />"
|
48
64
|
EOD
|
49
65
|
|
50
66
|
footer = "</svg>"
|
51
|
-
|
67
|
+
|
68
|
+
if @transparent
|
69
|
+
header + @tree_data + footer
|
70
|
+
else
|
71
|
+
header + rect + @tree_data + footer
|
72
|
+
end
|
52
73
|
end
|
53
74
|
|
54
75
|
# Create a temporary file and returns only its filename
|
@@ -287,19 +308,9 @@ EOD
|
|
287
308
|
main_before = parts[0].strip
|
288
309
|
sub = parts[1]
|
289
310
|
main = get_txt_only(main_before)
|
290
|
-
# if(main.contains_cjk?)
|
291
|
-
# main = 'n' * main.strip.size * 2
|
292
|
-
# else
|
293
|
-
# main
|
294
|
-
# end
|
295
311
|
main_metrics = img_get_txt_metrics(main, font, font_size, multiline)
|
296
312
|
width = main_metrics.width
|
297
313
|
if sub
|
298
|
-
# if(sub.contains_cjk?)
|
299
|
-
# sub = 'n' * sub.strip.size * 2
|
300
|
-
# else
|
301
|
-
# sub
|
302
|
-
# end
|
303
314
|
sub_metrics = img_get_txt_metrics(sub, font, font_size * SUBSCRIPT_CONST, multiline)
|
304
315
|
width += sub_metrics.width
|
305
316
|
end
|
@@ -21,18 +21,19 @@ class TreeGraph < Graph
|
|
21
21
|
|
22
22
|
def initialize(e_list, metrics, symmetrize, color, leafstyle, multibyte,
|
23
23
|
fontstyle, font, font_it, font_bd, font_itbd, font_math, font_cjk, font_size,
|
24
|
-
margin)
|
24
|
+
margin, transparent)
|
25
25
|
|
26
26
|
# Store class-specific parameters
|
27
|
-
@fontstyle
|
28
|
-
@font
|
29
|
-
@font_size
|
30
|
-
@font_it
|
31
|
-
@font_bd
|
32
|
-
@font_itbd
|
33
|
-
@font_math
|
34
|
-
@font_cjk
|
35
|
-
@margin
|
27
|
+
@fontstyle = fontstyle
|
28
|
+
@font = multibyte ? font_cjk : font
|
29
|
+
@font_size = font_size
|
30
|
+
@font_it = font_it
|
31
|
+
@font_bd = font_bd
|
32
|
+
@font_itbd = font_itbd
|
33
|
+
@font_math = font_math
|
34
|
+
@font_cjk = font_cjk
|
35
|
+
@margin = margin
|
36
|
+
@transparent = transparent
|
36
37
|
|
37
38
|
super(e_list, metrics, symmetrize, color, leafstyle, multibyte, @font, @font_size)
|
38
39
|
|
@@ -49,6 +50,9 @@ class TreeGraph < Graph
|
|
49
50
|
def draw
|
50
51
|
parse_list
|
51
52
|
@im = Image.new(@width, @height)
|
53
|
+
if @transparent
|
54
|
+
@im.matte_reset!
|
55
|
+
end
|
52
56
|
@im.interlace = PlaneInterlace
|
53
57
|
@gc.draw(@im)
|
54
58
|
end
|
@@ -63,7 +67,11 @@ class TreeGraph < Graph
|
|
63
67
|
def to_blob(fileformat='PNG')
|
64
68
|
draw
|
65
69
|
@im.trim!
|
66
|
-
|
70
|
+
if @transparent
|
71
|
+
@im.border!(@margin, @margin, "transparent")
|
72
|
+
else
|
73
|
+
@im.border!(@margin, @margin, "white")
|
74
|
+
end
|
67
75
|
@im.format = fileformat
|
68
76
|
@im.interlace = PlaneInterlace
|
69
77
|
return @im.to_blob
|
@@ -73,6 +81,7 @@ class TreeGraph < Graph
|
|
73
81
|
|
74
82
|
# Add the element into the tree (draw it)
|
75
83
|
def draw_element(x, y, w, string, type)
|
84
|
+
numlines = string.count("\n")
|
76
85
|
string = string.sub(/\^\z/){""}
|
77
86
|
# Calculate element dimensions and position
|
78
87
|
if (type == ETYPE_LEAF) and @leafstyle == "nothing"
|
@@ -81,7 +90,7 @@ class TreeGraph < Graph
|
|
81
90
|
top = row2px(y)
|
82
91
|
end
|
83
92
|
left = x + @m[:b_side]
|
84
|
-
bottom = top + @e_height
|
93
|
+
bottom = top + @e_height * numlines
|
85
94
|
right = left + w
|
86
95
|
|
87
96
|
# Split the string into the main part and the
|
@@ -284,7 +293,7 @@ class TreeGraph < Graph
|
|
284
293
|
end
|
285
294
|
end
|
286
295
|
|
287
|
-
def img_get_txt_width(text, font, font_size, multiline =
|
296
|
+
def img_get_txt_width(text, font, font_size, multiline = true)
|
288
297
|
parts = text.split("_", 2)
|
289
298
|
main_before = parts[0].strip
|
290
299
|
sub = parts[1]
|
data/lib/rsyntaxtree/version.rb
CHANGED
data/lib/rsyntaxtree.rb
CHANGED
@@ -49,7 +49,7 @@ class RSGenerator
|
|
49
49
|
.gsub('-CABRACKET-', '>')
|
50
50
|
new_params[key] = data
|
51
51
|
new_params[:multibyte] = data.contains_cjk?
|
52
|
-
when :symmetrize, :color, :autosub
|
52
|
+
when :symmetrize, :color, :autosub, :transparent
|
53
53
|
new_params[key] = value && value != "off" ? true : false
|
54
54
|
when :fontsize
|
55
55
|
new_params[key] = value.to_i * FONT_SCALING
|
@@ -98,22 +98,23 @@ class RSGenerator
|
|
98
98
|
|
99
99
|
# defaults to the following
|
100
100
|
@params = {
|
101
|
-
:symmetrize
|
102
|
-
:color
|
103
|
-
:autosub
|
104
|
-
:
|
105
|
-
:
|
106
|
-
:
|
107
|
-
:
|
108
|
-
:
|
109
|
-
:
|
110
|
-
:
|
111
|
-
:
|
112
|
-
:
|
113
|
-
:
|
114
|
-
:
|
115
|
-
:
|
116
|
-
:
|
101
|
+
:symmetrize => true,
|
102
|
+
:color => true,
|
103
|
+
:autosub => false,
|
104
|
+
:transparent => false,
|
105
|
+
:fontsize => 18,
|
106
|
+
:format => "png",
|
107
|
+
:leafstyle => "auto",
|
108
|
+
:filename => "syntree",
|
109
|
+
:data => "",
|
110
|
+
:margin => 0,
|
111
|
+
:vheight => 1.0,
|
112
|
+
:fontstyle => "sans",
|
113
|
+
:font => "/NotoSansCJKjp-Regular.otf",
|
114
|
+
:font_it => "/NotoSans-Italic.ttf",
|
115
|
+
:font_bd => "/NotoSans-Bold.ttf",
|
116
|
+
:font_bdit => "/NotoSans-BoldItalic.ttf",
|
117
|
+
:font_math => "/NotoSansMath-Regular.ttf"
|
117
118
|
}
|
118
119
|
@metrics = {
|
119
120
|
:e_width => 120,
|
@@ -163,7 +164,7 @@ class RSGenerator
|
|
163
164
|
elist = sp.get_elementlist
|
164
165
|
graph = SVGGraph.new(elist, @metrics,
|
165
166
|
@params[:symmetrize], @params[:color], @params[:leafstyle], @params[:multibyte],
|
166
|
-
@params[:fontstyle], @params[:font], @params[:font_cjk], @params[:fontsize],
|
167
|
+
@params[:fontstyle], @params[:font], @params[:font_cjk], @params[:fontsize], @params[:margin], @params[:transparent]
|
167
168
|
)
|
168
169
|
graph.svg_data
|
169
170
|
end
|
@@ -176,7 +177,7 @@ class RSGenerator
|
|
176
177
|
graph = TreeGraph.new(elist, @metrics,
|
177
178
|
@params[:symmetrize], @params[:color], @params[:leafstyle], @params[:multibyte],
|
178
179
|
@params[:fontstyle], @params[:font], @params[:font_it], @params[:font_bd], @params[:font_bdit], @params[:font_math],
|
179
|
-
@params[:font_cjk], @params[:fontsize], @params[:margin],
|
180
|
+
@params[:font_cjk], @params[:fontsize], @params[:margin], @params[:transparent]
|
180
181
|
)
|
181
182
|
graph.to_blob(@params[:format])
|
182
183
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rsyntaxtree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoichiro Hasebe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-12-
|
11
|
+
date: 2021-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rmagick
|