konjac 0.2.9.5 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,110 +0,0 @@
1
- #!/usr/bin/osascript
2
- -- round up ASCII characters used for formatting in Word
3
- global bell
4
- global line_feed
5
- global form_feed
6
- global carriage_return
7
- global format_chars
8
-
9
- -- strip line breaks and formatting characters
10
- to strip_line_breaks(this_line)
11
- try
12
- -- remove last character until no it's no longer an excluded character
13
- repeat while last character of this_line is in format_chars
14
- set this_line to characters 1 thru -2 of this_line as string
15
- end repeat
16
- return this_line as text
17
- on error
18
- return ""
19
- end try
20
- end strip_line_breaks
21
-
22
- to get_posix_path(mac_path)
23
- set result to POSIX path of mac_path
24
- end get_posix_path
25
-
26
- to extract(filename)
27
- tell application "Microsoft Word"
28
- activate
29
- open filename
30
- set active_document to active document
31
- set ignored_content to {missing value, "", line_feed}
32
-
33
- -- set tags path to full path to active presentation with a .tags extension
34
- set tags_path to ((path of active_document) & ".diff")
35
-
36
- -- delete tags file if it already exists
37
- tell application "Finder"
38
- if exists file tags_path then
39
- delete file tags_path
40
- end if
41
- end tell
42
-
43
- -- open tags file for writing
44
- set tags_file to open for access file tags_path with write permission
45
- set eof of tags_file to 0
46
-
47
- write ("--- " & my get_posix_path(path of active_document)) to tags_file
48
- write line_feed to tags_file
49
- write ("+++ " & my get_posix_path(path of active_document)) to tags_file
50
- write line_feed to tags_file
51
-
52
- -- iterate through paragraphs
53
- set paragraph_index to 1
54
- set current_paragraph to paragraph 1 in active_document
55
-
56
- -- save old delimiters
57
-
58
- repeat
59
- -- get text content if it's available
60
- set text_content to my strip_line_breaks(content of text object of current_paragraph) as string
61
-
62
- -- replace "soft returns" (vertical tabs) with real returns
63
- set old_delimiters to AppleScript's text item delimiters
64
- set AppleScript's text item delimiters to {ASCII character 11}
65
- set split_content to every text item of text_content
66
- set AppleScript's text item delimiters to old_delimiters
67
-
68
- if text_content is not in ignored_content then
69
- write ("@@ " & paragraph_index & " @@") to tags_file
70
- write line_feed to tags_file
71
- repeat with text_line in split_content
72
- write ("-" & text_line) to tags_file as «class utf8»
73
- write line_feed to tags_file
74
- end repeat
75
- repeat with text_line in split_content
76
- write ("+" & text_line) to tags_file as «class utf8»
77
- write line_feed to tags_file
78
- end repeat
79
- end if
80
-
81
- -- increment paragraph index
82
- set paragraph_index to (paragraph_index + 1)
83
-
84
- -- try to find the next paragraph
85
- try
86
- set current_paragraph to next paragraph of current_paragraph
87
- on error
88
- exit repeat
89
- end try
90
- end repeat
91
-
92
- -- close everything
93
- close access tags_file
94
- end tell
95
- end extract
96
-
97
- -- initialize global variables, just formatting characters here
98
- to init_globals()
99
- set bell to ASCII character 7
100
- set line_feed to ASCII character 10
101
- set form_feed to ASCII character 12
102
- set carriage_return to ASCII character 13
103
- set format_chars to {bell, line_feed, form_feed, carriage_return}
104
- end init_globals
105
-
106
- -- main()
107
- on run argv
108
- init_globals()
109
- extract(item 1 of argv)
110
- end
@@ -1,139 +0,0 @@
1
- #!/usr/bin/osascript
2
- global numbers_as_text
3
- global line_feed
4
- global vertical_tab
5
- global carriage_return
6
-
7
- -- parses the headers/separators inside the tags file
8
- to parse_header(header)
9
- set at_offset to 4
10
-
11
- repeat while character at_offset of header is in my numbers_as_text
12
- set at_offset to (at_offset + 1)
13
- end repeat
14
-
15
- return at_offset
16
- end parse_header
17
-
18
- to ends_with_forbidden_character(txt)
19
- set last_character to character -1 of txt
20
-
21
- if last_character is in {line_feed, carriage_return, vertical_tab}
22
- return true
23
- else
24
- return false
25
- end if
26
- end ends_with_forbidden_character
27
-
28
- to get_posix_path(mac_path)
29
- set result to POSIX path of mac_path
30
- end get_posix_path
31
-
32
- to import(filename)
33
- tell application "Microsoft Word"
34
- activate
35
- open filename
36
- set active_document to active document
37
- set ignored_content to {missing value, "", line_feed}
38
-
39
- -- set tags path to full path to active presentation with a .tags extension
40
- set tags_path to ((path of active_document) & ".diff")
41
-
42
- -- read in tags
43
- set tags_file to open for access file tags_path
44
- set tags to (read tags_file for (get eof tags_file) ¬
45
- as «class utf8» using delimiter line_feed)
46
- set tag_index to 0
47
- set tag_length to length of tags
48
- close access tags_file
49
-
50
- set text_content to ""
51
-
52
- -- iterate through paragraphs
53
- set paragraph_index to 0
54
- set prev_paragraph_index to 0
55
- set is_new_tag to false
56
-
57
- repeat with current_line in tags
58
- if current_line starts with "@@ " then
59
- set space_offset to my parse_header(current_line)
60
- set paragraph_index to text 4 thru space_offset ¬
61
- of current_line as number
62
- set is_new_tag to true
63
- else if current_line starts with "+" then
64
- if paragraph_index is not 0 then
65
- set prev_paragraph_index to paragraph_index
66
-
67
- -- handle blank lines with soft returns
68
- if length of current_line is less than 2
69
- set this_content to ""
70
- else
71
- set this_content to text 2 thru (length of current_line) ¬
72
- of current_line
73
- end if
74
-
75
- -- add to text content, joining with a soft return for multiple lines
76
- if text_content is "" then
77
- set text_content to this_content
78
- else
79
- set text_content to (text_content & vertical_tab & this_content)
80
- end if
81
- set is_new_tag to false
82
- end if
83
- end if
84
-
85
- -- increment tag index
86
- set tag_index to tag_index + 1
87
-
88
- -- write if we've moved to a new tag or reached the end of the file
89
- if text_content is not "" and (tag_index is tag_length or is_new_tag)
90
- try
91
- set current_content to text object of paragraph prev_paragraph_index ¬
92
- of active document
93
- set my_range to create range active document ¬
94
- start (start of content of current_content) ¬
95
- end (end of content of current_content)
96
-
97
- select my_range
98
-
99
- -- FIXME: this is super-ugly
100
- if count of paragraphs of selection is greater than 1
101
- set my_range to create range active document ¬
102
- start (start of content of text object of selection) ¬
103
- end ((end of content of text object of selection) - 1)
104
- select my_range
105
- repeat while (count of paragraphs of selection is greater than 1)
106
- -- maybe?
107
- move start of range my_range by a line item
108
- select my_range
109
- end repeat
110
- end if
111
-
112
- repeat while my ends_with_forbidden_character(content of selection as text)
113
- set my_range to create range active document ¬
114
- start (start of content of text object of selection) ¬
115
- end ((end of content of text object of selection) - 1)
116
- select my_range
117
- end repeat
118
-
119
- type text selection text text_content
120
- set text_content to ""
121
- end try
122
- end if
123
- end repeat
124
- end tell
125
- end import
126
-
127
- -- initialize global variables
128
- to init_globals()
129
- set numbers_as_text to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
130
- set line_feed to ASCII character 10
131
- set vertical_tab to ASCII character 11
132
- set carriage_return to ASCII character 13
133
- end init_globals
134
-
135
- -- main()
136
- on run argv
137
- init_globals()
138
- import(item 1 of argv)
139
- end run
data/lib/konjac/os.rb DELETED
@@ -1,51 +0,0 @@
1
- module Konjac
2
- # A module for determining what OS we're using
3
- module OS
4
- class << self
5
- # Override string output
6
- def to_s
7
- RbConfig::CONFIG["host_os"]
8
- end
9
-
10
- # OS is a version of Mac
11
- def mac?
12
- @mac ||= is? /mac|darwin/
13
- end
14
-
15
- # OS is a version of Linux
16
- def linux?
17
- @linux ||= is? /linux|cygwin/
18
- end
19
-
20
- # OS is a version of Windows
21
- def windows?
22
- @windows ||= is? /mswin|^win|mingw/
23
- end
24
-
25
- # OS is POSIX (well, actually, this makes the somewhat bad assumption
26
- # that non-Windows OSes are POSIX)
27
- def posix?
28
- !windows?
29
- end
30
-
31
- # Throws a message if the computer is using a command restricted to Macs
32
- def not_a_mac
33
- if mac?
34
- return false
35
- else
36
- puts I18n.t(:mac_only)
37
- return true
38
- end
39
- end
40
-
41
-
42
- private
43
-
44
- # Test whether the +host_os+ configuration parameter matches a specified
45
- # regular expression
46
- def is?(match)
47
- match === RbConfig::CONFIG["host_os"]
48
- end
49
- end
50
- end
51
- end
data/lib/konjac/tag.rb DELETED
@@ -1,50 +0,0 @@
1
- # coding: utf-8
2
- module Konjac
3
- # A tag containing original text and (if available) its translation, plus the
4
- # index of the <w:t> tag extracted from its .docx document
5
- class Tag
6
- # The index of the <w:t> tag in the cleaned-up XML file output by
7
- # Konjac::Word.export_docx_tags
8
- attr_accessor :index
9
-
10
- # The original text
11
- attr_accessor :original
12
-
13
- # The translated text
14
- attr_accessor :translated
15
-
16
- # Creates a new tag.
17
- #
18
- # t = Tag.new(1, "dog", "犬")
19
- def initialize(index, original, translated)
20
- @index, @original, @translated = index, original, translated
21
- end
22
-
23
- # Converts the Tag into a string for use in .konjac files
24
- #
25
- # <tt>Tag.new(1, "dog", "犬").to_s</tt> will output
26
- #
27
- # [[KJ-1]]
28
- # > dog
29
- # 犬
30
- #
31
- # whereas <tt>Tag.new(2, "cat").to_s</tt> will output
32
- #
33
- # [[KJ-2]]
34
- # > cat
35
- def to_s
36
- "[[KJ-#{index}]]\n> #{original}#{"\n" + translated if translated?}"
37
- end
38
-
39
- # Whether the tag has been translated
40
- #
41
- # dog = Tag.new(1, "dog", "犬")
42
- # dog.translated? # => true
43
- #
44
- # cat = Tag.new(1, "cat", "")
45
- # cat.translated? # => false
46
- def translated?
47
- !translated.nil? && translated != ""
48
- end
49
- end
50
- end
@@ -1,65 +0,0 @@
1
- module Konjac
2
- # A class for managing the tags generated by extraction from {Microsoft
3
- # Word}[http://office.microsoft.com/en-us/word/] 2003+ documents
4
- class TagManager
5
- # A list of the Tag objects current being managed
6
- attr_accessor :tags
7
-
8
- # A regex to match lines that start with <tt>-</tt>, that is, lines that
9
- # indicate removed text in .diff files
10
- OLD_TAG = /^-[^-]/
11
-
12
- # A regex to match lines that start with <tt>+</tt>, that is, lines that
13
- # indicate added text in .diff files
14
- NEW_TAG = /^\+[^+]/
15
-
16
- # Regex for matching index numbers
17
- KONJAC_TAG = /^@@\s(\d+).*@@$/
18
-
19
- # Creates a new TagManager
20
- def initialize(path)
21
- @tags = []
22
- parse_lines File.readlines(path)
23
- end
24
-
25
- # Parses the lines of a file into Tag objects
26
- def parse_lines(lines)
27
- index = nil
28
- orig = nil
29
- trans = nil
30
-
31
- lines.each do |line|
32
- if line =~ KONJAC_TAG
33
- # Handle instances where there is no translation
34
- unless orig.nil?
35
- @tags << Tag.new(index, orig, trans)
36
- index = nil
37
- orig = nil
38
- end
39
-
40
- index = line.match(KONJAC_TAG)[1].to_i
41
- elsif line =~ OLD_TAG
42
- orig = line[1..-1].chomp
43
- elsif line =~ NEW_TAG
44
- trans = line[1..-1].chomp
45
- unless index.nil?
46
- @tags << Tag.new(index, orig, trans)
47
- index = nil
48
- orig = nil
49
- trans = nil
50
- end
51
- end
52
- end
53
- end
54
-
55
- # A list of all the tags available
56
- def all
57
- @tags
58
- end
59
-
60
- # Finds the <em>nth</em> tag being managed
61
- def [](index)
62
- @tags[index]
63
- end
64
- end
65
- end
data/spec/tag_spec.rb DELETED
@@ -1,63 +0,0 @@
1
- # coding: utf-8
2
- require File.dirname(__FILE__) + "/spec_helper"
3
- require "tempfile"
4
-
5
- describe Tag do
6
- before :each do
7
- @tags_file = Tempfile.new(["tags", ".tags"])
8
- @tags_file.write <<-eos.gsub(/^\s+/, "")
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 --
25
- eos
26
- @tags_file.rewind
27
- @manager = TagManager.new(@tags_file.path)
28
- end
29
-
30
- it "should accurately read a tag" do
31
- @manager.all.should_not == nil
32
- @manager[0].index.should == 0
33
- @manager[0].original.should == "犬"
34
- @manager[0].translated.should == "dog"
35
- end
36
-
37
- it "should succeed reading multiple tags" do
38
- @manager[1].index.should == 1
39
- @manager[1].original.should == "何ですか。"
40
- @manager[1].translated.should == "What is it?"
41
- end
42
-
43
- it "should ignore blank translations" do
44
- @manager[2].index.should == 2
45
- @manager[2].original.should == "空白"
46
- @manager[2].translated.should == nil
47
- end
48
-
49
- it "should know whether a tag has been translated" do
50
- @manager[0].translated?.should == true
51
- @manager[2].translated?.should == false
52
- end
53
-
54
- it "should ignore comments and additional info after an index tag" do
55
- @manager[3].index.should == 3
56
- @manager[3].original.should == "コメント"
57
- @manager[3].translated.should == "Comment"
58
- end
59
-
60
- it "should skip over blank indexes" do
61
- @manager[4].index.should == 5
62
- end
63
- end