Class: Typst::Base

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

Direct Known Subclasses

Html, Pdf, Svg

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, root: ".", font_paths: []) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
# File 'lib/typst.rb', line 13

def initialize(input, root: ".", font_paths: [])
  self.input = input
  self.root = Pathname.new(root).expand_path.to_s
  self.font_paths = font_paths.collect{ |fp| Pathname.new(fp).expand_path.to_s }
end

Instance Attribute Details

#font_pathsObject

Returns the value of attribute font_paths.



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

def font_paths
  @font_paths
end

#inputObject

Returns the value of attribute input.



9
10
11
# File 'lib/typst.rb', line 9

def input
  @input
end

#rootObject

Returns the value of attribute root.



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

def root
  @root
end

Class Method Details

.from_s(main_source, dependencies: {}, fonts: {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/typst.rb', line 23

def self.from_s(main_source, dependencies: {}, fonts: {})
  dependencies = {} if dependencies.nil?
  fonts = {} if fonts.nil?
  Dir.mktmpdir do |tmp_dir|
    tmp_main_file = Pathname.new(tmp_dir).join("main.typ")
    File.write(tmp_main_file, main_source)

    dependencies.each do |dep_name, dep_source|
      tmp_dep_file = Pathname.new(tmp_dir).join(dep_name)
      File.write(tmp_dep_file, dep_source)
    end

    relative_font_path = Pathname.new(tmp_dir).join("fonts")
    fonts.each do |font_name, font_bytes|
      Pathname.new(relative_font_path).mkpath
      tmp_font_file = relative_font_path.join(font_name)
      File.write(tmp_font_file, font_bytes)
    end

    new(tmp_main_file, root: tmp_dir, font_paths: [relative_font_path])
  end
end

.from_zip(zip_file_path, main_file = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/typst.rb', line 46

def self.from_zip(zip_file_path, main_file = nil)
  dependencies = {}
  fonts = {}

  Zip::File.open(zip_file_path) do |zipfile|
    file_names = zipfile.dir.glob("*").collect{ |f| f.name }
    case
      when file_names.include?(main_file) then tmp_main_file = main_file
      when file_names.include?("main.typ") then tmp_main_file = "main.typ"
      when file_names.size == 1 then tmp_main_file = file_names.first
      else raise "no main file found"
    end
    main_source = zipfile.file.read(tmp_main_file)
    file_names.delete(tmp_main_file)
    file_names.delete("fonts/")

    file_names.each do |dep_name|
      dependencies[dep_name] = zipfile.file.read(dep_name)
    end

    font_file_names = zipfile.dir.glob("fonts/*").collect{ |f| f.name }
    font_file_names.each do |font_name|
      fonts[Pathname.new(font_name).basename.to_s] = zipfile.file.read(font_name)
    end

    from_s(main_source, dependencies: dependencies, fonts: fonts)
  end
end

Instance Method Details

#write(output) ⇒ Object



19
20
21
# File 'lib/typst.rb', line 19

def write(output)
  File.open(output, "wb"){ |f| f.write(document) }
end