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

very lazy formatter



132
133
134
# File 'lib/rd2odt.rb', line 132

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



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
180
181
182
# File 'lib/rd2odt.rb', line 136

def self.ah_to_xml_by_array(ary)
  if ary.first.is_a?(Array) ||
    ary.first.is_a?(Symbol) && /<.*>/ === ary.first.to_s
    # This case is:
    #   [[:tag], [:tag]]
    #       |
    #       v
    #   <tag></tag>
    #   <tag></tag>
    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



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/rd2odt.rb', line 188

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



201
202
203
# File 'lib/rd2odt.rb', line 201

def self.ah_to_xml_by_string(s)
  return CGI.escapeHTML(s.to_s)
end

.ah_to_xml_by_symbol(symbol) ⇒ Object



184
185
186
# File 'lib/rd2odt.rb', line 184

def self.ah_to_xml_by_symbol(symbol)
  return symbol.to_s.gsub("__", ":").gsub("_", "-")
end

.create_odt(visitor, doc, output_path, template_path) ⇒ Object



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
127
128
129
# File 'lib/rd2odt.rb', line 98

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
      # todo: test
      # todo: extract only inner_object.href for more optimizing.
      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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rd2odt.rb', line 58

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



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/rd2odt.rb', line 205

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

.optionsObject



30
31
32
# File 'lib/rd2odt.rb', line 30

def options
  return @@options
end

.parse_option(argv) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rd2odt.rb', line 35

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

.treat_input(lines) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rd2odt.rb', line 82

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.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/rd2odt.rb', line 238

def self.unzip(input_path)
  # if !system("unzip", "-q", input_path)
  #   raise "unzip failure: #{input_path.inspect}"
  # end
  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.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/rd2odt.rb', line 218

def self.zip(output_path)
  # if !system("zip", "-9qr", output_path, ".")
  #   raise "zip failure: #{output_path.inspect}"
  # end
  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\.\//, "")  # remove "./"
      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