logaling-command 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +6 -0
- data/lib/logaling/command/application.rb +73 -137
- data/lib/logaling/command/pager.rb +36 -0
- data/lib/logaling/command/renderers.rb +134 -0
- data/lib/logaling/command/version.rb +1 -1
- data/lib/logaling/command.rb +2 -0
- data/lib/logaling/external_glossaries/tmx.rb +4 -4
- data/lib/logaling/external_glossary.rb +14 -14
- data/lib/logaling/glossary.rb +57 -121
- data/lib/logaling/glossary_db.rb +101 -91
- data/lib/logaling/glossary_source.rb +35 -0
- data/lib/logaling/glossary_sources/base.rb +42 -0
- data/lib/logaling/glossary_sources/glossary_csv_source.rb +33 -0
- data/lib/logaling/glossary_sources/glossary_tsv_source.rb +33 -0
- data/lib/logaling/glossary_sources/glossary_yaml_source.rb +125 -0
- data/lib/logaling/project.rb +79 -0
- data/lib/logaling/repository.rb +41 -92
- data/lib/logaling.rb +1 -0
- data/logaling-command.gemspec +1 -1
- data/spec/logaling/command_spec.rb +10 -9
- data/spec/logaling/glossary_spec.rb +7 -8
- data/spec/logaling/repository_spec.rb +11 -19
- metadata +68 -20
data/CHANGES
CHANGED
@@ -19,7 +19,9 @@ require 'thor'
|
|
19
19
|
require 'rainbow'
|
20
20
|
require 'pathname'
|
21
21
|
require "logaling/repository"
|
22
|
+
require "logaling/project"
|
22
23
|
require "logaling/glossary"
|
24
|
+
require "logaling/glossary_source"
|
23
25
|
require "logaling/config"
|
24
26
|
|
25
27
|
module Logaling::Command
|
@@ -33,10 +35,10 @@ module Logaling::Command
|
|
33
35
|
@repository = Logaling::Repository.new(@logaling_home)
|
34
36
|
@config = Logaling::Config.load(@repository.config_path)
|
35
37
|
|
36
|
-
@dotfile_path = options["logaling-config"] ? options["logaling-config"] : find_dotfile
|
38
|
+
@dotfile_path = options["logaling-config"] ? options["logaling-config"] : Logaling::Project.find_dotfile
|
37
39
|
@project_config_path = File.join(@dotfile_path, 'config')
|
38
40
|
@config.load(@project_config_path)
|
39
|
-
rescue Logaling::
|
41
|
+
rescue Logaling::ProjectNotFound => e
|
40
42
|
@project_config_path = nil
|
41
43
|
ensure
|
42
44
|
@config.merge!(options)
|
@@ -71,7 +73,7 @@ module Logaling::Command
|
|
71
73
|
config.save(File.join(logaling_config_path, "config"))
|
72
74
|
|
73
75
|
unless options["no-register"]
|
74
|
-
@dotfile_path = options["logaling-config"] ? options["logaling-config"] : find_dotfile
|
76
|
+
@dotfile_path = options["logaling-config"] ? options["logaling-config"] : Logaling::Project.find_dotfile
|
75
77
|
@project_config_path = File.join(@dotfile_path, 'config')
|
76
78
|
@config.load(@project_config_path)
|
77
79
|
register_and_index
|
@@ -88,13 +90,17 @@ module Logaling::Command
|
|
88
90
|
require "logaling/external_glossary"
|
89
91
|
Logaling::ExternalGlossary.load
|
90
92
|
if options["list"]
|
91
|
-
Logaling::ExternalGlossary.list.each {|
|
93
|
+
Logaling::ExternalGlossary.list.each {|glossary_source| say "#{glossary_source.name.bright} : #{glossary_source.description}" }
|
92
94
|
else
|
93
95
|
case external_glossary
|
94
96
|
when 'tmx'
|
95
|
-
|
96
|
-
|
97
|
-
|
97
|
+
check_import_parameter(args)
|
98
|
+
url = args[1]
|
99
|
+
if url && !URI.parse(url).host
|
100
|
+
url = File.expand_path(url)
|
101
|
+
end
|
102
|
+
glossary = Logaling::Glossary.new(args[0], args[2], args[3])
|
103
|
+
@repository.import_tmx(Logaling::ExternalGlossary.get(external_glossary), glossary, url)
|
98
104
|
@repository.index
|
99
105
|
else
|
100
106
|
@repository.import(Logaling::ExternalGlossary.get(external_glossary))
|
@@ -129,12 +135,13 @@ module Logaling::Command
|
|
129
135
|
raise Logaling::CommandFailed, "Can't use '-g <glossary>' option." if options["glossary"]
|
130
136
|
@config.check_required_option("glossary" => "Do 'loga unregister' at project directory.")
|
131
137
|
|
132
|
-
@repository.
|
138
|
+
project = @repository.find_project(@config.glossary)
|
139
|
+
@repository.unregister(project)
|
133
140
|
@repository.index
|
134
141
|
say "#{@config.glossary} is now unregistered."
|
135
142
|
rescue Logaling::CommandFailed => e
|
136
143
|
say e.message
|
137
|
-
rescue Logaling::
|
144
|
+
rescue Logaling::ProjectNotFound => e
|
138
145
|
say "#{@config.glossary} is not yet registered."
|
139
146
|
end
|
140
147
|
|
@@ -167,9 +174,10 @@ module Logaling::Command
|
|
167
174
|
}
|
168
175
|
@config.check_required_option(required_options)
|
169
176
|
check_logaling_home_exists
|
170
|
-
@repository.
|
171
|
-
|
172
|
-
|
177
|
+
project = @repository.find_project(@config.glossary)
|
178
|
+
raise Logaling::ProjectNotFound unless project
|
179
|
+
glossary = project.find_glossary(@config.source_language, @config.target_language)
|
180
|
+
if glossary.bilingual_pair_exists?(source_term, target_term)
|
173
181
|
raise Logaling::TermError, "term '#{source_term}: #{target_term}' already exists in '#{@config.glossary}'"
|
174
182
|
end
|
175
183
|
|
@@ -178,6 +186,9 @@ module Logaling::Command
|
|
178
186
|
say e.message
|
179
187
|
rescue Logaling::GlossaryNotFound => e
|
180
188
|
say "Try 'loga new or register' first."
|
189
|
+
rescue Logaling::ProjectNotFound
|
190
|
+
say "glossary <#{@config.glossary}> not found."
|
191
|
+
say "Try 'loga list' and confirm glossary name."
|
181
192
|
end
|
182
193
|
|
183
194
|
desc 'delete [SOURCE TERM] [TARGET TERM(optional)] [--force(optional)]', 'Delete term.'
|
@@ -190,6 +201,9 @@ module Logaling::Command
|
|
190
201
|
}
|
191
202
|
@config.check_required_option(required_options)
|
192
203
|
check_logaling_home_exists
|
204
|
+
project = @repository.find_project(@config.glossary)
|
205
|
+
raise Logaling::ProjectNotFound unless project
|
206
|
+
glossary = project.find_glossary(@config.source_language, @config.target_language)
|
193
207
|
|
194
208
|
if target_term
|
195
209
|
glossary.delete(source_term, target_term)
|
@@ -200,6 +214,9 @@ module Logaling::Command
|
|
200
214
|
say e.message
|
201
215
|
rescue Logaling::GlossaryNotFound => e
|
202
216
|
say "Try 'loga new or register' first."
|
217
|
+
rescue Logaling::ProjectNotFound
|
218
|
+
say "glossary <#{@config.glossary}> not found."
|
219
|
+
say "Try 'loga list' and confirm glossary name."
|
203
220
|
end
|
204
221
|
|
205
222
|
desc 'update [SOURCE TERM] [TARGET TERM] [NEW TARGET TERM] [NOTE(optional)]', 'Update term.'
|
@@ -211,9 +228,10 @@ module Logaling::Command
|
|
211
228
|
}
|
212
229
|
@config.check_required_option(required_options)
|
213
230
|
check_logaling_home_exists
|
214
|
-
@repository.
|
215
|
-
|
216
|
-
|
231
|
+
project = @repository.find_project(@config.glossary)
|
232
|
+
raise Logaling::ProjectNotFound unless project
|
233
|
+
glossary = project.find_glossary(@config.source_language, @config.target_language)
|
234
|
+
if glossary.bilingual_pair_exists?(source_term, new_target_term, note)
|
217
235
|
raise Logaling::TermError, "term '#{source_term}: #{new_target_term}' already exists in '#{@config.glossary}'"
|
218
236
|
end
|
219
237
|
|
@@ -222,6 +240,9 @@ module Logaling::Command
|
|
222
240
|
say e.message
|
223
241
|
rescue Logaling::GlossaryNotFound => e
|
224
242
|
say "Try 'loga new or register' first."
|
243
|
+
rescue Logaling::ProjectNotFound
|
244
|
+
say "glossary <#{@config.glossary}> not found."
|
245
|
+
say "Try 'loga list' and confirm glossary name."
|
225
246
|
end
|
226
247
|
|
227
248
|
desc 'lookup [TERM]', 'Lookup terms.'
|
@@ -232,27 +253,39 @@ module Logaling::Command
|
|
232
253
|
def lookup(source_term)
|
233
254
|
check_logaling_home_exists
|
234
255
|
@repository.index
|
256
|
+
if @config.glossary
|
257
|
+
project = @repository.find_project(@config.glossary)
|
258
|
+
raise Logaling::ProjectNotFound unless project
|
259
|
+
glossary = project.find_glossary(@config.source_language, @config.target_language)
|
260
|
+
else
|
261
|
+
glossary = nil
|
262
|
+
end
|
235
263
|
terms = @repository.lookup(source_term, glossary, options["dictionary"])
|
236
264
|
unless terms.empty?
|
237
265
|
max_str_size = terms.map{|term| term[:source_term].size}.sort.last
|
238
266
|
run_pager
|
239
267
|
terms.each_with_index do |term, i|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
268
|
+
case options["output"]
|
269
|
+
when "terminal"
|
270
|
+
term_renderer = Logaling::Command::Renderers::TermDefaultRenderer.new(term, @repository, @config, options)
|
271
|
+
term_renderer.max_str_size = max_str_size
|
272
|
+
term_renderer.render($stdout)
|
273
|
+
when "csv"
|
274
|
+
term_renderer = Logaling::Command::Renderers::TermCsvRenderer.new(term, @repository, @config, options)
|
275
|
+
term_renderer.render($stdout)
|
276
|
+
when "json"
|
277
|
+
term_renderer = Logaling::Command::Renderers::TermJsonRenderer.new(term, @repository, @config, options)
|
278
|
+
term_renderer.index = i
|
279
|
+
term_renderer.last_index = terms.length
|
280
|
+
term_renderer.render($stdout)
|
249
281
|
end
|
250
|
-
printer(source_string, target_string, note,
|
251
|
-
glossary_name, max_str_size, i, terms.length)
|
252
282
|
end
|
253
283
|
else
|
254
284
|
"source-term <#{source_term}> not found"
|
255
285
|
end
|
286
|
+
rescue Logaling::ProjectNotFound
|
287
|
+
say "glossary <#{@config.glossary}> not found."
|
288
|
+
say "Try 'loga list' and confirm glossary name."
|
256
289
|
rescue Logaling::CommandFailed, Logaling::TermError => e
|
257
290
|
say e.message
|
258
291
|
end
|
@@ -272,8 +305,10 @@ module Logaling::Command
|
|
272
305
|
}
|
273
306
|
@config.check_required_option(required_options)
|
274
307
|
check_logaling_home_exists
|
275
|
-
@repository.
|
276
|
-
|
308
|
+
project = @repository.find_project(@config.glossary)
|
309
|
+
raise Logaling::ProjectNotFound unless project
|
310
|
+
glossary = project.find_glossary(@config.source_language, @config.target_language)
|
311
|
+
terms = glossary.terms
|
277
312
|
unless terms.empty?
|
278
313
|
run_pager
|
279
314
|
max_str_size = terms.map{|term| term[:source_term].size}.sort.last
|
@@ -285,9 +320,11 @@ module Logaling::Command
|
|
285
320
|
else
|
286
321
|
"glossary <#{@config.glossary}> not found"
|
287
322
|
end
|
288
|
-
|
289
323
|
rescue Logaling::CommandFailed, Logaling::GlossaryDBNotFound => e
|
290
324
|
say e.message
|
325
|
+
rescue Logaling::ProjectNotFound
|
326
|
+
say "glossary <#{@config.glossary}> not found."
|
327
|
+
say "Try 'loga list' and confirm glossary name."
|
291
328
|
end
|
292
329
|
|
293
330
|
desc 'list', 'Show glossary list.'
|
@@ -295,52 +332,25 @@ module Logaling::Command
|
|
295
332
|
def list
|
296
333
|
check_logaling_home_exists
|
297
334
|
@repository.index
|
298
|
-
|
299
|
-
unless
|
335
|
+
projects = @repository.projects
|
336
|
+
unless projects.empty?
|
300
337
|
run_pager
|
301
|
-
|
302
|
-
printf(" %s\n",
|
338
|
+
projects.each do |project|
|
339
|
+
printf(" %s\n", project.name)
|
303
340
|
end
|
304
341
|
else
|
305
342
|
"There is no registered glossary."
|
306
343
|
end
|
307
|
-
|
308
344
|
rescue Logaling::CommandFailed, Logaling::GlossaryDBNotFound => e
|
309
345
|
say e.message
|
310
346
|
end
|
311
347
|
|
312
348
|
private
|
313
|
-
def windows?
|
314
|
-
RUBY_PLATFORM =~ /win32|mingw32/i
|
315
|
-
end
|
316
|
-
|
317
|
-
def glossary
|
318
|
-
@glossary ||= Logaling::Glossary.new(@config.glossary, @config.source_language, @config.target_language, @logaling_home)
|
319
|
-
end
|
320
|
-
|
321
349
|
def error(msg)
|
322
350
|
STDERR.puts(msg)
|
323
351
|
exit 1
|
324
352
|
end
|
325
353
|
|
326
|
-
def find_dotfile
|
327
|
-
dir = Dir.pwd
|
328
|
-
searched_path = []
|
329
|
-
loop do
|
330
|
-
path = File.join(dir, '.logaling')
|
331
|
-
if File.exist?(path)
|
332
|
-
return path
|
333
|
-
else
|
334
|
-
unless Pathname.new(dir).root?
|
335
|
-
searched_path << dir
|
336
|
-
dir = File.dirname(dir)
|
337
|
-
else
|
338
|
-
raise(Logaling::CommandFailed, "Can't found .logaling in #{searched_path}")
|
339
|
-
end
|
340
|
-
end
|
341
|
-
end
|
342
|
-
end
|
343
|
-
|
344
354
|
def logaling_config_path
|
345
355
|
if options["logaling-config"]
|
346
356
|
options["logaling-config"]
|
@@ -349,42 +359,8 @@ module Logaling::Command
|
|
349
359
|
end
|
350
360
|
end
|
351
361
|
|
352
|
-
# http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby
|
353
362
|
def run_pager
|
354
|
-
|
355
|
-
return if windows?
|
356
|
-
return unless STDOUT.tty?
|
357
|
-
|
358
|
-
read, write = IO.pipe
|
359
|
-
|
360
|
-
unless Kernel.fork # Child process
|
361
|
-
STDOUT.reopen(write)
|
362
|
-
STDERR.reopen(write) if STDERR.tty?
|
363
|
-
read.close
|
364
|
-
write.close
|
365
|
-
return
|
366
|
-
end
|
367
|
-
|
368
|
-
# Parent process, become pager
|
369
|
-
STDIN.reopen(read)
|
370
|
-
read.close
|
371
|
-
write.close
|
372
|
-
|
373
|
-
ENV['LESS'] = 'FSRX' # Don't page if the input is short enough
|
374
|
-
|
375
|
-
# wait until we have input before we start the pager
|
376
|
-
Kernel.select [STDIN]
|
377
|
-
pager = ENV['PAGER'] || 'less'
|
378
|
-
exec pager rescue exec "/bin/sh", "-c", pager
|
379
|
-
end
|
380
|
-
|
381
|
-
def extract_keyword_and_coloring(snipped_term, term)
|
382
|
-
return term if snipped_term.empty? || options["no-color"]
|
383
|
-
display_string = snipped_term.map do |word|
|
384
|
-
word.is_a?(Hash) ? word[:keyword].bright : word
|
385
|
-
end
|
386
|
-
display_string = display_string.join
|
387
|
-
display_string
|
363
|
+
Pager.run unless options["no-pager"]
|
388
364
|
end
|
389
365
|
|
390
366
|
def check_logaling_home_exists
|
@@ -393,52 +369,12 @@ module Logaling::Command
|
|
393
369
|
end
|
394
370
|
end
|
395
371
|
|
396
|
-
def
|
397
|
-
|
398
|
-
case options["output"]
|
399
|
-
when "terminal"
|
400
|
-
unless note
|
401
|
-
format = target_string + "\t" + glossary_name
|
402
|
-
else
|
403
|
-
format = target_string + "\t# " + note + "\t" + glossary_name
|
404
|
-
end
|
405
|
-
printf(" %-#{max_str_size+10}s %s\n", source_string, format)
|
406
|
-
when "csv"
|
407
|
-
items = [source_string, target_string, note,
|
408
|
-
@config.source_language, @config.target_language]
|
409
|
-
print(CSV.generate {|csv| csv << items})
|
410
|
-
when "json"
|
411
|
-
puts("[") if i == 0
|
412
|
-
puts(",") if i > 0
|
413
|
-
record = {
|
414
|
-
:source => source_string, :target => target_string, :note => note,
|
415
|
-
:source_language => @config.source_language,
|
416
|
-
:target_language => @config.target_language
|
417
|
-
}
|
418
|
-
print JSON.pretty_generate(record)
|
419
|
-
puts("\n]") if i == last-1
|
420
|
-
end
|
421
|
-
end
|
422
|
-
|
423
|
-
def check_import_parameter(glossary_info)
|
424
|
-
unless glossary_info[:name] && glossary_info[:url]
|
372
|
+
def check_import_parameter(args)
|
373
|
+
unless args[0] && args[1]
|
425
374
|
raise Logaling::CommandFailed, "Do 'loga import tmx <glossary name> <url or path>'"
|
426
375
|
end
|
427
376
|
end
|
428
377
|
|
429
|
-
def initialize_import_parameter(arr)
|
430
|
-
glossary_info = {}
|
431
|
-
url = arr[1]
|
432
|
-
if url && !URI.parse(url).host
|
433
|
-
url = File::expand_path(url)
|
434
|
-
end
|
435
|
-
glossary_info[:name] = arr[0]
|
436
|
-
glossary_info[:url] = url
|
437
|
-
glossary_info[:source_language] = arr[2]
|
438
|
-
glossary_info[:target_language] = arr[3]
|
439
|
-
glossary_info
|
440
|
-
end
|
441
|
-
|
442
378
|
def register_and_index
|
443
379
|
@repository.register(@dotfile_path, @config.glossary)
|
444
380
|
@repository.index
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Logaling::Command
|
2
|
+
# http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby
|
3
|
+
module Pager
|
4
|
+
def self.run
|
5
|
+
return if windows?
|
6
|
+
return unless STDOUT.tty?
|
7
|
+
|
8
|
+
read, write = IO.pipe
|
9
|
+
|
10
|
+
unless Kernel.fork # Child process
|
11
|
+
STDOUT.reopen(write)
|
12
|
+
STDERR.reopen(write) if STDERR.tty?
|
13
|
+
read.close
|
14
|
+
write.close
|
15
|
+
return
|
16
|
+
end
|
17
|
+
|
18
|
+
# Parent process, become pager
|
19
|
+
STDIN.reopen(read)
|
20
|
+
read.close
|
21
|
+
write.close
|
22
|
+
|
23
|
+
ENV['LESS'] = 'FSRX' # Don't page if the input is short enough
|
24
|
+
|
25
|
+
# wait until we have input before we start the pager
|
26
|
+
Kernel.select [STDIN]
|
27
|
+
pager = ENV['PAGER'] || 'less'
|
28
|
+
exec pager rescue exec "/bin/sh", "-c", pager
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def self.windows?
|
33
|
+
RUBY_PLATFORM =~ /win32|mingw32/i
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Koji SHIMADA <koji.shimada@enishi-tech.com>
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module Logaling::Command
|
19
|
+
module Renderers
|
20
|
+
class TermRenderer
|
21
|
+
def initialize(term, repository, config, options)
|
22
|
+
@term = term
|
23
|
+
@repository = repository
|
24
|
+
@config = config
|
25
|
+
@options = options
|
26
|
+
end
|
27
|
+
|
28
|
+
def render(output); end
|
29
|
+
|
30
|
+
def glossary_name
|
31
|
+
@term[:glossary_name]
|
32
|
+
end
|
33
|
+
|
34
|
+
def note
|
35
|
+
@term[:note].to_s unless @term[:note].empty?
|
36
|
+
end
|
37
|
+
|
38
|
+
def source_term
|
39
|
+
extract_keyword_and_coloring(@term[:snipped_source_term], @term[:source_term])
|
40
|
+
end
|
41
|
+
|
42
|
+
def target_term
|
43
|
+
extract_keyword_and_coloring(@term[:snipped_target_term], @term[:target_term])
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def extract_keyword_and_coloring(snipped_term, term)
|
48
|
+
return term if @options["no-color"]
|
49
|
+
build_term_string_from_snippets(snipped_term)
|
50
|
+
end
|
51
|
+
|
52
|
+
def build_term_string_from_snippets(snippets)
|
53
|
+
snippets.map{|snippet| decorate_snippet(snippet) }.join
|
54
|
+
end
|
55
|
+
|
56
|
+
def decorate_snippet(snippet)
|
57
|
+
keyword?(snippet) ? snippet[:keyword].bright : snippet
|
58
|
+
end
|
59
|
+
|
60
|
+
def keyword?(snippet)
|
61
|
+
snippet.is_a?(Hash)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class TermDefaultRenderer < TermRenderer
|
66
|
+
attr_accessor :max_str_size
|
67
|
+
|
68
|
+
def initialize(term, repository, config, options)
|
69
|
+
super
|
70
|
+
@max_str_size = 0
|
71
|
+
end
|
72
|
+
|
73
|
+
def render(output)
|
74
|
+
format = [target_term, note, glossary_name].compact.join("\t")
|
75
|
+
output.printf(" %-#{@max_str_size+10}s %s\n", source_term, format)
|
76
|
+
end
|
77
|
+
|
78
|
+
def glossary_name
|
79
|
+
if @repository.glossary_counts > 1
|
80
|
+
if @term[:glossary_name] == @config.glossary
|
81
|
+
@term[:glossary_name].foreground(:white).background(:green)
|
82
|
+
else
|
83
|
+
@term[:glossary_name]
|
84
|
+
end
|
85
|
+
else
|
86
|
+
""
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def note
|
91
|
+
note_string = super
|
92
|
+
"# #{note_string}" if note_string
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class TermCsvRenderer < TermRenderer
|
97
|
+
def render(output)
|
98
|
+
items = [source_term, target_term, note,
|
99
|
+
@config.source_language, @config.target_language, glossary_name]
|
100
|
+
output.print(CSV.generate {|csv| csv << items})
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class TermJsonRenderer < TermRenderer
|
105
|
+
attr_accessor :index, :last_index
|
106
|
+
|
107
|
+
def initialize(term, repository, config, options)
|
108
|
+
super
|
109
|
+
@index = 0
|
110
|
+
@last_index = 0
|
111
|
+
end
|
112
|
+
|
113
|
+
def render(output)
|
114
|
+
first_line? ? output.puts("[") : output.puts(",")
|
115
|
+
record = {
|
116
|
+
:source => source_term, :target => target_term, :note => note,
|
117
|
+
:source_language => @config.source_language,
|
118
|
+
:target_language => @config.target_language,
|
119
|
+
:glossary => glossary_name
|
120
|
+
}
|
121
|
+
output.print JSON.pretty_generate(record)
|
122
|
+
output.puts("\n]") if last_line?
|
123
|
+
end
|
124
|
+
|
125
|
+
private
|
126
|
+
def first_line?
|
127
|
+
@index == 0
|
128
|
+
end
|
129
|
+
def last_line?
|
130
|
+
@index == @last_index-1
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
data/lib/logaling/command.rb
CHANGED
@@ -23,8 +23,8 @@ module Logaling
|
|
23
23
|
output_format 'csv'
|
24
24
|
|
25
25
|
private
|
26
|
-
def convert_to_csv(csv,
|
27
|
-
doc = ::Nokogiri::XML(open(
|
26
|
+
def convert_to_csv(csv, glossary, url)
|
27
|
+
doc = ::Nokogiri::XML(open(url, "r"))
|
28
28
|
tu_nodes = doc.xpath('//tmx/body/tu')
|
29
29
|
tu_nodes.each do |tu|
|
30
30
|
original = ""
|
@@ -33,12 +33,12 @@ module Logaling
|
|
33
33
|
tu.children.each do |tuv|
|
34
34
|
if tuv.name == "tuv"
|
35
35
|
lang = convert_language_code_iso_639(tuv["lang"])
|
36
|
-
if lang ==
|
36
|
+
if lang == glossary.source_language
|
37
37
|
tuv.children.each do |child|
|
38
38
|
original = child.text.strip if child.name == "seg"
|
39
39
|
notes << child.text.strip if child.name == "note"
|
40
40
|
end
|
41
|
-
elsif lang ==
|
41
|
+
elsif lang == glossary.target_language
|
42
42
|
tuv.children.each do |child|
|
43
43
|
translation = child.text.strip if child.name == "seg"
|
44
44
|
notes << child.text.strip if child.name == "note"
|
@@ -68,19 +68,19 @@ class Logaling::ExternalGlossary
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
-
def import(
|
72
|
-
if
|
73
|
-
unless file_exists?(
|
74
|
-
raise Logaling::GlossaryNotFound, "Failed open url/path <#{
|
71
|
+
def import(glossary=nil, url=nil)
|
72
|
+
if glossary && url
|
73
|
+
unless file_exists?(url)
|
74
|
+
raise Logaling::GlossaryNotFound, "Failed open url/path <#{url}>"
|
75
75
|
end
|
76
76
|
end
|
77
|
-
File.open(import_file_name(
|
77
|
+
File.open(import_file_name(glossary), "w") do |output|
|
78
78
|
output_format = self.class.output_format
|
79
79
|
output_format = output_format.to_s if output_format.is_a?(Symbol)
|
80
80
|
case output_format
|
81
81
|
when "csv"
|
82
|
-
if
|
83
|
-
convert_to_csv(CSV.new(output),
|
82
|
+
if glossary
|
83
|
+
convert_to_csv(CSV.new(output), glossary, url)
|
84
84
|
else
|
85
85
|
convert_to_csv(CSV.new(output))
|
86
86
|
end
|
@@ -91,14 +91,14 @@ class Logaling::ExternalGlossary
|
|
91
91
|
end
|
92
92
|
|
93
93
|
private
|
94
|
-
def import_file_name(
|
95
|
-
if
|
96
|
-
|
97
|
-
|
98
|
-
|
94
|
+
def import_file_name(glossary=nil)
|
95
|
+
if glossary
|
96
|
+
glossary.name ||= self.class.name
|
97
|
+
glossary.source_language ||= self.class.source_language
|
98
|
+
glossary.target_language ||= self.class.target_language
|
99
99
|
|
100
|
-
[
|
101
|
-
|
100
|
+
[glossary.name, glossary.source_language,
|
101
|
+
glossary.target_language, self.class.output_format].join('.')
|
102
102
|
else
|
103
103
|
[self.class.name, self.class.source_language,
|
104
104
|
self.class.target_language, self.class.output_format].join('.')
|