ngworder 0.1.0 → 0.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 +4 -4
- data/README.md +3 -2
- data/lib/ngworder/version.rb +1 -1
- data/lib/ngworder.rb +60 -9
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 23598b09a95c1a5d1c7362d008a6fd720abedca91cd3c3203c785184f3e44c71
|
|
4
|
+
data.tar.gz: 980852622d530f10d99555c6f7ec15d4979882ca61b1d605cd92f19af914e5d5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b2b577059d5e7f2c6c0491f03ac5d89dda7ae0593fee619e43918c897734330b746e170370e3602b2ac7bb7f59066370a9ce56254643df3ab511129f6acc86b3
|
|
7
|
+
data.tar.gz: 2686e87281aab43bb92e6413a502bcc1fd6d733f446f3a1d8e8438790d48dae85bcdac3f985f50ddbfe5b117170b8b3e59ee9a2dbae91f22c92375fc9958a2d9
|
data/README.md
CHANGED
|
@@ -4,8 +4,7 @@ Simple CLI to extract NG words from Japanese text using a plain text rules file.
|
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
```
|
|
7
|
-
gem
|
|
8
|
-
gem install ./ngworder-0.1.0.gem
|
|
7
|
+
gem install ngworder
|
|
9
8
|
```
|
|
10
9
|
|
|
11
10
|
## Usage
|
|
@@ -13,6 +12,8 @@ gem install ./ngworder-0.1.0.gem
|
|
|
13
12
|
ngworder target.md
|
|
14
13
|
ngworder --rule=NGWORDS.txt target.md
|
|
15
14
|
ngworder --rg target.md
|
|
15
|
+
ngworder --no-line target.md
|
|
16
|
+
ngworder --color=auto target.md
|
|
16
17
|
ngworder --help
|
|
17
18
|
```
|
|
18
19
|
|
data/lib/ngworder/version.rb
CHANGED
data/lib/ngworder.rb
CHANGED
|
@@ -100,8 +100,15 @@ module Ngworder
|
|
|
100
100
|
backslashes.even?
|
|
101
101
|
end
|
|
102
102
|
|
|
103
|
-
def parse_matcher(raw)
|
|
104
|
-
trimmed =
|
|
103
|
+
def parse_matcher(raw, trim: :both)
|
|
104
|
+
trimmed = case trim
|
|
105
|
+
when :right
|
|
106
|
+
raw.rstrip
|
|
107
|
+
when :none
|
|
108
|
+
raw
|
|
109
|
+
else
|
|
110
|
+
raw.strip
|
|
111
|
+
end
|
|
105
112
|
return nil if trimmed.empty?
|
|
106
113
|
|
|
107
114
|
if trimmed.start_with?("/") && trimmed.length >= 2 && trimmed.end_with?("/")
|
|
@@ -120,14 +127,14 @@ module Ngworder
|
|
|
120
127
|
rules = []
|
|
121
128
|
|
|
122
129
|
File.readlines(path, chomp: true).each do |line|
|
|
123
|
-
content = strip_comment(line)
|
|
124
|
-
next if content.empty?
|
|
130
|
+
content = strip_comment(line)
|
|
131
|
+
next if content.strip.empty?
|
|
125
132
|
|
|
126
133
|
parts = split_unescaped_bang(content)
|
|
127
|
-
base = parse_matcher(parts.shift || "")
|
|
134
|
+
base = parse_matcher(parts.shift || "", trim: :right)
|
|
128
135
|
next unless base
|
|
129
136
|
|
|
130
|
-
excludes = parts.map { |part| parse_matcher(part) }.compact
|
|
137
|
+
excludes = parts.map { |part| parse_matcher(part, trim: :both) }.compact
|
|
131
138
|
rules << Rule.new(base, base.label, excludes)
|
|
132
139
|
end
|
|
133
140
|
|
|
@@ -218,6 +225,8 @@ module Ngworder
|
|
|
218
225
|
opts.banner = "Usage: ngworder [--rule=NGWORDS.txt] <files...>"
|
|
219
226
|
opts.on("--rule=PATH", "Rules file path (default: NGWORDS.txt)") { |value| options[:rule] = value }
|
|
220
227
|
opts.on("--rg", "Use ripgrep for literal-only prefiltering") { options[:rg] = true }
|
|
228
|
+
opts.on("--[no-]line", "Print the entire line after each match") { |value| options[:line] = value }
|
|
229
|
+
opts.on("--color=MODE", "Colorize matches (auto, always, never)") { |value| options[:color] = value }
|
|
221
230
|
opts.on("-h", "--help", "Show help") do
|
|
222
231
|
puts opts
|
|
223
232
|
return 0
|
|
@@ -240,6 +249,42 @@ module Ngworder
|
|
|
240
249
|
rules = Parser.build_rules(rule_path)
|
|
241
250
|
warn "No rules loaded from #{rule_path}" if rules.empty?
|
|
242
251
|
|
|
252
|
+
line_enabled = options.key?(:line) ? options[:line] : true
|
|
253
|
+
|
|
254
|
+
color_mode = (options[:color] || "auto").downcase
|
|
255
|
+
unless %w[auto always never].include?(color_mode)
|
|
256
|
+
warn "Invalid color mode: #{color_mode} (use auto, always, or never)"
|
|
257
|
+
return 2
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
color_enabled = case color_mode
|
|
261
|
+
when "always"
|
|
262
|
+
true
|
|
263
|
+
when "never"
|
|
264
|
+
false
|
|
265
|
+
else
|
|
266
|
+
$stdout.tty?
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
colorize_match = lambda do |text|
|
|
270
|
+
return text unless color_enabled
|
|
271
|
+
|
|
272
|
+
"\e[35m#{text}\e[0m"
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
highlight_line = lambda do |line, span|
|
|
276
|
+
return line unless color_enabled
|
|
277
|
+
|
|
278
|
+
start_idx = span[0]
|
|
279
|
+
end_idx = span[1]
|
|
280
|
+
head = line[0, start_idx]
|
|
281
|
+
mid = line[start_idx, end_idx - start_idx]
|
|
282
|
+
tail = line[end_idx..-1]
|
|
283
|
+
return line if mid.nil?
|
|
284
|
+
|
|
285
|
+
"#{head}\e[35m#{mid}\e[0m#{tail}"
|
|
286
|
+
end
|
|
287
|
+
|
|
243
288
|
found = false
|
|
244
289
|
|
|
245
290
|
argv.each do |path|
|
|
@@ -277,7 +322,9 @@ module Ngworder
|
|
|
277
322
|
|
|
278
323
|
found = true
|
|
279
324
|
col_no = span[0] + 1
|
|
280
|
-
|
|
325
|
+
match_text = colorize_match.call(span[2])
|
|
326
|
+
puts "#{path}:#{line_no}:#{col_no} #{match_text} NG:#{rule.label}"
|
|
327
|
+
puts highlight_line.call(line, span) if line_enabled
|
|
281
328
|
end
|
|
282
329
|
end
|
|
283
330
|
end
|
|
@@ -305,7 +352,9 @@ module Ngworder
|
|
|
305
352
|
|
|
306
353
|
found = true
|
|
307
354
|
col_no = span[0] + 1
|
|
308
|
-
|
|
355
|
+
match_text = colorize_match.call(span[2])
|
|
356
|
+
puts "#{path}:#{line_no}:#{col_no} #{match_text} NG:#{rule.label}"
|
|
357
|
+
puts highlight_line.call(line, span) if line_enabled
|
|
309
358
|
end
|
|
310
359
|
end
|
|
311
360
|
|
|
@@ -322,7 +371,9 @@ module Ngworder
|
|
|
322
371
|
|
|
323
372
|
found = true
|
|
324
373
|
col_no = span[0] + 1
|
|
325
|
-
|
|
374
|
+
match_text = colorize_match.call(span[2])
|
|
375
|
+
puts "#{path}:#{line_no}:#{col_no} #{match_text} NG:#{rule.label}"
|
|
376
|
+
puts highlight_line.call(line, span) if line_enabled
|
|
326
377
|
end
|
|
327
378
|
end
|
|
328
379
|
end
|