Class: CooCoo::DataSources::Xournal::Saver

Inherits:
Object
  • Object
show all
Defined in:
lib/coo-coo/data_sources/xournal/saver.rb

Overview

TODO:

Keep linked images with the Xournal when it is saved.

Saves a Document

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSaver

Returns a new instance of Saver.



18
19
# File 'lib/coo-coo/data_sources/xournal/saver.rb', line 18

def initialize
end

Class Method Details

.save(doc, io_or_path) ⇒ Object

Saves doc to io_or_path using Xournal’s compressed XML format.

Parameters:

  • io_or_path (String, IO)

    File name or an IO



9
10
11
# File 'lib/coo-coo/data_sources/xournal/saver.rb', line 9

def self.save(doc, io_or_path)
  new.save(doc, io_or_path)
end

.to_xml(doc) ⇒ Object

Saves doc to an XML string.



14
15
16
# File 'lib/coo-coo/data_sources/xournal/saver.rb', line 14

def self.to_xml(doc)
  new.to_xml(doc)
end

Instance Method Details

#save(doc, io_or_path) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/coo-coo/data_sources/xournal/saver.rb', line 21

def save(doc, io_or_path)
  if io_or_path.respond_to?(:write)
    save_to_io(doc, io_or_path)
  elsif io_or_path.kind_of?(String)
    save_to_file(doc, io_or_path)
  else
    raise ArgumentError.new("Only paths as String and IO are supported outputs. Not #{io_or_path.class}")
  end
end

#to_xml(doc) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/coo-coo/data_sources/xournal/saver.rb', line 31

def to_xml(doc)
  Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.xournal(version: doc.version) do
      xml.title(doc.title)
      doc.pages.each do |p|
        page_to_xml(p, xml)
      end
    end
  end.to_xml
end