Class: CooCoo::DataSources::Xournal::Loader

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

Overview

Loads a Document.

Defined Under Namespace

Classes: Error, ParseError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Loader

Returns a new instance of Loader.



20
21
22
# File 'lib/coo-coo/data_sources/xournal/loader.rb', line 20

def initialize(doc)
  @doc = doc || Document.new
end

Class Method Details

.from_file(path) ⇒ Document

Loads a Xournal document from the file at path.

Returns:



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/coo-coo/data_sources/xournal/loader.rb', line 26

def self.from_file(path)
  doc = nil
  
  Zlib::GzipReader.open(path) do |io|
    doc = from_xml(io)
  end

  doc
rescue Zlib::GzipFile::Error
  from_regular_file(path)
end

.from_xml(data) ⇒ Object

Loads a Document from XML in a String.

Raises:



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/coo-coo/data_sources/xournal/loader.rb', line 39

def self.from_xml(data)
  xml = Nokogiri::XML(data)
  root = xml.xpath('//xournal')[0]
  raise ParseError.new("XML root is not 'xournal'") unless root
  title_el = root.xpath("title")
  title = title_el[0].text if title_el.size > 0
  
  self.
    new(Document.new(title, root['version'])).
    from_xml(xml)
end

Instance Method Details

#from_xml(xml) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/coo-coo/data_sources/xournal/loader.rb', line 51

def from_xml(xml)
  xml.xpath("//page").each do |page|
    @doc.add_page(load_page(page))
  end

  @doc
end