Class: Typst::Document

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ Document

Returns a new instance of Document.



5
6
7
# File 'lib/document.rb', line 5

def initialize(bytes)
  @bytes = bytes
end

Instance Attribute Details

#bytesObject

Returns the value of attribute bytes.



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

def bytes
  @bytes
end

Instance Method Details

#documentObject



33
34
35
# File 'lib/document.rb', line 33

def document
  pages.size == 1 ? pages.first : pages
end

#pagesObject



29
30
31
# File 'lib/document.rb', line 29

def pages
  bytes.collect{ |page| page.pack("C*").to_s }
end

#write_one(filename) ⇒ Object



17
18
19
# File 'lib/document.rb', line 17

def write_one(filename)
  File.write(filename, pages.first, mode: "wb")
end

#write_paged(base_filename) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/document.rb', line 21

def write_paged(base_filename)
  pages.each_with_index do |page, i|
    paged_filename = File.basename(base_filename, ".*") + "_{{n}}" + File.extname(base_filename) unless base_filename.include?("{{n}}")
    paged_filename = paged_filename.gsub("{{n}}", (i+1).to_s)
    File.write(paged_filename, page, mode: "wb")
  end
end

#write_some(filename) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/document.rb', line 9

def write_some(filename)
  if pages.size == 1
    write_one(filename)
  else
    write_paged(filename)
  end
end