Module: RD2ODT
- Defined in:
- lib/rd2odt.rb
Defined Under Namespace
Classes: Error, InnerObject, ProgramOptionError, ProgramOptionParseError, RD2ODTVisitor
Constant Summary
collapse
- OPTION_PARSER =
OptionParser.new
- @@options =
{
:backtrace => false,
:template => nil,
}
Class Method Summary
collapse
Class Method Details
.ah_to_xml(o) ⇒ Object
129
130
131
|
# File 'lib/rd2odt.rb', line 129
def self.ah_to_xml(o)
return __send__("ah_to_xml_by_" + o.class.name.downcase, o)
end
|
.ah_to_xml_by_array(ary) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/rd2odt.rb', line 133
def self.ah_to_xml_by_array(ary)
if ary.first.is_a?(Array) ||
ary.first.is_a?(Symbol) && /<.*>/ === ary.first.to_s
return ary.map { |item|
ah_to_xml(item)
}.join("\n")
end
ary = ary.dup
result = "<"
tag_name = ah_to_xml(ary.shift)
result << tag_name
if Hash === ary.first
h = ary.shift
result << ah_to_xml_by_hash(h)
end
if ary.empty?
result << " />"
return result
end
result << ">"
ary.each do |item|
case item
when Array
result << "\n"
result << ah_to_xml_by_array(item).gsub(/^/, " ")
result << "\n"
else
result << ah_to_xml(item)
end
end
result << "</" + tag_name + ">"
return result
end
|
.ah_to_xml_by_hash(h) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/rd2odt.rb', line 185
def self.ah_to_xml_by_hash(h)
return h.keys.sort_by { |item|
item.to_s
}.map { |key|
converted_key = ah_to_xml_by_symbol(key)
value = h[key]
converted_value = ah_to_xml_by_string(value)
" " + converted_key + "=" + '"' + converted_value + '"'
}.join
end
|
.ah_to_xml_by_string(s) ⇒ Object
198
199
200
|
# File 'lib/rd2odt.rb', line 198
def self.ah_to_xml_by_string(s)
return CGI.escapeHTML(s.to_s)
end
|
.ah_to_xml_by_symbol(symbol) ⇒ Object
181
182
183
|
# File 'lib/rd2odt.rb', line 181
def self.ah_to_xml_by_symbol(symbol)
return symbol.to_s.gsub("__", ":").gsub("_", "-")
end
|
.create_odt(visitor, doc, output_path, template_path) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/rd2odt.rb', line 95
def self.create_odt(visitor, doc, output_path, template_path)
current_path = Dir.pwd
output_absolute_path = File.expand_path(output_path)
template_absolute_path = File.expand_path(template_path)
Dir.mktmpdir do |tmpdir|
Dir.chdir(tmpdir) do
unzip(template_absolute_path)
open("styles.xml", "r+") do |f|
operate_styles_xml(f, visitor.additional_styles)
end
open("content.xml", "w") do |f|
f.puts('<?xml version="1.0" encoding="UTF-8"?>')
f.puts
f.puts(ah_to_xml(doc))
end
visitor.inner_objects.each do |inner_object|
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
unzip(File.join(current_path, inner_object.path))
from = inner_object.href
to = File.join(tmpdir, inner_object.fixed_href)
FileUtils.mkdir_p(File.dirname(to))
FileUtils.mv(from, to)
end
end
end
zip(output_absolute_path)
end
end
end
|
.main(argv) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/rd2odt.rb', line 55
def main(argv)
parse_option(argv)
include_paths = [
File.dirname(@@input_path),
File.dirname(@@output_path),
]
puts("input_path: " + @@input_path.inspect) if $DEBUG
puts("output_path: " + @@output_path.inspect) if $DEBUG
puts("options: " + options.inspect) if $DEBUG
puts("include_paths: " + include_paths.inspect) if $DEBUG
input_lines = treat_input(File.readlines(@@input_path))
tree = RD::RDTree.new(input_lines, include_paths, nil)
tree.parse
visitor = RD2ODTVisitor.new
doc = visitor.visit(tree)
create_odt(visitor, doc, @@output_path, options[:template])
rescue Error => e
e.process
end
|
.operate_styles_xml(io, additional_styles) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/rd2odt.rb', line 202
def self.operate_styles_xml(io, additional_styles)
parser = REXML::Document.new(io.read)
office_styles = parser.elements["/office:document-styles/office:styles"]
additional_styles.each do |element|
office_styles.add_element(element)
end
io.rewind
io.truncate(0)
io.write(parser.to_s)
end
|
.options ⇒ Object
27
28
29
|
# File 'lib/rd2odt.rb', line 27
def options
return @@options
end
|
.parse_option(argv) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/rd2odt.rb', line 32
def parse_option(argv)
begin
OPTION_PARSER.parse!(argv)
rescue OptionParser::ParseError => e
raise ProgramOptionParseError, e
end
@@input_path = argv.shift
if @@input_path.nil?
raise ProgramOptionError, "no input file path."
end
@@output_path =
argv.shift ||
(@@input_path == "-" ? "output.odt" : @@input_path + ".odt")
if options[:template].nil?
options[:template] = @@input_path + ".ott"
end
@@input_path
end
|
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/rd2odt.rb', line 79
def self.treat_input(lines)
result = lines.dup
if lines.grep(/^=begin\b/).empty? &&
lines.grep(/^=end\b/).empty?
result.unshift("=begin\n")
if !(/\n\z/ === result[-1])
result[-1] = result[-1] + "\n"
end
result.push("=end\n")
end
return result
end
|
.unzip(input_path) ⇒ Object
unzip to current directory.
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/rd2odt.rb', line 235
def self.unzip(input_path)
Zip::ZipFile.foreach(input_path) do |zip_entry|
path = zip_entry.name
if zip_entry.directory?
FileUtils.mkdir_p(path)
elsif zip_entry.file?
FileUtils.mkdir_p(File.dirname(path))
zip_entry.get_input_stream do |input|
open(path, "w") do |output|
output.write(input.read)
end
end
end
end
end
|
.zip(output_path) ⇒ Object
create zip file by current directory.
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
# File 'lib/rd2odt.rb', line 215
def self.zip(output_path)
FileUtils.rm_f(output_path)
Zip::ZipFile.open(output_path, Zip::ZipFile::CREATE) do |zip_file|
Find.find(".") do |path_orig|
path = path_orig.sub(/\A\.\//, "")
if File.file?(path)
zip_file.get_output_stream(path) do |f|
f.write(File.read(path))
end
elsif File.directory?(path)
zip_file.mkdir(path)
end
end
end
end
|