Class: Typst::Base
- Inherits:
-
Object
- Object
- Typst::Base
- Defined in:
- lib/base.rb
Direct Known Subclasses
HtmlExperimental, Pdf, Png, Query, Svg
Instance Attribute Summary collapse
-
#compiled ⇒ Object
Returns the value of attribute compiled.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
- .from_s(main_source, **options) ⇒ Object
- .from_zip(zip_file_path, main_file = "main.typ", **options) ⇒ Object
Instance Method Summary collapse
- #compile(format, **options) ⇒ Object
-
#initialize(*options) ⇒ Base
constructor
A new instance of Base.
- #pretty(pretty = true) ⇒ Object
- #query(selector, field: nil, one: false, format: "json") ⇒ Object
- #typst_args(opts) ⇒ Object
- #typst_options ⇒ Object
- #typst_pdf_args ⇒ Object
- #typst_png_args ⇒ Object
- #typst_pretty_args ⇒ Object
- #ugly(pretty = false) ⇒ Object
- #with_dependencies(dependencies) ⇒ Object
- #with_font_paths(font_paths) ⇒ Object
- #with_fonts(fonts) ⇒ Object
- #with_inputs(inputs) ⇒ Object
- #with_root(root) ⇒ Object
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 43 |
# File 'lib/base.rb', line 6 def initialize(*) if .size.zero? raise "No options given" elsif .first.is_a?(String) file, = ||= {} [:file] = file elsif .first.is_a?(Hash) = .first end if .has_key?(:file) raise "Can't find file" unless File.exist?([:file]) elsif .has_key?(:body) raise "Empty body" if [:body].to_s.empty? elsif .has_key?(:zip) raise "Can't find zip" unless File.exist?([:zip]) else raise "No input given" end root = Pathname.new([:root] || "."). raise "Invalid path for root" unless root.exist? [:root] = root.to_s font_paths = ([:font_paths] || []).collect{ |fp| Pathname.new(fp). } [:font_paths] = font_paths.collect(&:to_s) [:dependencies] ||= {} [:fonts] ||= {} [:sys_inputs] ||= {} [:ignore_system_fonts] ||= false [:ignore_embedded_fonts] ||= false [:pretty] ||= false [:render_bleed] ||= false self. = end |
Instance Attribute Details
#compiled ⇒ Object
Returns the value of attribute compiled.
4 5 6 |
# File 'lib/base.rb', line 4 def compiled @compiled end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/base.rb', line 3 def @options end |
Class Method Details
.from_s(main_source, **options) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/base.rb', line 68 def self.from_s(main_source, **) Typst::build_world_from_s(main_source, **) do |opts| = .merge(opts) if [:format] Typst::formats[[:format]].new(**) else new(**) end end end |
.from_zip(zip_file_path, main_file = "main.typ", **options) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/base.rb', line 79 def self.from_zip(zip_file_path, main_file = "main.typ", **) Typst::build_world_from_zip(zip_file_path, main_file, **) do |opts| = .merge(opts) if [:format] Typst::formats[[:format]].new(**) else new(**) end end end |
Instance Method Details
#compile(format, **options) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/base.rb', line 125 def compile(format, **) raise "Invalid format" if Typst::formats[format].nil? = self..merge() if .has_key?(:file) Typst::formats[format].new(**).compiled elsif .has_key?(:body) Typst::build_world_from_s(self.[:body], **) do |opts| Typst::formats[format].new(**.merge(opts)).compiled end elsif .has_key?(:zip) main_file = [:main_file] Typst::build_world_from_zip([:zip], main_file, **) do |opts| Typst::formats[format].new(**.merge(opts)).compiled end else raise "No input given" end end |
#pretty(pretty = true) ⇒ Object
115 116 117 118 |
# File 'lib/base.rb', line 115 def pretty(pretty = true) self.[:pretty] = pretty self end |
#query(selector, field: nil, one: false, format: "json") ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/base.rb', line 146 def query(selector, field: nil, one: false, format: "json") = { field: field, one: one, format: format } if self..has_key?(:file) Typst::Query.new(selector, self.[:file], **.merge(self..slice(:root, :font_paths, :ignore_system_fonts, :ignore_embedded_fonts, :sys_inputs))) elsif self..has_key?(:body) Typst::build_world_from_s(self.[:body], **self.) do |opts| Typst::Query.new(selector, opts[:file], **.merge(opts.slice(:root, :font_paths, :ignore_system_fonts, :ignore_embedded_fonts, :sys_inputs))) end elsif self..has_key?(:zip) Typst::build_world_from_zip(self.[:zip], **self.) do |opts| Typst::Query.new(selector, opts[:file], **.merge(opts.slice(:root, :font_paths, :ignore_system_fonts, :ignore_embedded_fonts, :sys_inputs))) end else raise "No input given" end end |
#typst_args(opts) ⇒ Object
49 50 51 |
# File 'lib/base.rb', line 49 def typst_args(opts) .values_at(*opts).append([:sys_inputs].map{ |k,v| [k.to_s,v.to_s] }.to_h) end |
#typst_options ⇒ Object
45 46 47 |
# File 'lib/base.rb', line 45 def [:file, :root, :font_paths, :ignore_system_fonts, :ignore_embedded_fonts, :render_bleed] end |
#typst_pdf_args ⇒ Object
57 58 59 60 61 62 |
# File 'lib/base.rb', line 57 def typst_pdf_args [:pdf_standards] ||= [] opts = - [:render_bleed] + [:pretty] args = typst_args(opts) [*args, [:pdf_standards]] end |
#typst_png_args ⇒ Object
64 65 66 |
# File 'lib/base.rb', line 64 def typst_png_args [*typst_args(), [:ppi]] end |
#typst_pretty_args ⇒ Object
53 54 55 |
# File 'lib/base.rb', line 53 def typst_pretty_args typst_args(.append(:pretty)) end |
#ugly(pretty = false) ⇒ Object
120 121 122 123 |
# File 'lib/base.rb', line 120 def ugly(pretty = false) self.pretty(pretty) self end |
#with_dependencies(dependencies) ⇒ Object
90 91 92 93 |
# File 'lib/base.rb', line 90 def with_dependencies(dependencies) self.[:dependencies] = self.[:dependencies].merge(dependencies) self end |
#with_font_paths(font_paths) ⇒ Object
105 106 107 108 |
# File 'lib/base.rb', line 105 def with_font_paths(font_paths) self.[:font_paths] = self.[:font_paths] + font_paths self end |
#with_fonts(fonts) ⇒ Object
95 96 97 98 |
# File 'lib/base.rb', line 95 def with_fonts(fonts) self.[:fonts] = self.[:fonts].merge(fonts) self end |
#with_inputs(inputs) ⇒ Object
100 101 102 103 |
# File 'lib/base.rb', line 100 def with_inputs(inputs) self.[:sys_inputs] = self.[:sys_inputs].merge(inputs) self end |