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.
- #query(selector, field: nil, one: false, format: "json") ⇒ Object
- #typst_args ⇒ Object
- #typst_pdf_args ⇒ Object
- #typst_png_args ⇒ 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 |
# 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] ||= {} [:resource_path] ||= File.dirname(__FILE__) [:ignore_system_fonts] ||= 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
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/base.rb', line 56 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
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", **) 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
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, **) 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 |
#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") = { field: field, one: one, format: format } if self..has_key?(:file) Typst::Query.new(selector, self.[:file], **.merge(self..slice(:root, :font_paths, :resource_path, :ignore_system_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, :resource_path, :ignore_system_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, :resource_path, :ignore_system_fonts, :sys_inputs))) end else raise "No input given" end end |
#typst_args ⇒ Object
43 44 45 |
# File 'lib/base.rb', line 43 def typst_args [[:file], [:root], [:font_paths], [:resource_path], [:ignore_system_fonts], [:sys_inputs].map{ |k,v| [k.to_s,v.to_s] }.to_h] end |
#typst_pdf_args ⇒ Object
47 48 49 50 |
# File 'lib/base.rb', line 47 def typst_pdf_args [:pdf_standards] ||= [] [*typst_args, [:pdf_standards]] end |
#typst_png_args ⇒ Object
52 53 54 |
# File 'lib/base.rb', line 52 def typst_png_args [*typst_args, [:ppi]] end |
#with_dependencies(dependencies) ⇒ Object
78 79 80 81 |
# File 'lib/base.rb', line 78 def with_dependencies(dependencies) self.[:dependencies] = self.[: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.[:font_paths] = self.[: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.[:fonts] = self.[:fonts].merge(fonts) self end |
#with_inputs(inputs) ⇒ Object
88 89 90 91 |
# File 'lib/base.rb', line 88 def with_inputs(inputs) self.[:sys_inputs] = self.[:sys_inputs].merge(inputs) self end |