Class: Typst::Base

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

Direct Known Subclasses

Html, HtmlExperimental, Pdf, Png, Query, Svg

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/base.rb', line 6

def initialize(*options)
  if options.size.zero?
    raise "No options given"
  elsif options.first.is_a?(String)
    file, options = options
    options ||= {}
    options[:file] = file
  elsif options.first.is_a?(Hash)
    options = options.first
  end

  if options.has_key?(:file)
    raise "Can't find file" unless File.exist?(options[:file])
  elsif options.has_key?(:body)
    raise "Empty body" if options[:body].to_s.empty?
  elsif options.has_key?(:zip)
    raise "Can't find zip" unless File.exist?(options[:zip])
  else
    raise "No input given"
  end

  root = Pathname.new(options[:root] || ".").expand_path
  raise "Invalid path for root" unless root.exist?
  options[:root] = root.to_s

  font_paths = (options[:font_paths] || []).collect{ |fp| Pathname.new(fp).expand_path }
  options[:font_paths] = font_paths.collect(&:to_s)

  options[:dependencies] ||= {}
  options[:fonts] ||= {}
  options[:sys_inputs] ||= {}

  self.options = options
end

Instance Attribute Details

#compiledObject

Returns the value of attribute compiled.



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

def compiled
  @compiled
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.from_s(main_source, **options) ⇒ Object



45
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
74
75
# File 'lib/base.rb', line 45

def self.from_s(main_source, **options)
  dependencies = options[:dependencies] ||= {}
  fonts = options[:fonts] ||= {}

  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

    options[:file] = tmp_main_file
    options[:root] = tmp_dir
    options[:font_paths] = [relative_font_path]

    if options[:format]
      Typst::formats[options[:format]].new(**options)
    else
      new(**options)
    end
  end
end

.from_zip(zip_file_path, main_file = nil, **options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/base.rb', line 77

def self.from_zip(zip_file_path, main_file = nil, **options)
  options[:dependencies] ||= {}
  options[: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|
      options[: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|
      options[:fonts][Pathname.new(font_name).basename.to_s] = zipfile.file.read(font_name)
    end

    options[:main_file] = tmp_main_file

    from_s(main_source, **options)
  end
end

Instance Method Details

#bytesObject



159
160
161
162
# File 'lib/base.rb', line 159

def bytes
  STDERR.puts "DEPRECATION WARNING: this method will go away in a future version"
  compiled.bytes
end

#compile(format, **options) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/base.rb', line 133

def compile(format, **options)
  raise "Invalid format" if Typst::formats[format].nil?

  options = self.options.merge(options)

  if options.has_key?(:file)
    Typst::formats[format].new(**options).compiled
  elsif options.has_key?(:body)
    Typst::formats[format].from_s(options[:body], **options).compiled
  elsif options.has_key?(:zip)
    Typst::formats[format].from_zip(options[:zip], options[:main_file], **options).compiled
  else
    raise "No input given"
  end
end

#documentObject



154
155
156
157
# File 'lib/base.rb', line 154

def document
  STDERR.puts "DEPRECATION WARNING: this method will go away in a future version"
  compiled.document
end

#pagesObject



164
165
166
167
# File 'lib/base.rb', line 164

def pages
  STDERR.puts "DEPRECATION WARNING: this method will go away in a future version"
  compiled.pages
end

#typst_argsObject



41
42
43
# File 'lib/base.rb', line 41

def typst_args
  [options[:file], options[:root], options[:font_paths], File.dirname(__FILE__), false, options[:sys_inputs].map{ |k,v| [k.to_s,v.to_s] }.to_h]
end

#with_dependencies(dependencies) ⇒ Object



108
109
110
111
# File 'lib/base.rb', line 108

def with_dependencies(dependencies)
  self.options[:dependencies] = self.options[:dependencies].merge(dependencies)
  self
end

#with_font_paths(font_paths) ⇒ Object



123
124
125
126
# File 'lib/base.rb', line 123

def with_font_paths(font_paths)
  self.options[:font_paths] = self.options[:font_paths] + font_paths
  self
end

#with_fonts(fonts) ⇒ Object



113
114
115
116
# File 'lib/base.rb', line 113

def with_fonts(fonts)
  self.options[:fonts] = self.options[:fonts].merge(fonts)
  self
end

#with_inputs(inputs) ⇒ Object



118
119
120
121
# File 'lib/base.rb', line 118

def with_inputs(inputs)
  self.options[:sys_inputs] = self.options[:sys_inputs].merge(inputs)
  self
end

#with_root(root) ⇒ Object



128
129
130
131
# File 'lib/base.rb', line 128

def with_root(root)
  self.options[:root] = root
  self
end

#write(output) ⇒ Object



149
150
151
152
# File 'lib/base.rb', line 149

def write(output)
  STDERR.puts "DEPRECATION WARNING: this method will go away in a future version"
  compiled.write(output)
end