Class: Vivlio::PDF::Viewer

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio/pdf/viewer.rb

Overview

The Vivliostyle Viewer installation used for rendering: a directory of static files containing index.html.

Defaults to the copy vendored in this gem; pass another path to run a different release without rebuilding the gem.

Constant Summary collapse

VENDORED_PATH =
File.expand_path('../../../vendor/viewer', __dir__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Viewer

Returns a new instance of Viewer.



30
31
32
33
34
35
# File 'lib/vivlio/pdf/viewer.rb', line 30

def initialize(path)
  @path = File.expand_path(path.to_s)
  @index = LocalFile.new(File.join(@path, 'index.html'), kind: 'Vivliostyle Viewer')
  @version = read_version
  freeze
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/vivlio/pdf/viewer.rb', line 15

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version.



15
16
17
# File 'lib/vivlio/pdf/viewer.rb', line 15

def version
  @version
end

Class Method Details

.coerce(value) ⇒ Object

Accepts a Viewer, a path, or nil (meaning the vendored viewer).



22
23
24
25
26
27
28
# File 'lib/vivlio/pdf/viewer.rb', line 22

def self.coerce(value)
  case value
  when Viewer then value
  when nil then default
  else new(value)
  end
end

.defaultObject



17
18
19
# File 'lib/vivlio/pdf/viewer.rb', line 17

def self.default
  @default ||= new(VENDORED_PATH)
end

Instance Method Details

#index_pathObject



37
38
39
# File 'lib/vivlio/pdf/viewer.rb', line 37

def index_path
  @index.path
end

#to_sObject



61
62
63
# File 'lib/vivlio/pdf/viewer.rb', line 61

def to_s
  path
end

#url_for(source, book_mode: true, style: nil) ⇒ Object

Viewer URL loading source.

Fragment parameters:

src              document to load
bookMode         follow the spine/TOC to load the whole publication
renderAllPages   paginate everything up front (required before print)
style            additional stylesheet(s)

A stylesheet the viewer cannot find is silently ignored, so the paths are resolved here and a missing one is reported instead.



51
52
53
54
55
56
57
58
59
# File 'lib/vivlio/pdf/viewer.rb', line 51

def url_for(source, book_mode: true, style: nil)
  parameters = ["src=#{Source.coerce(source).url}"]
  parameters << 'bookMode=true' if book_mode
  parameters << 'renderAllPages=true'
  Array(style).each do |sheet|
    parameters << "style=#{LocalFile.new(sheet, kind: 'stylesheet').url}"
  end
  "#{@index.url}##{parameters.join('&')}"
end