Class: Typst::Base

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

Direct Known Subclasses

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
40
41
42
# 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] ||= {}
  options[:ignore_system_fonts] ||= false
  options[:ignore_embedded_fonts] ||= false
  options[:pretty] ||= false

  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



65
66
67
68
69
70
71
72
73
74
# File 'lib/base.rb', line 65

def self.from_s(main_source, **options)
  Typst::build_world_from_s(main_source, **options) do |opts|
    from_options = options.merge(opts)
    if from_options[:format]
      Typst::formats[from_options[:format]].new(**from_options)
    else
      new(**from_options)
    end
  end
end

.from_zip(zip_file_path, main_file = "main.typ", **options) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/base.rb', line 76

def self.from_zip(zip_file_path, main_file = "main.typ", **options)
  Typst::build_world_from_zip(zip_file_path, main_file, **options) do |opts|
    from_options = options.merge(opts)
    if from_options[:format]
      Typst::formats[from_options[:format]].new(**from_options)
    else
      new(**from_options)
    end
  end
end

Instance Method Details

#compile(format, **options) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/base.rb', line 122

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::build_world_from_s(self.options[:body], **options) do |opts|
      Typst::formats[format].new(**options.merge(opts)).compiled
    end
  elsif options.has_key?(:zip)
    main_file = options[:main_file]
    Typst::build_world_from_zip(options[:zip], main_file, **options) do |opts|
      Typst::formats[format].new(**options.merge(opts)).compiled
    end
  else
    raise "No input given"
  end
end

#pretty(pretty = true) ⇒ Object



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

def pretty(pretty = true)
  self.options[:pretty] = pretty
  self
end

#query(selector, field: nil, one: false, format: "json") ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/base.rb', line 143

def query(selector, field: nil, one: false, format: "json")
  query_options = { field: field, one: one, format: format }

  if self.options.has_key?(:file)
    Typst::Query.new(selector, self.options[:file], **query_options.merge(self.options.slice(:root, :font_paths, :ignore_system_fonts, :ignore_embedded_fonts, :sys_inputs)))
  elsif self.options.has_key?(:body)
    Typst::build_world_from_s(self.options[:body], **self.options) do |opts|
      Typst::Query.new(selector, opts[:file], **query_options.merge(opts.slice(:root, :font_paths, :ignore_system_fonts, :ignore_embedded_fonts, :sys_inputs)))
    end
  elsif self.options.has_key?(:zip)
    Typst::build_world_from_zip(self.options[:zip], **self.options) do |opts|
      Typst::Query.new(selector, opts[:file], **query_options.merge(opts.slice(:root, :font_paths, :ignore_system_fonts, :ignore_embedded_fonts, :sys_inputs)))
    end
  else
    raise "No input given"
  end
end

#typst_argsObject



48
49
50
# File 'lib/base.rb', line 48

def typst_args
  options.values_at(*typst_options).append(options[:sys_inputs].map{ |k,v| [k.to_s,v.to_s] }.to_h)
end

#typst_optionsObject



44
45
46
# File 'lib/base.rb', line 44

def typst_options
  [:file, :root, :font_paths, :ignore_system_fonts, :ignore_embedded_fonts]
end

#typst_pdf_argsObject



56
57
58
59
# File 'lib/base.rb', line 56

def typst_pdf_args
  options[:pdf_standards] ||= []
  [*typst_pretty_args, options[:pdf_standards]]
end

#typst_png_argsObject



61
62
63
# File 'lib/base.rb', line 61

def typst_png_args
  [*typst_args, options[:ppi]]
end

#typst_pretty_argsObject



52
53
54
# File 'lib/base.rb', line 52

def typst_pretty_args
  options.values_at(*typst_options, :pretty).append(options[:sys_inputs].map{ |k,v| [k.to_s,v.to_s] }.to_h)
end

#ugly(pretty = false) ⇒ Object



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

def ugly(pretty = false)
  self.pretty(pretty)
  self
end

#with_dependencies(dependencies) ⇒ Object



87
88
89
90
# File 'lib/base.rb', line 87

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

#with_font_paths(font_paths) ⇒ Object



102
103
104
105
# File 'lib/base.rb', line 102

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

#with_fonts(fonts) ⇒ Object



92
93
94
95
# File 'lib/base.rb', line 92

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

#with_inputs(inputs) ⇒ Object



97
98
99
100
# File 'lib/base.rb', line 97

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

#with_root(root) ⇒ Object



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

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