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
138
139
140
|
# File 'lib/rd2odt.rb', line 138
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
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
180
181
182
183
184
185
186
187
188
|
# File 'lib/rd2odt.rb', line 142
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
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/rd2odt.rb', line 194
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
207
208
209
|
# File 'lib/rd2odt.rb', line 207
def self.ah_to_xml_by_string(s)
return CGI.escapeHTML(s.to_s)
end
|
.ah_to_xml_by_symbol(symbol) ⇒ Object
190
191
192
|
# File 'lib/rd2odt.rb', line 190
def self.ah_to_xml_by_symbol(symbol)
return symbol.to_s.gsub("__", ":").gsub("_", "-")
end
|
.create_odt(visitor, doc, output_path, template_path) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/rd2odt.rb', line 104
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/rd2odt.rb', line 64
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
211
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/rd2odt.rb', line 211
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
36
37
38
|
# File 'lib/rd2odt.rb', line 36
def options
return @@options
end
|
.parse_option(argv) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/rd2odt.rb', line 41
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
|
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/rd2odt.rb', line 88
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.
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/rd2odt.rb', line 244
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.
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/rd2odt.rb', line 224
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
|