Class: RD2ODT::RD2ODTVisitor

Inherits:
RD::RDVisitor show all
Defined in:
lib/rd2odt.rb

Constant Summary collapse

DO_NOT_INCLUDE_TAG_NAMES =
["office:forms", "text:sequence-decls"]

Constants inherited from RD::OutputFormatVisitor

RD::OutputFormatVisitor::INCLUDE_SUFFIX, RD::OutputFormatVisitor::OUTPUT_SUFFIX

Constants inherited from RD::Visitor

RD::Visitor::SYSTEM_NAME, RD::Visitor::SYSTEM_VERSION, RD::Visitor::VERSION

Instance Attribute Summary collapse

Attributes inherited from RD::OutputFormatVisitor

#filename, #include_suffix

Instance Method Summary collapse

Methods inherited from RD::RDVisitor

#apply_to_DescListItemTerm, #apply_to_MethodListItemTerm, #prepare_labels, #refer_external

Methods inherited from RD::Visitor

define_visit_Nonterminal, define_visit_Terminal, version, #visit, #visit_DescListItem, #visit_MethodListItem, #visit_Reference, #visit_children

Constructor Details

#initialize(*args) ⇒ RD2ODTVisitor

Returns a new instance of RD2ODTVisitor.



271
272
273
274
275
276
277
278
# File 'lib/rd2odt.rb', line 271

def initialize(*args)
  super

  self.number_of_include_files = 0
  self.additional_styles = []
  self.automatic_styles = []
  self.inner_objects = []
end

Instance Attribute Details

#additional_stylesObject

for styles.xml#/office:document-styles/office:styles



264
265
266
# File 'lib/rd2odt.rb', line 264

def additional_styles
  @additional_styles
end

#automatic_stylesObject

for content.xml#/office:document-content/office:automatic-styles



261
262
263
# File 'lib/rd2odt.rb', line 261

def automatic_styles
  @automatic_styles
end

#continue_numbering_headlineObject

Returns the value of attribute continue_numbering_headline.



258
259
260
# File 'lib/rd2odt.rb', line 258

def continue_numbering_headline
  @continue_numbering_headline
end

#inner_objectsObject

included OLE objects



269
270
271
# File 'lib/rd2odt.rb', line 269

def inner_objects
  @inner_objects
end

#number_of_include_filesObject

Returns the value of attribute number_of_include_files.



266
267
268
# File 'lib/rd2odt.rb', line 266

def number_of_include_files
  @number_of_include_files
end

Instance Method Details

#apply_to_DocumentElement(element, sub_content) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/rd2odt.rb', line 280

def apply_to_DocumentElement(element, sub_content)
  result =
    [:office__document_content,
     {
       :xmlns__office =>
       "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
       :xmlns__style => "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
       :xmlns__text => "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
       :xmlns__table => "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
       :xmlns__draw => "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
       :xmlns__fo =>
       "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
       :xmlns__xlink => "http://www.w3.org/1999/xlink",
       :xmlns__dc => "http://purl.org/dc/elements/1.1/",
       :xmlns__meta => "urn:oasis:names:tc:opendocument:xmlns:meta:1.0",
       :xmlns__number =>
       "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
       :xmlns__svg =>
       "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
       :xmlns__chart => "urn:oasis:names:tc:opendocument:xmlns:chart:1.0",
       :xmlns__dr3d => "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0",
       :xmlns__math => "http://www.w3.org/1998/Math/MathML",
       :xmlns__form => "urn:oasis:names:tc:opendocument:xmlns:form:1.0",
       :xmlns__script =>
       "urn:oasis:names:tc:opendocument:xmlns:script:1.0",
       :xmlns__ooo => "http://openoffice.org/2004/office",
       :xmlns__ooow => "http://openoffice.org/2004/writer",
       :xmlns__oooc => "http://openoffice.org/2004/calc",
       :xmlns__dom => "http://www.w3.org/2001/xml-events",
       :xmlns__xforms => "http://www.w3.org/2002/xforms",
       :xmlns__xsd => "http://www.w3.org/2001/XMLSchema",
       :xmlns__xsi => "http://www.w3.org/2001/XMLSchema-instance",
       :xmlns__field =>
       "urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:field:1.0",
       :office__version => "1.1",
     },
     [:office__scripts],
     [:office__font_face_decls,
      [:style__font_face,
       {
         :style__name => "さざなみ明朝",
         :svg__font_family => "さざなみ明朝",
         :style__font_family_generic => "roman",
         :style__font_pitch => "variable",
       }],
      [:style__font_face,
       {
         :style__name => "IPAゴシック",
         :svg__font_family => "IPAゴシック",
         :style__font_family_generic => "swiss",
         :style__font_pitch => "variable",
       }],
      [:style__font_face,
       {
         :style__name => "IPAゴシック1",
         :svg__font_family => "IPAゴシック",
         :style__font_family_generic => "system",
         :style__font_pitch => "variable",
       }],
     ], # :office__font_face_decls
     [:office__automatic_styles,
      *self.automatic_styles.map { |element|
        element.to_s.to_sym
      }],
     [:office__body,
      [:office__text,
       [:text__sequence_decls,
        [:text__sequence_decl,
         {
           :text__display_outline_level => "0",
           :text__name => "Illustration",
         }],
        [:text__sequence_decl,
         {
           :text__display_outline_level => "0",
           :text__name => "Table",
         }],
        [:text__sequence_decl,
         {
           :text__display_outline_level => "0",
           :text__name => "Text",
         }],
        [:text__sequence_decl,
         {
           :text__display_outline_level => "0",
           :text__name => "Drawing",
         }],
       ], # :text__sequence_decls
       *sub_content
      ], # :office__text
     ], # :office__body
    ] # :office__document_content
  return result
end

#apply_to_EnumList(element, items) ⇒ Object



415
416
417
418
419
# File 'lib/rd2odt.rb', line 415

def apply_to_EnumList(element, items)
  return apply_to_list(items,
                       :text__style_name => "Numbering_20_1",
                       :text__continue_numbering => "false")
end

#apply_to_EnumListItem(element, sub_contents) ⇒ Object



430
431
432
# File 'lib/rd2odt.rb', line 430

def apply_to_EnumListItem(element, sub_contents)
  return apply_to_list_item(sub_contents)
end

#apply_to_Headline(element, title) ⇒ Object



404
405
406
407
408
409
410
411
412
413
# File 'lib/rd2odt.rb', line 404

def apply_to_Headline(element, title)
  level = element.level
  result = create_headline_result(title, level, level)
  result[1][:text__style_name] = "Numbering_20_2"
  if level == 1 && !continue_numbering_headline
    result[1].delete(:text__continue_numbering)
  end
  self.continue_numbering_headline = true
  return result
end

#apply_to_Include(element) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/rd2odt.rb', line 463

def apply_to_Include(element)
  self.number_of_include_files += 1
  name_prefix = create_name_prefix
  path = search_file(element.tree.include_paths, element.filename)

  append_children = []
  content_xml = read_file_in_zip(path, "content.xml")
  parser = REXML::Document.new(content_xml)
  office_text =
    parser.elements["/office:document-content/office:body/office:text"]
  apply_prefix_to_xlink_href(path, office_text, name_prefix) # todo: test
  [
   "text:style-name",
   "table:style-name",
   "table:name",
   "draw:style-name",
  ].each do |attribute_key|
    apply_prefix_to_all_of_style_name(office_text, attribute_key,
                                      name_prefix)
  end
  office_text.each_element do |child|
    # may use XPath.
    next if DO_NOT_INCLUDE_TAG_NAMES.include?(child.expanded_name)
    append_children << child.to_s.to_sym
  end

  office_automatic_styles =
    parser.elements["/office:document-content/office:automatic-styles"]
  apply_prefix_to_all_of_style_name(office_automatic_styles,
                                    "style:name", name_prefix)
  office_automatic_styles.each_element do |child|
    self.automatic_styles << child.deep_clone
  end

  styles_xml = read_file_in_zip(path, "styles.xml")
  parser = REXML::Document.new(styles_xml)
  office_styles = parser.elements["/office:document-styles/office:styles"]
  [
   "style:name",
   "style:parent-style-name",
   "style:display-name",
  ].each do |attribute_key|
    apply_prefix_to_all_of_style_name(office_styles, attribute_key,
                                      name_prefix)
  end
  office_styles.elements.each("style:style") do |element|
    self.additional_styles << element
  end

  return append_children
end

#apply_to_ItemList(element, items) ⇒ Object



421
422
423
# File 'lib/rd2odt.rb', line 421

def apply_to_ItemList(element, items)
  return apply_to_list(items, :text__style_name => "List_20_1")
end

#apply_to_ItemListItem(element, sub_contents) ⇒ Object



434
435
436
# File 'lib/rd2odt.rb', line 434

def apply_to_ItemListItem(element, sub_contents)
  return apply_to_list_item(sub_contents)
end

#apply_to_StringElement(element) ⇒ Object



382
383
384
# File 'lib/rd2odt.rb', line 382

def apply_to_StringElement(element)
  return element.content.gsub(/[\r\n]+/m, "")
end

#apply_to_TextBlock(element, sub_contents) ⇒ Object



375
376
377
378
379
380
# File 'lib/rd2odt.rb', line 375

def apply_to_TextBlock(element, sub_contents)
  return [:text__p,
          {:text__style_name => "Text_20_body"},
          *sub_contents
         ]
end

#apply_to_Verbatim(element) ⇒ Object



443
444
445
446
447
448
449
450
451
# File 'lib/rd2odt.rb', line 443

def apply_to_Verbatim(element)
  lines = element.content.map { |line|
    escape_text(line.chomp)
  }
  return [:text__p,
          {:text__style_name=>"Preformatted_20_Text"},
          lines.join("<text:line-break />").to_sym,
         ]
end