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
# 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[:resource_path] ||= File.dirname(__FILE__)
  options[:ignore_system_fonts] ||= 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



56
57
58
59
60
61
62
63
64
65
# File 'lib/base.rb', line 56

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



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

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/base.rb', line 103

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

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



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

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, :resource_path, :ignore_system_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, :resource_path, :ignore_system_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, :resource_path, :ignore_system_fonts, :sys_inputs)))
    end
  else
    raise "No input given"
  end
end

#typst_argsObject



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

def typst_args
  [options[:file], options[:root], options[:font_paths], options[:resource_path], options[:ignore_system_fonts], options[:sys_inputs].map{ |k,v| [k.to_s,v.to_s] }.to_h]
end

#typst_pdf_argsObject



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

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

#typst_png_argsObject



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

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

#with_dependencies(dependencies) ⇒ Object



78
79
80
81
# File 'lib/base.rb', line 78

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

#with_font_paths(font_paths) ⇒ Object



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

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

#with_fonts(fonts) ⇒ Object



83
84
85
86
# File 'lib/base.rb', line 83

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

#with_inputs(inputs) ⇒ Object



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

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

#with_root(root) ⇒ Object



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

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