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, warnings = []) ⇒ Document

Returns a new instance of Document.



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

def initialize(bytes, warnings = [])
  @bytes = bytes
  @warnings = warnings
end

Instance Attribute Details

#bytesObject

Returns the value of attribute bytes.



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

def bytes
  @bytes
end

#warningsObject

Diagnostics the compiler emitted while succeeding. An unknown font family is the common one: Typst substitutes another face, the document generates, and nothing tells you unless you look here.



8
9
10
# File 'lib/document.rb', line 8

def warnings
  @warnings
end

Instance Method Details

#documentObject



43
44
45
# File 'lib/document.rb', line 43

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

#pagesObject



39
40
41
# File 'lib/document.rb', line 39

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

#warnings?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/document.rb', line 15

def warnings?
  !warnings.empty?
end

#write_one(filename) ⇒ Object



27
28
29
# File 'lib/document.rb', line 27

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

#write_paged(base_filename) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/document.rb', line 31

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



19
20
21
22
23
24
25
# File 'lib/document.rb', line 19

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