konjac 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/osascript
2
+ to strip_line_breaks(this_line)
3
+ try
4
+ repeat while last character of this_line is in {ASCII character 13, ASCII character 10, ASCII character 7}
5
+ set this_line to characters 1 thru -2 of this_line as string
6
+ end repeat
7
+ return this_line
8
+ on error
9
+ return ""
10
+ end try
11
+ end strip_line_breaks
12
+
13
+ to get_posix_path(mac_path)
14
+ set result to POSIX path of mac_path
15
+ end get_posix_path
16
+
17
+ tell application "Microsoft Word"
18
+ activate
19
+ set active_document to active document
20
+ set carriage_return to ASCII character 13
21
+ set line_feed to ASCII character 10
22
+ set ignored_content to {missing value, "", line_feed}
23
+
24
+ -- set tags path to full path to active presentation with a .tags extension
25
+ set tags_path to ((path of active_document) & ".diff")
26
+
27
+ -- delete tags file if it already exists
28
+ tell application "Finder"
29
+ if exists file tags_path then
30
+ delete file tags_path
31
+ end if
32
+ end tell
33
+
34
+ -- open tags file for writing
35
+ set tags_file to open for access file tags_path with write permission
36
+ set eof of tags_file to 0
37
+
38
+ write ("---" & my get_posix_path(path of active_document)) to tags_file
39
+ write line_feed to tags_file
40
+ write ("+++" & my get_posix_path(path of active_document)) to tags_file
41
+ write line_feed to tags_file
42
+
43
+ -- iterate through paragraphs
44
+ set paragraph_index to 1
45
+ set current_paragraph to paragraph 1 in active document
46
+
47
+ repeat
48
+ -- get text content if it's available
49
+ set text_content to my strip_line_breaks(content of text object of current_paragraph as text)
50
+
51
+ if text_content is not in ignored_content then
52
+ write ("@@ " & paragraph_index & " @@") to tags_file
53
+ write line_feed to tags_file
54
+ write ("-" & text_content) to tags_file as «class utf8»
55
+ write line_feed to tags_file
56
+ write ("+" & text_content) to tags_file as «class utf8»
57
+ write line_feed to tags_file
58
+ end if
59
+
60
+ -- increment paragraph index
61
+ set paragraph_index to (paragraph_index + 1)
62
+
63
+ -- try to find the next paragraph
64
+ try
65
+ set current_paragraph to next paragraph of current_paragraph
66
+ on error
67
+ exit repeat
68
+ end try
69
+ end repeat
70
+
71
+ -- close everything
72
+ close access tags_file
73
+ end tell
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/osascript
2
+ set numbers_as_text to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
3
+
4
+ -- parses the headers/separators inside the tags file
5
+ to parse_header(header)
6
+ set at_offset to 4
7
+
8
+ repeat while character at_offset of header is in my numbers_as_text
9
+ set at_offset to (at_offset + 1)
10
+ end repeat
11
+
12
+ return at_offset
13
+ end parse_header
14
+
15
+ to get_posix_path(mac_path)
16
+ set result to POSIX path of mac_path
17
+ end get_posix_path
18
+
19
+ tell application "Microsoft Word"
20
+ activate
21
+ set active_document to active document
22
+ set carriage_return to ASCII character 13
23
+ set line_feed to ASCII character 10
24
+ set ignored_content to {missing value, "", line_feed}
25
+
26
+ -- set tags path to full path to active presentation with a .tags extension
27
+ set tags_path to ((path of active_document) & ".diff")
28
+
29
+ -- read in tags
30
+ set tags_file to open for access file tags_path
31
+ set tags to (read tags_file for (get eof tags_file) as «class utf8» using delimiter line_feed)
32
+ close access tags_file
33
+
34
+ -- iterate through paragraphs
35
+ set paragraph_index to 0
36
+ repeat with current_line in tags
37
+ if current_line starts with "@@ " then
38
+ set space_offset to my parse_header(current_line)
39
+ set paragraph_index to text 4 thru space_offset of current_line as number
40
+ else if current_line starts with "+" then
41
+ if paragraph_index is not 0 then
42
+ set current_content to text object of paragraph paragraph_index of active document
43
+ set my_range to create range active document start (start of content of current_content) end (end of content of current_content)
44
+ select my_range
45
+ type text selection text (text 2 thru (length of current_line) of current_line)
46
+ end if
47
+ end if
48
+ end repeat
49
+ end tell
@@ -5,12 +5,14 @@ module Konjac
5
5
  # A list of the Tag objects current being managed
6
6
  attr_accessor :tags
7
7
 
8
- # A regex to match lines that start with <tt>> </tt>, that is, lines that
8
+ # A regex to match lines that start with <tt>-</tt>, that is, lines that
9
9
  # indicate original text in .konjac files
10
- STARTS_WITH_CLOSE_TAG = /^\>/
10
+ OLD_TAG = /^-[^-]/
11
+
12
+ NEW_TAG = /^\+[^+]/
11
13
 
12
14
  # Regex for matching index numbers
13
- KONJAC_TAG = /^\[\[KJ\-(\d+)\]\]/
15
+ KONJAC_TAG = /^@@\s(\d+).*@@$/
14
16
 
15
17
  # Creates a new TagManager
16
18
  def initialize(path)
@@ -34,10 +36,10 @@ module Konjac
34
36
  end
35
37
 
36
38
  index = line.match(KONJAC_TAG)[1].to_i
37
- elsif line =~ STARTS_WITH_CLOSE_TAG
38
- orig = line[2..-1].chomp
39
- else
40
- trans = line.chomp
39
+ elsif line =~ OLD_TAG
40
+ orig = line[1..-1].chomp
41
+ elsif line =~ NEW_TAG
42
+ trans = line[1..-1].chomp
41
43
  unless index.nil?
42
44
  @tags << Tag.new(index, orig, trans)
43
45
  index = nil
@@ -2,6 +2,7 @@
2
2
  module Konjac
3
3
  # A class consisting of functions for translation
4
4
  module Translator
5
+ DIFF_ADD = /^\+(?!\+\+ )/
5
6
  class << self
6
7
  # Translates a file or list of files
7
8
  #
@@ -11,14 +12,21 @@ module Konjac
11
12
  load_dictionary from_lang, to_lang, opts
12
13
 
13
14
  files.each do |source|
14
- # Read in file and replace matches in content
15
- content = File.read(source)
16
- content = translate_content(content)
15
+ # Handle .diff files differently (har har)
16
+ is_diff = (File.extname(source) == ".diff")
17
17
 
18
- # Write changed content to file
19
- File.open(Utils.build_converted_file_name(source, from_lang, to_lang), "w") do |file|
20
- file.puts content
18
+ temp_file = Tempfile.new("konjac")
19
+ File.open(source, "r") do |file|
20
+ file.each_line do |line|
21
+ if is_diff && line !~ DIFF_ADD
22
+ temp_file.puts line
23
+ else
24
+ temp_file.puts translate_content(line)
25
+ end
26
+ end
21
27
  end
28
+ target = Utils.build_converted_file_name(source, from_lang, to_lang)
29
+ FileUtils.mv temp_file.path, target
22
30
  end
23
31
  end
24
32
 
data/lib/konjac/utils.rb CHANGED
@@ -38,7 +38,7 @@ module Konjac
38
38
  file = opts[:directory] + "/" + file
39
39
  end
40
40
  unless opts[:extension].nil?
41
- file = file.sub(/\.[^\\\/]*$/, "") + opts[:extension].to_s.tr(".", "")
41
+ file = file.sub(/\.[^\\\/]*$/, "") + "." + opts[:extension].to_s.tr(".", "")
42
42
  end
43
43
  parsed_files += Dir.glob(File.expand_path(file))
44
44
  end
@@ -1,4 +1,4 @@
1
1
  module Konjac
2
2
  # The current version number of Konjac
3
- VERSION = "0.2.2"
3
+ VERSION = "0.2.4"
4
4
  end
data/lib/konjac/word.rb CHANGED
@@ -6,7 +6,7 @@ module Konjac
6
6
  # Imports the text content of a tag file into a {Microsoft
7
7
  # Word}[http://office.microsoft.com/en-us/word/] 2003+ document
8
8
  def import_docx_tags(files)
9
- sub_files = Utils.parse_files(files, ".docx")
9
+ sub_files = Utils.parse_files(files, :extension => :docx)
10
10
  sub_files.each do |sub_file|
11
11
  # Build the list of paths we need to work with
12
12
  dirname = File.dirname(sub_file)
@@ -14,7 +14,7 @@ module Konjac
14
14
  orig_docx = "#{dirname}/#{basename}.docx"
15
15
  new_path = "#{dirname}/#{basename}_imported.docx"
16
16
  xml_path = "#{dirname}/#{basename}.xml"
17
- tags_path = "#{dirname}/#{basename}.konjac"
17
+ tags_path = "#{dirname}/#{basename}.diff"
18
18
  out_path = "#{dirname}/word/document.xml"
19
19
 
20
20
  # Open the original XML file and the updated tags
@@ -60,7 +60,7 @@ module Konjac
60
60
  end
61
61
  end
62
62
 
63
- sub_files = Utils.parse_files(files, ".docx")
63
+ sub_files = Utils.parse_files(files, :extension => :docx)
64
64
  sub_files.each do |sub_file|
65
65
  # Build a list of all the paths we're working with
66
66
  dirname = File.dirname(sub_file)
@@ -68,7 +68,7 @@ module Konjac
68
68
  orig_docx = "#{dirname}/#{basename}.docx"
69
69
  xml_path = "#{dirname}/#{basename}_orig.xml"
70
70
  clean_path = "#{dirname}/#{basename}.xml"
71
- tags_path = "#{dirname}/#{basename}.konjac"
71
+ tags_path = "#{dirname}/#{basename}.diff"
72
72
 
73
73
  # Unzip the DOCX's word/document.xml file and pipe the output into
74
74
  # an XML with the same base name as the DOCX
@@ -98,17 +98,16 @@ module Konjac
98
98
  end
99
99
 
100
100
  # Write the tags file
101
- index = 0
102
-
103
- cleaner.xpath("//w:t").each do |node|
104
- tags_file.puts "[[KJ-%i]]%s" % [index, additional_info(node)]
105
- tags_file.puts "> %s" % node.content
101
+ tags_file.puts "---" + orig_docx
102
+ tags_file.puts "+++" + orig_docx
103
+ cleaner.xpath("//w:t").each_with_index do |node, index|
104
+ tags_file.puts "@@ %i @@" % [index, additional_info(node)]
105
+ tags_file.puts "-" + node.content
106
106
  if attempting_to_translate
107
- tags_file.puts Translator.translate_content(node.content)
107
+ tags_file.puts "+" + Translator.translate_content(node.content)
108
108
  else
109
- tags_file.puts node.content
109
+ tags_file.puts "+" + node.content
110
110
  end
111
- index += 1
112
111
  end
113
112
  end
114
113
 
data/lib/konjac.rb CHANGED
@@ -4,6 +4,7 @@ autoload :Amatch, "amatch"
4
4
  autoload :FileUtils, "fileutils"
5
5
  autoload :I18n, "i18n"
6
6
  autoload :Nokogiri, "nokogiri"
7
+ autoload :Tempfile, "tempfile"
7
8
 
8
9
  # Konjac is a Ruby command-line utility for translating files using a YAML
9
10
  # wordlist
data/spec/cli_spec.rb CHANGED
@@ -40,7 +40,11 @@ describe CLI do
40
40
  CLI.start
41
41
  converted_path = Utils.build_converted_file_name(@english.path, "en", "ja")
42
42
  File.read(@english.path).should == "I like dogs."
43
- File.read(converted_path).should == "I like 犬.\n"
43
+ File.open(converted_path, "r") do |file|
44
+ file.each do |line|
45
+ line.chomp.should == "I like 犬."
46
+ end
47
+ end
44
48
  end
45
49
  end
46
50
  end
data/spec/tag_spec.rb CHANGED
@@ -6,20 +6,22 @@ describe Tag do
6
6
  before :each do
7
7
  @tags_file = Tempfile.new(["tags", ".tags"])
8
8
  @tags_file.write <<-eos.gsub(/^\s+/, "")
9
- [[KJ-0]]
10
- >
11
- dog
12
- [[KJ-1]]
13
- > 何ですか。
14
- What is it?
15
- [[KJ-2]]
16
- > 空白
17
- [[KJ-3]] #=> comment
18
- > コメント
19
- Comment
20
- [[KJ-5]]
21
- > 以上
22
- -- end --
9
+ --- /path/to/old
10
+ +++ /path/to/new
11
+ @@ 0 @@
12
+ -犬
13
+ +dog
14
+ @@ 1 @@
15
+ -何ですか。
16
+ +What is it?
17
+ @@ 2 @@
18
+ -空白
19
+ @@ 3 comment @@
20
+ -コメント
21
+ +Comment
22
+ @@ 5 @@
23
+ -以上
24
+ +--- end --
23
25
  eos
24
26
  @tags_file.rewind
25
27
  @manager = TagManager.new(@tags_file.path)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: konjac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-19 00:00:00.000000000 Z
12
+ date: 2012-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amatch
16
- requirement: &70292727602840 !ruby/object:Gem::Requirement
16
+ requirement: &70275492952780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70292727602840
24
+ version_requirements: *70275492952780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: i18n
27
- requirement: &70292727602280 !ruby/object:Gem::Requirement
27
+ requirement: &70275492973220 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70292727602280
35
+ version_requirements: *70275492973220
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nokogiri
38
- requirement: &70292727601660 !ruby/object:Gem::Requirement
38
+ requirement: &70275492972580 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70292727601660
46
+ version_requirements: *70275492972580
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sdoc
49
- requirement: &70292727601020 !ruby/object:Gem::Requirement
49
+ requirement: &70275492971960 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70292727601020
57
+ version_requirements: *70275492971960
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: term-ansicolor
60
- requirement: &70292727600580 !ruby/object:Gem::Requirement
60
+ requirement: &70275492971540 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70292727600580
68
+ version_requirements: *70275492971540
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: trollop
71
- requirement: &70292727600160 !ruby/object:Gem::Requirement
71
+ requirement: &70275492971120 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70292727600160
79
+ version_requirements: *70275492971120
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: autotest
82
- requirement: &70292727599560 !ruby/object:Gem::Requirement
82
+ requirement: &70275492970680 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70292727599560
90
+ version_requirements: *70275492970680
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: autotest-fsevent
93
- requirement: &70292727598960 !ruby/object:Gem::Requirement
93
+ requirement: &70275492970240 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70292727598960
101
+ version_requirements: *70275492970240
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: autotest-growl
104
- requirement: &70292727598300 !ruby/object:Gem::Requirement
104
+ requirement: &70275492969820 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70292727598300
112
+ version_requirements: *70275492969820
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: bundler
115
- requirement: &70292727597880 !ruby/object:Gem::Requirement
115
+ requirement: &70275492969400 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70292727597880
123
+ version_requirements: *70275492969400
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: rspec
126
- requirement: &70292727597460 !ruby/object:Gem::Requirement
126
+ requirement: &70275492968980 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,12 +131,14 @@ dependencies:
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *70292727597460
134
+ version_requirements: *70275492968980
135
135
  description: A Ruby command-line utility for translating files using a YAML wordlist
136
136
  email:
137
137
  - bryan.mckelvey@gmail.com
138
138
  executables:
139
139
  - konjac
140
+ - konjac_word_export
141
+ - konjac_word_import
140
142
  extensions: []
141
143
  extra_rdoc_files:
142
144
  - README.rdoc
@@ -149,6 +151,8 @@ files:
149
151
  - README.rdoc
150
152
  - Rakefile
151
153
  - bin/konjac
154
+ - bin/konjac_word_export
155
+ - bin/konjac_word_import
152
156
  - konjac.gemspec
153
157
  - lib/konjac.rb
154
158
  - lib/konjac/cli.rb