rsyntaxtree 1.3.1 → 1.4.0

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: d22d15ae9e17b8312dc6fdc9c37c4fe942e981854eb69589eb4b117acd1d781b
4
- data.tar.gz: 983152369ea224f73372c0e7f7754bc5db36e65fab6d20eec235757dde5a44b0
3
+ metadata.gz: 0e09c9414509d88c6bb4d1bc9b8acdb5384094e642a1fb4765c62a4ffe6a4ba0
4
+ data.tar.gz: c4f617a630db5edf5373ceeeb4c03b889cc84d548201a4746f448386db2efebe
5
5
  SHA512:
6
- metadata.gz: 7d754218424e09ebf23c84471f192f5f3192dd0a94e1b277e3c28bfed2cdcd5ca5e0cb4785fd19f37584811943cd09e0eb9a22595f2b0c321add4d417bda08c3
7
- data.tar.gz: 024f6fd854639fee40d7377509b2f36b00cab56c86929c6d2c1b833aab7c0ad77b6a519a5506a679fa315c69c8b1fe7c16bf1665e425ac4747cb6714feb67c2a
6
+ metadata.gz: 3d1e620f2662c43e3c1c66b55750ae252fa90e978ddfebed0fc5937cc8265014f3fc02aed3ee3627b4a2de264709c58e2e076884c1f0c869c8868db9dcf42237
7
+ data.tar.gz: 7f6f5d8ccb8b0f3f50363e5e1809ec32461e677458ec4b53bfbe383ef48889ed6ff8f2fda1edfc47fafcf4747857fcf260a1007d083733e20aeb4eefaf987b00
@@ -0,0 +1,6 @@
1
+ # These are supported funding model platforms
2
+
3
+ github: [yohasebe]
4
+ ko_fi: yohasebe
5
+ buy_me_a_coffee: yohasebe
6
+ # custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
data/.gitignore CHANGED
@@ -18,3 +18,4 @@ tags
18
18
  .rubocop_todo.yml
19
19
  .solargraph.yml
20
20
 
21
+ CLAUDE.md
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 4.0.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,34 @@
1
+ # Changelog
2
+
3
+ ## [1.4.0] - 2026-01
4
+
5
+ ### Added
6
+ - Per-node coloring with `@color:` syntax (named colors and hex colors)
7
+ - Penn Treebank format support with escaped parentheses (`\(`, `\)`)
8
+ - Standard input support for piping tree data
9
+ - Configuration file support (`.rsyntaxtreerc`)
10
+ - Config file validation with helpful error messages
11
+
12
+ ### Fixed
13
+ - Triangle connector now correctly draws from parent to child (not child to grandchild)
14
+
15
+ ### Documentation
16
+ - Added TikZ output limitations section
17
+ - Improved README with Features section
18
+ - Added examples for per-node coloring (054, 055, 056)
19
+
20
+ ## [1.3.2] - 2024
21
+
22
+ - Garbage collection friendly implementation
23
+
24
+ ## [1.3.1] - 2024
25
+
26
+ - Bug fixes and improvements
27
+
28
+ ## [1.3.0] - 2024
29
+
30
+ - TikZ/forest LaTeX output support
31
+
32
+ ## Previous versions
33
+
34
+ See commit history for earlier changes.
data/Gemfile CHANGED
@@ -6,3 +6,9 @@ gem "optimist"
6
6
  gem "parslet"
7
7
  gem "rmagick"
8
8
  gem "rsvg2"
9
+
10
+ group :development, :test do
11
+ gem "minitest"
12
+ gem "nokogiri"
13
+ gem "rake"
14
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2007-2026 Yoichiro Hasebe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,26 +1,76 @@
1
1
  <img src='https://github.com/yohasebe/rsyntaxtree/blob/master/img/rsyntaxtree.png?raw=true' style='width: 256px;' />
2
2
 
3
- **RSyntaxTree** is a graphical syntax tree generator for linguistic research
3
+ **RSyntaxTree** is a graphical syntax tree generator for linguistic research.
4
4
 
5
- ## Documentation
5
+ <p>
6
+ <a href="https://yohasebe.com/rsyntaxtree"><strong>Web App</strong></a> ·
7
+ <a href="https://yohasebe.github.io/rsyntaxtree/examples"><strong>Example Gallery</strong></a> ·
8
+ <a href="https://yohasebe.github.io/rsyntaxtree/documentation"><strong>Documentation</strong></a>
9
+ </p>
6
10
 
7
- Documentation is currently available in the following languages:
11
+ ## Features
8
12
 
9
- - [Documentation in English](https://yohasebe.github.io/rsyntaxtree/documentation)
10
- - [日本語ドキュメント](https://yohasebe.github.io/rsyntaxtree/documentation_ja)
13
+ RSyntaxTree provides a rich set of features for creating publication-quality syntax trees:
14
+
15
+ ### Basic Syntax
16
+
17
+ Use bracket notation to define tree structures:
18
+
19
+ ```text
20
+ [S [NP the cat] [VP [V sleeps]]]
21
+ ```
22
+
23
+ ### Text Decoration
24
+
25
+ Apply various text styles to node labels:
26
+
27
+ - **Bold**: `**text**`
28
+ - *Italic*: `*text*`
29
+ - Subscript: `_text_`
30
+ - Superscript: `__text__`
31
+ - Overline/Underline/Strikethrough: `=text=`, `-text-`, `~text~`
32
+
33
+ ### Per-Node Coloring
34
+
35
+ Apply custom colors to individual nodes using `@color:` syntax:
36
+
37
+ ```text
38
+ [S [@red:NP the cat] [@blue:VP sleeps]]
39
+ ```
40
+
41
+ Supports named colors (`red`, `blue`, `green`, etc.) and hex colors (`@#FF5733:`).
42
+
43
+ ### Enclosures and Triangles
44
+
45
+ - **Brackets**: `[#NP text]` → draws brackets around the node
46
+ - **Rectangle**: `[##NP text]` → draws a rectangle around the node
47
+ - **Triangle**: `[^NP the quick fox]` → draws a triangle connector
48
+
49
+ Combine with colors: `[#@red:NP text]`, `[^@blue:VP phrase]`
50
+
51
+ ### Path Drawing
52
+
53
+ Connect nodes with lines or arrows:
54
+
55
+ ```text
56
+ [S [NP+1 text] [VP [V+>1 connects]]]
57
+ ```
58
+
59
+ ### Multiple Output Formats
60
+
61
+ Generate trees in PNG, SVG, PDF, JPG, or GIF format.
11
62
 
12
- - [Example Gallery](https://yohasebe.github.io/rsyntaxtree/examples)
13
63
  ## Web Interface
14
64
 
15
65
  <img src='https://github.com/yohasebe/rsyntaxtree/blob/master/img/rsyntaxtree-web-screenshot.png?raw=true' width='700px'/>
16
66
 
17
- See updates and a working web interface available at <https://yohasebe.com/rsyntaxtree>.
67
+ A working web interface is available at <https://yohasebe.com/rsyntaxtree>.
18
68
 
19
- You can run RSyntaxTree's web interface on your local machine using Docker Desktop. See [RSyntaxTree Web UI](https://github.com/yohasebe/rsyntaxtree_web)
69
+ You can also run RSyntaxTree's web interface on your local machine using Docker Desktop. See [RSyntaxTree Web UI](https://github.com/yohasebe/rsyntaxtree_web).
20
70
 
21
71
  ## Examples
22
72
 
23
- See [RSyntaxTree Example Gallery](https://yohasebe.github.io/rsyntaxtree/examples) page for examples for
73
+ See [RSyntaxTree Example Gallery](https://yohasebe.github.io/rsyntaxtree/examples) for examples covering:
24
74
 
25
75
  - Generative Grammar
26
76
  - Combinatory Categorial Grammar
@@ -29,9 +79,7 @@ See [RSyntaxTree Example Gallery](https://yohasebe.github.io/rsyntaxtree/example
29
79
  - Construction Grammar
30
80
  - Pragmatics
31
81
  - Phonology
32
- - etc.
33
-
34
- **NOTE**: Some tree structures in the example gallery are experimental in the sense that they are not drawn according to conventions of the field.
82
+ - and more
35
83
 
36
84
  **Input text**
37
85
 
@@ -56,65 +104,99 @@ See [RSyntaxTree Example Gallery](https://yohasebe.github.io/rsyntaxtree/example
56
104
 
57
105
  ## Installation
58
106
 
59
- `# gem install rsyntaxtree`
107
+ ```bash
108
+ gem install rsyntaxtree
109
+ ```
60
110
 
61
- **NOTE**: Currently, installation of the gem package on macOS is not recommended. Please use the [Docker image](https://hub.docker.com/r/yohasebe/rsyntaxtree) or the [web interface](https://yohasebe.com/rsyntaxtree).
111
+ ### macOS Installation Notice
62
112
 
63
- ## Usage
113
+ **Important for macOS users:** If you encounter build errors for native extensions (`gobject-introspection`, `cairo-gobject`, `gio2`), run the following commands before installing RSyntaxTree:
64
114
 
65
- For the web interface, see Usage section of <https://yohasebe.com/rsyntaxtree>.
115
+ ```bash
116
+ gem install gobject-introspection -- --with-ldflags="-Wl,-undefined,dynamic_lookup"
117
+ gem install cairo-gobject -- --with-ldflags="-Wl,-undefined,dynamic_lookup"
118
+ gem install gio2 -- --with-ldflags="-Wl,-undefined,dynamic_lookup"
119
+ ```
66
120
 
67
- For the command-line interface, type `$rsyntaxtree -h` after installation. Here's what you get:
121
+ Then install RSyntaxTree:
68
122
 
69
- ```text
70
- RSyntaxTree, (linguistic) syntax tree generator written in Ruby.
123
+ ```bash
124
+ gem install rsyntaxtree
125
+ ```
126
+
127
+ Alternatively, use the [Docker image](https://hub.docker.com/r/yohasebe/rsyntaxtree) or the [web interface](https://yohasebe.com/rsyntaxtree).
128
+
129
+ ## Usage
130
+
131
+ ### Command Line
71
132
 
133
+ ```text
72
134
  Usage:
73
- 1) rsyntaxtree [options] "[VP [VP [V set] [NP bracket notation]] [ADV here]]"
74
- 2) rsyntaxtree [options] "/path/to/text/file"
75
- where [options] are:
76
- -o, --outdir=<s> Output directory (default: ./)
77
- -u, --outfilename=<s> Output file base name (default: syntree)
78
- -f, --format=<s> Output format: png, gif, jpg, pdf, or svg (default: png)
79
- -l, --leafstyle=<s> visual style of tree leaves: auto, triangle, bar, or nothing (default: auto)
80
- -n, --fontstyle=<s> Font style (available when ttf font is specified): sans, serif, cjk, mono (default: sans)
81
- -t, --font=<s> Path to a ttf font used to generate tree (optional)
82
- -s, --fontsize=<i> Size: 8-26 (default: 16)
83
- -i, --linewidth=<i> Size: 1-5 (default: 1)
84
- -v, --vheight=<f> Connector Height: 0.5-5.0 (default: 2.0)
85
- -c, --color=<s> Color text and bars: modern, traditional, or off (default: modern)
86
- -y, --symmetrize=<s> Generate radically symmetrical, balanced tree: on or off (default: off)
87
- -r, --transparent=<s> Make background transparent: on or off (default: off)
88
- -p, --polyline=<s> draw polyline connectors: on or off (default: off)
89
- -d, --hide-default-connectors=<s> make default connectors transparent: on or off (default: off)
90
- -h, --help This is a custom help message
91
- -e, --version Print version and exit
135
+ 1) rsyntaxtree [options] "[S [NP text] [VP here]]"
136
+ 2) rsyntaxtree [options] "(S (NP text) (VP here))" # Penn Treebank format
137
+ 3) rsyntaxtree [options] "/path/to/text/file"
138
+ 4) echo "[S [NP text] [VP here]]" | rsyntaxtree [options]
92
139
  ```
93
140
 
94
- See the [documentation](https://yohasebe.github.io/rsyntaxtree/documentation) for more detailed info about the syntax.
141
+ ### Options
95
142
 
96
- ## References
143
+ | Option | Description | Default |
144
+ |--------|-------------|---------|
145
+ | `-o, --outdir` | Output directory | `./` |
146
+ | `-f, --format` | Output format: png, gif, jpg, pdf, svg | `png` |
147
+ | `-l, --leafstyle` | Leaf style: auto, triangle, bar, nothing | `auto` |
148
+ | `-n, --fontstyle` | Font style: sans, serif, cjk, mono | `sans` |
149
+ | `-s, --fontsize` | Font size: 8-26 | `16` |
150
+ | `-c, --color` | Color mode: modern, traditional, off | `modern` |
151
+ | `-y, --symmetrize` | Symmetrical tree: on, off | `off` |
152
+ | `-p, --polyline` | Polyline connectors: on, off | `off` |
153
+
154
+ Run `rsyntaxtree -h` for the full list of options.
155
+
156
+ ### Input Formats
97
157
 
98
- Please use the following BibTeX entry when referring to RSyntaxTree.
158
+ - **Bracket notation**: `[S [NP text] [VP here]]`
159
+ - **Penn Treebank format**: `(S (NP text) (VP here))` - automatically converted
160
+ - **Standard input**: `echo "[S [NP text]]" | rsyntaxtree`
99
161
 
162
+ ### Configuration File
163
+
164
+ RSyntaxTree supports configuration files (`.rsyntaxtreerc`) in YAML format. Place the file in your home directory or current working directory.
165
+
166
+ ```yaml
167
+ # ~/.rsyntaxtreerc
168
+ format: svg
169
+ color: modern
170
+ fontsize: 18
100
171
  ```
101
- @misc{rsyntaxtree_2024,
172
+
173
+ CLI options override config file settings.
174
+
175
+ ## Documentation
176
+
177
+ For detailed documentation on syntax and markup:
178
+
179
+ - [Documentation in English](https://yohasebe.github.io/rsyntaxtree/documentation)
180
+ - [日本語ドキュメント](https://yohasebe.github.io/rsyntaxtree/documentation_ja)
181
+ - [Example Gallery](https://yohasebe.github.io/rsyntaxtree/examples)
182
+
183
+ ## References
184
+
185
+ Please use the following BibTeX entry when citing RSyntaxTree:
186
+
187
+ ```bibtex
188
+ @misc{rsyntaxtree,
102
189
  author = {Yoichiro Hasebe},
103
- title = {RSyntaxTree: A graphical syntax tree image generator}
190
+ title = {RSyntaxTree: A graphical syntax tree image generator},
104
191
  url = {https://yohasebe.com/rsyntaxtree},
105
- year = {2024}
192
+ year = {2026}
106
193
  }
107
194
  ```
108
195
 
109
- ## Development
110
-
111
- For the latest updates and downloads please visit <http://github.com/yohasebe/rsyntaxtree>
112
-
113
196
  ## Author
114
197
 
115
- Yoichiro Hasebe <yohasebe@gmail.com>
198
+ Yoichiro Hasebe (<yohasebe@gmail.com>)
116
199
 
117
200
  ## License
118
201
 
119
202
  RSyntaxTree is distributed under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
120
-
data/Rakefile CHANGED
@@ -9,24 +9,58 @@ require_relative 'lib/rsyntaxtree/utils'
9
9
  # task default: "test"
10
10
 
11
11
  Rake::TestTask.new do |task|
12
- task.pattern = "test/*_test.rb"
13
- task.warning = false
12
+ task.pattern = "test/*_test.rb"
13
+ task.warning = false
14
14
  end
15
15
 
16
16
  desc "Generate SVG and PNG example images locally"
17
17
  task :generate do
18
- require_relative "dev/generate_examples"
18
+ require_relative "dev/generate_examples"
19
19
  end
20
20
 
21
21
  desc "Docker image Build"
22
22
  task :docker_build do
23
- `docker build ./ -t rsyntaxtree_devel`
23
+ `docker build ./ -t rsyntaxtree_devel`
24
24
  end
25
25
 
26
26
  desc "Generate SVG and PNG example images using Docker image"
27
27
  task :docker_generate do
28
- docpath = File.expand_path(File.join(__dir__, "docs"))
29
- `docker build ./ -t rsyntaxtree_devel`
30
- `docker run --rm -v #{docpath}:/rsyntaxtree/hostdocs rsyntaxtree_devel ruby /rsyntaxtree/dev/generate_examples.rb /rsyntaxtree/hostdocs`
31
- `cat #{docpath}/generate_examples.log`
28
+ docpath = File.expand_path(File.join(__dir__, "docs"))
29
+ `docker build ./ -t rsyntaxtree_devel`
30
+ `docker run --rm -v #{docpath}:/rsyntaxtree/hostdocs rsyntaxtree_devel ruby /rsyntaxtree/dev/generate_examples.rb /rsyntaxtree/hostdocs`
31
+ `cat #{docpath}/generate_examples.log`
32
+ end
33
+
34
+ # Add new task for macOS environment configuration
35
+
36
+ desc "Configure Bundler build options for macOS"
37
+ task :setup_macos do
38
+ require "rbconfig"
39
+ host_os = RbConfig::CONFIG['host_os']
40
+
41
+ # Check if the host OS is macOS (Darwin)
42
+
43
+ if host_os =~ /darwin/
44
+ puts "macOS detected. Setting up build options for native extensions..."
45
+
46
+ gems_with_options = {
47
+ "gobject-introspection" => '--with-ldflags=-Wl,-undefined,dynamic_lookup',
48
+ "cairo-gobject" => '--with-ldflags=-Wl,-undefined,dynamic_lookup',
49
+ "gio2" => '--with-ldflags=-Wl,-undefined,dynamic_lookup'
50
+ }
51
+
52
+ # Configure each gem with the necessary ldflags option using Bundler config command
53
+
54
+ gems_with_options.each do |gem_name, flags|
55
+ command = "bundle config build.#{gem_name} \"#{flags}\""
56
+ puts "Executing: #{command}"
57
+ unless system(command)
58
+ abort("Failed to execute command: #{command}")
59
+ end
60
+ end
61
+
62
+ puts "macOS setup complete. Please run 'bundle install' after this."
63
+ else
64
+ puts "This setup task is intended for macOS environments. Current OS: #{host_os}"
65
+ end
32
66
  end
data/bin/rsyntaxtree CHANGED
@@ -2,9 +2,85 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'optimist'
5
+ require 'yaml'
5
6
 
6
7
  require_relative '../lib/rsyntaxtree'
7
8
  require_relative '../lib/rsyntaxtree/utils'
9
+ require_relative '../lib/rsyntaxtree/format_converter'
10
+
11
+ # Valid configuration keys and their validators
12
+ CONFIG_VALIDATORS = {
13
+ outdir: ->(v) { FileTest.directory?(v) ? nil : "must be an existing directory path" },
14
+ outfilename: ->(v) { v.is_a?(String) ? nil : "must be a string" },
15
+ format: ->(v) { /\A(png|jpg|gif|pdf|svg)\z/ =~ v.to_s ? nil : "must be png, jpg, gif, pdf, or svg" },
16
+ leafstyle: ->(v) { /\A(auto|triangle|bar|nothing)\z/ =~ v.to_s ? nil : "must be auto, triangle, bar, or nothing" },
17
+ fontstyle: ->(v) { /\A(sans|serif|cjk|mono)\z/ =~ v.to_s ? nil : "must be sans, serif, cjk, or mono" },
18
+ font: ->(v) { v.nil? || File.exist?(v) ? nil : "must be path to an existing ttf font" },
19
+ fontsize: ->(v) { v.is_a?(Integer) && v >= 8 && v <= 26 ? nil : "must be in the range of 8-26" },
20
+ linewidth: ->(v) { v.is_a?(Integer) && v >= 1 && v <= 5 ? nil : "must be in the range of 1-5" },
21
+ vheight: ->(v) { v.is_a?(Numeric) && v >= 0.5 && v <= 5.0 ? nil : "must be in the range of 0.5-5.0" },
22
+ color: ->(v) { /\A(modern|traditional|on|off|true|false)\z/ =~ v.to_s ? nil : "must be modern, traditional, or off" },
23
+ symmetrize: ->(v) { /\A(on|off|true|false)\z/ =~ v.to_s ? nil : "must be on or off" },
24
+ transparent: ->(v) { /\A(on|off|true|false)\z/ =~ v.to_s ? nil : "must be on or off" },
25
+ polyline: ->(v) { /\A(on|off|true|false)\z/ =~ v.to_s ? nil : "must be on or off" },
26
+ hide_default_connectors: ->(v) { /\A(on|off|true|false)\z/ =~ v.to_s ? nil : "must be on or off" }
27
+ }.freeze
28
+
29
+ # Validate configuration and return errors/warnings
30
+ def validate_config(config, source_path)
31
+ errors = []
32
+ warnings = []
33
+
34
+ config.each do |key, value|
35
+ key_sym = key.to_sym
36
+ if CONFIG_VALIDATORS.key?(key_sym)
37
+ error_msg = CONFIG_VALIDATORS[key_sym].call(value)
38
+ if error_msg
39
+ errors << "Error in #{source_path}: '#{key}' #{error_msg} (got: #{value.inspect})"
40
+ end
41
+ else
42
+ warnings << "Warning: unknown option '#{key}' in #{source_path}"
43
+ end
44
+ end
45
+
46
+ { errors: errors, warnings: warnings }
47
+ end
48
+
49
+ # Load configuration from .rsyntaxtreerc files
50
+ def load_config
51
+ config = {}
52
+ all_errors = []
53
+ all_warnings = []
54
+
55
+ config_files = [
56
+ File.join(Dir.home, '.rsyntaxtreerc'),
57
+ File.join(Dir.pwd, '.rsyntaxtreerc')
58
+ ]
59
+
60
+ config_files.each do |path|
61
+ if File.exist?(path)
62
+ file_config = YAML.safe_load(File.read(path), symbolize_names: true) || {}
63
+ validation = validate_config(file_config, path)
64
+ all_errors.concat(validation[:errors])
65
+ all_warnings.concat(validation[:warnings])
66
+ config.merge!(file_config)
67
+ end
68
+ end
69
+
70
+ { config: config, errors: all_errors, warnings: all_warnings }
71
+ end
72
+
73
+ loaded = load_config
74
+ file_config = loaded[:config]
75
+
76
+ # Show warnings for unknown keys
77
+ loaded[:warnings].each { |w| warn w }
78
+
79
+ # Exit with errors for invalid values
80
+ unless loaded[:errors].empty?
81
+ loaded[:errors].each { |e| warn e }
82
+ exit 1
83
+ end
8
84
 
9
85
  opts = Optimist.options do
10
86
  version "RSyntaxTree #{RSyntaxTree::VERSION} (c) 2024 Yoichiro Hasebe"
@@ -12,14 +88,16 @@ opts = Optimist.options do
12
88
  RSyntaxTree, (linguistic) syntax tree generator written in Ruby.
13
89
 
14
90
  Usage:
15
- 1) rsyntaxtree [options] "[VP [VP [V set] [NP bracket notation]] [ADV here]]"
16
- 2) rsyntaxtree [options] "/path/to/text/file"
91
+ 1) rsyntaxtree [options] "[S [NP text] [VP here]]"
92
+ 2) rsyntaxtree [options] "(S (NP text) (VP here))" # Penn TreeBank format
93
+ 3) rsyntaxtree [options] "/path/to/text/file"
94
+ 4) echo "[S [NP text] [VP here]]" | rsyntaxtree [options]
17
95
  where [options] are:
18
96
  BANNER
19
97
 
20
98
  opt :outdir, "Output directory", default: "./"
21
99
  opt :outfilename, "Output file base name", default: "syntree"
22
- opt :format, "Output format: png, pdf, or svg", default: "png"
100
+ opt :format, "Output format: png, jpg, gif, pdf, or svg", default: "png"
23
101
  opt :leafstyle, "visual style of tree leaves: auto, triangle, bar, or nothing", default: "auto"
24
102
  opt :fontstyle, "Font style (available when ttf font is specified): sans, serif, cjk, mono", default: "sans"
25
103
  opt :font, "Path to a ttf font used to generate tree (optional)", type: String
@@ -48,17 +126,38 @@ Optimist.die :transparent, "must be either on or off" unless /\A(on|off|true|fal
48
126
  Optimist.die :polyline, "must be either on or off" unless /\A(on|off|true|false)\z/ =~ opts[:polyline]
49
127
  Optimist.die :hide_default_connectors, "must be either on or off" unless /\A(on|off|true|false)\z/ =~ opts[:hide_default_connectors]
50
128
 
129
+ # Merge config file options with CLI options (CLI takes precedence)
51
130
  string_opts = {}
131
+
132
+ # First, apply config file defaults
133
+ file_config.each do |key, value|
134
+ string_opts[key.to_sym] = value
135
+ end
136
+
137
+ # Then, apply CLI options (overrides config file)
52
138
  opts.each do |key, value|
53
- string_opts[key.to_sym] = value unless key == :font && !value
139
+ key_sym = key.to_sym
140
+ # Skip internal optimist keys and nil font
141
+ next if key.to_s.start_with?('_') || (key == :font && !value)
142
+ # Only override if explicitly given on CLI or not in config
143
+ if opts[:"#{key}_given"] || !file_config.key?(key_sym)
144
+ string_opts[key_sym] = value
145
+ end
54
146
  end
55
147
 
56
148
  data = if ARGV[0] && File.exist?(File.expand_path(ARGV[0]))
57
149
  File.read ARGV[0]
58
- else
150
+ elsif ARGV[0]
59
151
  ARGV[0]
152
+ elsif !$stdin.tty?
153
+ $stdin.read
154
+ else
155
+ nil
60
156
  end
61
157
 
158
+ # Auto-convert Penn TreeBank format to bracket notation
159
+ data = RSyntaxTree::FormatConverter.to_bracket(data) if data
160
+
62
161
  begin
63
162
  RSyntaxTree::RSGenerator.check_data(data)
64
163
  rescue RSTError => e
@@ -72,20 +171,9 @@ begin
72
171
  ext = string_opts[:format]
73
172
  outfilename = string_opts[:outfilename] || "syntree"
74
173
  filepath = File.expand_path(string_opts[:outdir]) + "/#{outfilename}." + ext
75
- case ext
76
- when "pdf"
77
- outfile = File.new(filepath, "wb")
78
- outfile.write rsg.draw_pdf
79
- outfile.close
80
- when "png"
81
- outfile = File.new(filepath, "wb")
82
- outfile.write rsg.draw_png
83
- outfile.close
84
- when "svg"
85
- outfile = File.new(filepath, "wb")
86
- outfile.write rsg.draw_svg
87
- outfile.close
88
- end
174
+ draw_method = "draw_#{ext}"
175
+ output = rsg.send(draw_method)
176
+ File.binwrite(filepath, output)
89
177
  rescue RSTError => e
90
178
  puts e
91
179
  exit
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: "016"
3
- caption: "Tick-tac-toe"
3
+ caption: "Tic-tac-toe"
4
4
  color: "off"
5
5
  category: "Miscellaneous"
6
6
  polyline: "off"
@@ -13,29 +13,22 @@ font: "Noto Serif"
13
13
  ---
14
14
  ```
15
15
  [expr
16
- [id x
17
-
18
- ]
16
+ [id x]
19
17
  [suffix
20
18
  \[
21
- [id 2
22
-
23
- ]
19
+ [id 2 ]
24
20
  \]
25
21
  [suffix
26
22
  \[
27
- [id 3
28
-
29
- ]
23
+ [id 3 ]
30
24
  \]
31
25
  [suffix
32
26
  \[
33
- [id 4
34
-
35
- ]
27
+ [id 4 ]
36
28
  \]
37
29
  ]
38
30
  ]
39
31
  ]
40
32
  ]
33
+
41
34
  ```
@@ -0,0 +1,22 @@
1
+ ---
2
+ name: "054"
3
+ color: "on"
4
+ caption: "Per-node coloring (named colors)"
5
+ category: "Miscellaneous"
6
+ polyline: "off"
7
+ symmetrization: "off"
8
+ connector: "auto"
9
+ connector_height: "2.0"
10
+ linewidth: "1"
11
+ hide_default_connectors: "off"
12
+ font: "Noto Sans"
13
+ ---
14
+ ```
15
+ [S
16
+ [@red:NP the dog]
17
+ [@blue:VP
18
+ [@green:V runs]
19
+ [@orange:Adv quickly]
20
+ ]
21
+ ]
22
+ ```
@@ -0,0 +1,24 @@
1
+ ---
2
+ name: "055"
3
+ color: "on"
4
+ caption: "Per-leaf coloring (hex colors)"
5
+ category: "Miscellaneous"
6
+ polyline: "off"
7
+ symmetrization: "off"
8
+ connector: "auto"
9
+ connector_height: "2.0"
10
+ linewidth: "1"
11
+ hide_default_connectors: "off"
12
+ font: "Noto Sans"
13
+ ---
14
+ ```
15
+ [S
16
+ [NP
17
+ [Det @#E63946:the]
18
+ [N @#457B9D:cat]
19
+ ]
20
+ [VP
21
+ [V @#2A9D8F:sleeps]
22
+ ]
23
+ ]
24
+ ```