rsyntaxtree 0.7.1 → 0.7.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
  SHA1:
3
- metadata.gz: 5801173697da7daaf1e8f8741b7af0cf6c296a6a
4
- data.tar.gz: 96806aede17441ce30718cfc983a4883166787f7
3
+ metadata.gz: 4579e09d625fbfed3bf8415818f1d3e1faae5f1c
4
+ data.tar.gz: b8541960fc98bc992340960990bf53a36a73730d
5
5
  SHA512:
6
- metadata.gz: 50f211f9a4d650270c502d5feafd69abe441becc400aba6df7f38c512ab8f2fb03af37ea983776566a907b5c3d7ffee9074bc24be9b0524c1d5443498212080a
7
- data.tar.gz: 5d38af416912e8b362713d771a5a48c484d123b053edd386fdb6f191e2e9d1677228fd51190bf9e312c01f496acef83fdac958db3e55808b8bfb91f143508b51
6
+ metadata.gz: fa4628dc3690b7532d80ca4d093d12fef301a8054ab034d9ea8b45302e802c65ac96d3cc2ce12edba514d439d75f6851e8d4297c50e042c9a2b1e2a56f7c9735
7
+ data.tar.gz: a11e485de6a1cdc3f11be9d75e0177b6b7f46ec6fbc1b6a23ba827227e593610a99206c26271f6dd4b2cc194316cb41bbefdab7eb872c7b44c4f97bab96bfdc0
data/README.md CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  **RSyntaxTree** is a graphical syntax tree generator written in the Ruby programming language inspired by [phpSyntaxTree](http://ironcreek.net/phpsyntaxtree/).
4
4
 
5
- - - -
6
-
7
5
  ### Web Interface
8
6
 
9
7
  Working web interface of is available at <https://yohasebe.com/rsyntaxtree>.
@@ -30,11 +28,12 @@ where [options] are:
30
28
  -n, --fontstyle=<s> Font style (available when ttf font is specified): sans, serif, mono, cjk (default: sans)
31
29
  -t, --font=<s> Path to a ttf font used to generate tree (optional)
32
30
  -s, --fontsize=<i> Size: 8-26 (default: 16)
33
- -m, --margin=<i> Margin: 0-120 (default: 0)
34
31
  -c, --color=<s> Color text and bars: on or off (default: on)
35
32
  -y, --symmetrize=<s> Generate symmetrical, balanced tree: on or off (default: on)
36
33
  -a, --autosub=<s> Put subscript numbers to nodes: on or off (default: off)
37
- -v, --version Print version and exit
34
+ -m, --margin=<i> Margin: 0-120 (default: 0)
35
+ -v, --vheight=<f> Connector Height: 0.5-2.0 (default: 0.5)
36
+ -e, --version Print version and exit
38
37
  -h, --help Show this message
39
38
  ```
40
39
 
@@ -51,12 +50,12 @@ You can put a subscript to any node by putting the _ character between the main
51
50
  Bracket notation (auto-subscript-on):
52
51
 
53
52
  ```text
54
- [S [NP RSyntaxTree^][VP [V generates][NP multilingual syntax trees]]]
53
+ [S [NP RSyntaxTree^][VP [V generates][NP [Adj multilingual] [NP syntax trees]]]]
55
54
  ```
56
55
 
57
56
  Resulting PNG
58
57
 
59
- ![RSyntaxTree generates multilingual syntax trees](https://i.gyazo.com/8d8cbce1e8d8d45c0dda0741009641fd.png)
58
+ ![RSyntaxTree generates multilingual syntax trees](https://i.gyazo.com/6bb68b0bdb35d7a10c4a11d5788d484f.png)
60
59
 
61
60
  ### Development
62
61
 
@@ -18,7 +18,7 @@ EOS
18
18
 
19
19
  opt :outdir, "Output directory",
20
20
  :default => "./"
21
- opt :format, "Output format: png, pdf, or svg",
21
+ opt :format, "Output format: png, gif, jpg, pdf, or svg",
22
22
  :default => "png"
23
23
  opt :leafstyle, "visual style of tree leaves: auto, triangle, bar, or nothing",
24
24
  :default => "auto"
@@ -29,6 +29,8 @@ EOS
29
29
  :default => 16
30
30
  opt :margin, "Margin: 0-120",
31
31
  :default => 0
32
+ opt :vheight, "Connector Height: 0.5-2.0",
33
+ :default => 1.0
32
34
  opt :color, "Color text and bars: on or off",
33
35
  :default => "on"
34
36
  opt :symmetrize, "Generate symmetrical, balanced tree: on or off",
@@ -38,15 +40,16 @@ EOS
38
40
  end
39
41
 
40
42
  Trollop::die :outdir, "must be an exsting directory path" unless FileTest::directory?(opts[:outdir])
41
- Trollop::die :format, "must be png or svg" unless /\A(png|pdf|svg)\z/ =~ opts[:format]
43
+ Trollop::die :format, "must be png, jpg, gif, or svg" unless /\A(png|jpg|gif|pdf|svg)\z/ =~ opts[:format]
42
44
  Trollop::die :leafstyle, "must be auto, triangle, bar, or nothing" unless /\A(auto|triangle|bar|nothing)\z/ =~ opts[:leafstyle]
43
45
  Trollop::die :fontstyle, "must be sans, serif, mono, cjk" unless /\A(sans|serif|mono|cjk)\z/ =~ opts[:fontstyle]
44
46
  Trollop::die :font, "must be path to an existing ttf font" if opts[:font] && !File::exists?(opts[:font])
45
- Trollop::die :fontsize, "must be in the range 8-26" unless opts[:fontsize] >= 8 && opts[:fontsize] <= 26
46
- Trollop::die :margin, "must be in the range 0-120" if opts[:margin] < 0 || opts[:margin] > 120
47
+ Trollop::die :fontsize, "must be in the range of 8-26" unless opts[:fontsize] >= 8 && opts[:fontsize] <= 26
47
48
  Trollop::die :color, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:color]
48
49
  Trollop::die :symmetrize, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:symmetrize]
49
50
  Trollop::die :autosub, "must be either on or off" unless /\A(on|off)\z/ =~ opts[:autosub]
51
+ Trollop::die :margin, "must be in the range of 0-120" if opts[:margin] < 0 || opts[:margin] > 120
52
+ Trollop::die :vheight, "must be in the range of 0.5-2.0" if opts[:vheight] < 0.5 || opts[:vheight] > 2.0
50
53
 
51
54
  string_opts = {}
52
55
  opts.each do |key, value|
@@ -68,6 +71,10 @@ outfile = File.new(File.expand_path(string_opts[:outdir]) + "/syntree." + ext, "
68
71
  case ext
69
72
  when "png"
70
73
  outfile.write rsg.draw_png
74
+ when "jpg"
75
+ outfile.write rsg.draw_jpg
76
+ when "gif"
77
+ outfile.write rsg.draw_gif
71
78
  when "pdf"
72
79
  outfile.write rsg.draw_pdf
73
80
  when "svg"
@@ -42,6 +42,10 @@ require 'pp'
42
42
 
43
43
  FONT_DIR = File.expand_path(File.dirname(__FILE__) + "/../fonts")
44
44
 
45
+ ETYPE_UNDEFINED = 0
46
+ ETYPE_NODE = 1
47
+ ETYPE_LEAF = 2
48
+
45
49
  class RSGenerator
46
50
  def initialize(params = {})
47
51
  new_params = {}
@@ -63,6 +67,8 @@ class RSGenerator
63
67
  new_params[key] = value.to_i
64
68
  when :margin
65
69
  new_params[key] = value.to_i
70
+ when :vheight
71
+ new_params[key] = value.to_f
66
72
  when :fontstyle
67
73
  if value == "noto-sans" || value == "sans"
68
74
  new_params[:font] = FONT_DIR + "/NotoSansCJKjp-Regular.otf"
@@ -92,10 +98,22 @@ class RSGenerator
92
98
  :font => FONT_DIR + "/NotoSansCKjp-Regular.otf",
93
99
  :filename => "syntree",
94
100
  :data => "",
95
- :margin => 0
101
+ :margin => 0,
102
+ :vheight => 1.0
103
+ }
104
+ @metrics = {
105
+ :e_width => 120,
106
+ :e_padd => 14,
107
+ :v_space => 20,
108
+ :h_space => 20,
109
+ :b_side => 10,
110
+ :b_topbot => 10
96
111
  }
97
112
 
98
113
  @params.merge! new_params
114
+ @params[:fontsize] = @params[:fontsize] * 2
115
+ @params[:margin] = @params[:margin] * 2
116
+ @metrics[:v_space] = @metrics[:v_space] * @params[:vheight]
99
117
  end
100
118
 
101
119
  def self.check_data(text)
@@ -108,6 +126,16 @@ class RSGenerator
108
126
  draw_tree
109
127
  end
110
128
 
129
+ def draw_jpg
130
+ @params[:format] = "jpg"
131
+ draw_tree
132
+ end
133
+
134
+ def draw_gif
135
+ @params[:format] = "gif"
136
+ draw_tree
137
+ end
138
+
111
139
  def draw_pdf
112
140
  @params[:format] = "pdf"
113
141
  draw_tree
@@ -118,7 +146,7 @@ class RSGenerator
118
146
  sp.parse
119
147
  sp.auto_subscript if @params[:autosub]
120
148
  elist = sp.get_elementlist
121
- graph = TreeGraph.new(elist,
149
+ graph = TreeGraph.new(elist, @metrics,
122
150
  @params[:symmetrize], @params[:color], @params[:leafstyle], @params[:font], @params[:fontsize], @params[:format], @params[:margin])
123
151
  graph.to_blob(@params[:format])
124
152
  end
@@ -129,7 +157,7 @@ class RSGenerator
129
157
  sp.parse
130
158
  sp.auto_subscript if @params[:autosub]
131
159
  elist = sp.get_elementlist
132
- graph = SVGGraph.new(elist,
160
+ graph = SVGGraph.new(elist, @metrics,
133
161
  @params[:symmetrize], @params[:color], @params[:leafstyle], @params[:font], @params[:fontsize])
134
162
  graph.svg_data
135
163
  end
@@ -27,10 +27,6 @@
27
27
  # along with this program; if not, write to the Free Software
28
28
  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
29
 
30
- ETYPE_UNDEFINED = 0
31
- ETYPE_NODE = 1
32
- ETYPE_LEAF = 2
33
-
34
30
  class Element
35
31
 
36
32
  attr_accessor :id, :parent, :type, :content, :level, :width, :indent, :triangle
@@ -72,7 +72,7 @@ class StringParser
72
72
  return false
73
73
  end
74
74
 
75
- if /\[\_ / =~ @data
75
+ if /\[\_/ =~ @data
76
76
  return false
77
77
  end
78
78
 
@@ -33,30 +33,28 @@ include Magick
33
33
 
34
34
  # constant variables are already set in tree_graph.rb
35
35
 
36
-
37
36
  class SVGGraph
38
37
 
39
- def initialize(e_list, symmetrize = true, color = true, leafstyle = "auto",
38
+ def initialize(e_list, metrics, symmetrize = true, color = true, leafstyle = "auto",
40
39
  font = "Helvetica", font_size = 10, simple = false)
41
40
 
42
41
  # Store parameters
43
42
  @e_list = e_list
43
+ @m = metrics
44
44
  @font = font
45
- @font_size = font_size
45
+ @font_size = font_size
46
46
  @leafstyle = leafstyle
47
47
  @symmetrize = symmetrize
48
- # Element dimensions
49
- @e_width = E_WIDTH
50
48
 
51
49
 
52
50
  # Calculate image dimensions
53
- @e_height = @font_size + E_PADD * 2
54
- h = @e_list.get_level_height
55
- w = calc_level_width(0)
56
- w_px = w + B_SIDE
57
- h_px = h * @e_height + (h-1) * (V_SPACE + @font_size) + B_TOPBOT * 2
58
- @height = h_px
59
- @width = w_px
51
+ @e_height = @font_size + @m[:e_padd] * 2
52
+ h = @e_list.get_level_height
53
+ w = calc_level_width(0)
54
+ w_px = w + @m[:b_side]
55
+ h_px = h * @e_height + (h-1) * (@m[:v_space] + @font_size) + @m[:b_topbot] * 2
56
+ @height = h_px
57
+ @width = w_px
60
58
 
61
59
 
62
60
  # Initialize the image and colors
@@ -127,7 +125,7 @@ EOD
127
125
  else
128
126
  top = row2px(y)
129
127
  end
130
- left = x + B_SIDE
128
+ left = x + @m[:b_side]
131
129
  bottom = top + @e_height
132
130
  right = left + w
133
131
 
@@ -173,7 +171,7 @@ EOD
173
171
  main_data = @text_styles.sub(/COLOR/, col)
174
172
  main_data = main_data.sub(/FONT_SIZE/, @font_size.to_s)
175
173
  main_x = txt_pos
176
- main_y = top + @e_height - E_PADD
174
+ main_y = top + @e_height - @m[:e_padd]
177
175
  main_data = main_data.sub(/X_VALUE/, main_x.to_s)
178
176
  main_data = main_data.sub(/Y_VALUE/, main_y.to_s)
179
177
  @tree_data += main_data.sub(/CONTENT/, main)
@@ -182,13 +180,12 @@ EOD
182
180
  sub_data = @text_styles.sub(/COLOR/, col)
183
181
  sub_data = sub_data.sub(/FONT_SIZE/, @font_size.to_s)
184
182
  sub_x = main_x + main_width + (sub_size/8)
185
- sub_y = top + (@e_height - E_PADD + sub_size / 2).ceil
183
+ sub_y = top + (@e_height - @m[:e_padd] + sub_size / 2).ceil
186
184
  if (sub.length > 0 )
187
185
  sub_data = sub_data.sub(/X_VALUE/, sub_x.ceil.to_s)
188
186
  sub_data = sub_data.sub(/Y_VALUE/, sub_y.ceil.to_s)
189
187
  @tree_data += sub_data.sub(/CONTENT/, sub)
190
188
  end
191
-
192
189
  end
193
190
 
194
191
  # Draw a line between child/parent elements
@@ -199,9 +196,9 @@ EOD
199
196
  end
200
197
 
201
198
  fromTop = row2px(fromY)
202
- fromLeft = (fromX + fromW / 2 + B_SIDE)
199
+ fromLeft = (fromX + fromW / 2 + @m[:b_side])
203
200
  toBot = (row2px(fromY - 1 ) + @e_height)
204
- toLeft = (toX + toW / 2 + B_SIDE)
201
+ toLeft = (toX + toW / 2 + @m[:b_side])
205
202
 
206
203
  line_data = @line_styles.sub(/X1/, fromLeft.ceil.to_s.to_s)
207
204
  line_data = line_data.sub(/Y1/, fromTop.ceil.to_s.to_s)
@@ -217,7 +214,7 @@ EOD
217
214
  end
218
215
 
219
216
  toX = fromX
220
- fromCenter = (fromX + fromW / 2 + B_SIDE)
217
+ fromCenter = (fromX + fromW / 2 + @m[:b_side])
221
218
 
222
219
  fromTop = row2px(fromY).ceil
223
220
  fromLeft1 = (fromCenter + textW / 2).ceil
@@ -225,9 +222,9 @@ EOD
225
222
  toBot = (row2px(fromY - 1) + @e_height)
226
223
 
227
224
  if symmetrize
228
- toLeft = (toX + textW / 2 + B_SIDE)
225
+ toLeft = (toX + textW / 2 + @m[:b_side])
229
226
  else
230
- toLeft = (toX + textW / 2 + B_SIDE * 3)
227
+ toLeft = (toX + textW / 2 + @m[:b_side] * 3)
231
228
  end
232
229
 
233
230
  polygon_data = @polygon_styles.sub(/X1/, fromLeft1.ceil.to_s)
@@ -465,11 +462,8 @@ EOD
465
462
  end
466
463
  end
467
464
 
468
-
469
465
  # Calculate top position from row (level)
470
466
  def row2px(row)
471
- B_TOPBOT + @e_height * row + (V_SPACE + @font_size) * row
467
+ @m[:b_topbot] + @e_height * row + (@m[:v_space] + @font_size) * row
472
468
  end
473
-
474
469
  end
475
-
@@ -29,47 +29,36 @@
29
29
 
30
30
  require 'imgutils'
31
31
  require 'elementlist'
32
- # require 'rubygems'
33
32
  require 'rmagick'
34
33
  include Magick
35
34
 
36
- # Double to make image retina ready
37
- E_WIDTH = 60 * 2 # Element width
38
- E_PADD = 7 * 2 # Element height padding
39
- V_SPACE = 20 * 2
40
- H_SPACE = 10 * 2
41
- B_SIDE = 5 * 2
42
- B_TOPBOT = 5 * 2
43
-
44
35
  class TreeGraph
45
36
 
46
- def initialize(e_list, symmetrize = true, color = true, terminal = "auto",
37
+ def initialize(e_list, metrics, symmetrize = true, color = true, terminal = "auto",
47
38
  font = "Helvetica", font_size = 10, simple = false, margin = 0)
48
39
 
49
40
  # Store parameters (double font size and margin to make image retina ready)
50
41
  @e_list = e_list
42
+ @m = metrics
51
43
  @font = font
52
- @terminal = terminal
44
+ @terminal = terminal
53
45
  @symmetrize = symmetrize
54
- @simple = simple
55
- @font_size = font_size * 2
56
- @margin = margin * 2
46
+ @simple = simple
47
+ @font_size = font_size
48
+ @margin = margin
57
49
 
58
- # Element dimensions
59
- @e_width = E_WIDTH
60
-
61
50
  # Calculate image dimensions
62
- @e_height = @font_size + E_PADD * 2
63
- h = @e_list.get_level_height
64
- w = calc_level_width(0)
65
- w_px = w + B_SIDE * 2
66
- h_px = h * @e_height + (h-1) * (V_SPACE + @font_size) + B_TOPBOT * 2
67
- @height = h_px
68
- @width = w_px
51
+ @e_height = @font_size + metrics[:e_padd] * 2
52
+ h = @e_list.get_level_height
53
+ w = calc_level_width(0)
54
+ w_px = w + metrics[:b_side] * 2
55
+ h_px = h * @e_height + (h-1) * (metrics[:v_space] + @font_size) + metrics[:b_topbot] * 2
56
+ @height = h_px
57
+ @width = w_px
69
58
 
70
59
  # Initialize the image and colors
71
- @im = Image.new(w_px, h_px)
72
- @gc = Draw.new
60
+ @im = Image.new(w_px, h_px)
61
+ @gc = Draw.new
73
62
  @gc.font = @font
74
63
  @gc.pointsize(@font_size)
75
64
 
@@ -123,7 +112,7 @@ class TreeGraph
123
112
  else
124
113
  top = row2px(y)
125
114
  end
126
- left = x + B_SIDE
115
+ left = x + @m[:b_side]
127
116
  bottom = top + @e_height
128
117
  right = left + w
129
118
 
@@ -172,14 +161,14 @@ class TreeGraph
172
161
  # Draw main text
173
162
  @gc.pointsize(@font_size)
174
163
  main_x = txt_pos
175
- main_y = top + @e_height - E_PADD
164
+ main_y = top + @e_height - @m[:e_padd]
176
165
  @gc.text(main_x.ceil, main_y.ceil, main)
177
166
 
178
167
  # Draw subscript text
179
168
  if (sub.length > 0 )
180
169
  @gc.pointsize(sub_size)
181
170
  sub_x = txt_pos + main_width + (sub_size/8)
182
- sub_y = top + (@e_height - E_PADD + sub_size / 2)
171
+ sub_y = top + (@e_height - @m[:e_padd] + sub_size / 2)
183
172
  @gc.text(sub_x.ceil, sub_y.ceil, sub)
184
173
  end
185
174
 
@@ -193,9 +182,9 @@ class TreeGraph
193
182
  end
194
183
 
195
184
  fromTop = row2px(fromY)
196
- fromLeft = (fromX + fromW / 2 + B_SIDE)
185
+ fromLeft = (fromX + fromW / 2 + @m[:b_side])
197
186
  toBot = (row2px(fromY - 1 ) + @e_height)
198
- toLeft = (toX + toW / 2 + B_SIDE)
187
+ toLeft = (toX + toW / 2 + @m[:b_side])
199
188
 
200
189
  @gc.fill("none")
201
190
  @gc.stroke @col_line
@@ -210,16 +199,16 @@ class TreeGraph
210
199
  end
211
200
 
212
201
  toX = fromX
213
- fromCenter = (fromX + fromW / 2 + B_SIDE)
202
+ fromCenter = (fromX + fromW / 2 + @m[:b_side])
214
203
 
215
204
  fromTop = row2px(fromY).ceil
216
205
  fromLeft1 = (fromCenter + textW / 2).ceil
217
206
  fromLeft2 = (fromCenter - textW / 2).ceil
218
207
  toBot = (row2px(fromY - 1) + @e_height)
219
208
  if symmetrize
220
- toLeft = (toX + textW / 2 + B_SIDE)
209
+ toLeft = (toX + textW / 2 + @m[:b_side])
221
210
  else
222
- toLeft = (toX + textW / 2 + B_SIDE * 3)
211
+ toLeft = (toX + textW / 2 + @m[:b_side] * 3)
223
212
  end
224
213
 
225
214
  @gc.fill("none")
@@ -455,7 +444,6 @@ class TreeGraph
455
444
  end
456
445
 
457
446
  def row2px(row)
458
- B_TOPBOT + @e_height * row + (V_SPACE + @font_size) * row
447
+ @m[:b_topbot] + @e_height * row + (@m[:v_space] + @font_size) * row
459
448
  end
460
-
461
449
  end
@@ -1,4 +1,4 @@
1
1
  module RSyntaxTree
2
- VERSION = "0.7.1"
2
+ VERSION = "0.7.2"
3
3
  end
4
4
 
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.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoichiro Hasebe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-03 00:00:00.000000000 Z
11
+ date: 2018-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rmagick