Class: Foliokit::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/foliokit/package.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, dir, root, root_package = nil) ⇒ Package

Returns a new instance of Package.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/foliokit/package.rb', line 6

def initialize(file, dir, root, root_package = nil)
  @file = file
  @dir = dir
  @root = root
  @root_package = root_package
  @dir.delete if @dir.exists? and !@dir.file("Folio.xml").exists?
  file.extract(@dir) unless @dir.exists?
  unwrap_folder! if @dir.files.none?
  wrap_article! if root_package.nil? and @dir.file("Article.xml").exists?
  if @dir.file("Folio.xml").exists?
    @manifest = Foliokit::ManifestFolio.new(@dir.file("Folio.xml"), self)
  elsif @dir.file("Article.xml").exists?
    @manifest = Foliokit::ManifestArticle.new(@dir.file("Article.xml"), self)
  else
    raise "invalid folio format"
  end
  @assets = {}
  @checksums = {}
end

Instance Attribute Details

#assetsObject

Returns the value of attribute assets.



4
5
6
# File 'lib/foliokit/package.rb', line 4

def assets
  @assets
end

#dirObject (readonly)

Returns the value of attribute dir.



3
4
5
# File 'lib/foliokit/package.rb', line 3

def dir
  @dir
end

#fileObject (readonly)

Returns the value of attribute file.



3
4
5
# File 'lib/foliokit/package.rb', line 3

def file
  @file
end

#manifestObject (readonly)

Returns the value of attribute manifest.



3
4
5
# File 'lib/foliokit/package.rb', line 3

def manifest
  @manifest
end

#rootObject (readonly)

Returns the value of attribute root.



3
4
5
# File 'lib/foliokit/package.rb', line 3

def root
  @root
end

#root_packageObject

Returns the value of attribute root_package.



4
5
6
# File 'lib/foliokit/package.rb', line 4

def root_package
  @root_package
end

Instance Method Details

#apply_asset(node, attribute, path, fallback_tag = nil, target_width = nil) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/foliokit/package.rb', line 44

def apply_asset(node, attribute, path, fallback_tag = nil, target_width = nil)
  src = store_asset(path, target_width)
  if src.nil?
    node.name = fallback_tag if fallback_tag.present?
  else
    node[attribute] = src
  end
end

#content_stacksObject



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

def content_stacks
  manifest.content_stacks
end

#idObject



26
27
28
# File 'lib/foliokit/package.rb', line 26

def id
  manifest.id
end

#store_asset(path, target_width = nil, bounds = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/foliokit/package.rb', line 53

def store_asset(path, target_width = nil, bounds = nil)
  parts = path.split("#")
  source = parts[0]
  index = (parts[1]&.to_i or 1) - 1
  source_file = dir.file(source)
  source_file.align_encoding! unless source_file.exists?
  return "#" unless source_file.exists?

  hash = Digest::MD5.hexdigest("#{dir.name}/#{source}/#{index}/#{bounds}")
  extension = source_file.extension
  asset_file = root.file("assets/#{hash}.#{extension}")
  asset_file.dir.create unless asset_file.dir.exists?
  checksum = "#{Digest::SHA256.file(source_file.to_s)}/#{index}/#{bounds}"
  return @checksums[checksum] if @checksums.key?(checksum)

  # generate image from pdf or copy file
  if source_file.mimetype == "application/pdf"
    # Convert PDF images
    doc = Poppler::Document.new(source_file.to_s)
    page = doc[index]
    surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, *page.size)
    cr = Cairo::Context.new(surface)
    cr.render_poppler_page(page)
    asset_file = root.file("assets/#{hash}.png")
    cr.target.write_to_png(asset_file.to_s)
    cr.target.finish
  else
    source_file.copy(asset_file)
  end

  # Crop to bounds
  asset_file.crop(bounds.to_h) unless bounds.nil?

  # Optimize image
  asset_file.optimize!

  # Cache and return result
  @checksums[checksum] = asset_file.relative_path(root)
  @checksums[checksum]
end

#subfolios(root_package) ⇒ Object



38
39
40
41
42
# File 'lib/foliokit/package.rb', line 38

def subfolios(root_package)
  manifest.subfolios.map do |element|
    Foliokit::Package.new(dir.file(element[:subfolio]), dir.dir(element[:id]), root, root_package)
  end
end

#valid_overlay_id?(id) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/foliokit/package.rb', line 34

def valid_overlay_id?(id)
  manifest.valid_overlay_id?(id)
end