Class: Typst::Base
- Inherits:
-
Object
- Object
- Typst::Base
- Defined in:
- lib/typst.rb
Instance Attribute Summary collapse
-
#font_paths ⇒ Object
Returns the value of attribute font_paths.
-
#input ⇒ Object
Returns the value of attribute input.
-
#root ⇒ Object
Returns the value of attribute root.
-
#sys_inputs ⇒ Object
Returns the value of attribute sys_inputs.
Class Method Summary collapse
- .from_s(main_source, dependencies: {}, fonts: {}, sys_inputs: {}) ⇒ Object
- .from_zip(zip_file_path, main_file = nil, sys_inputs: {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(input, root: ".", font_paths: [], sys_inputs: {}) ⇒ Base
constructor
A new instance of Base.
- #write(output) ⇒ Object
Constructor Details
#initialize(input, root: ".", font_paths: [], sys_inputs: {}) ⇒ Base
Returns a new instance of Base.
14 15 16 17 18 19 |
# File 'lib/typst.rb', line 14 def initialize(input, root: ".", font_paths: [], sys_inputs: {}) self.input = input self.root = Pathname.new(root)..to_s self.font_paths = font_paths.collect{ |fp| Pathname.new(fp)..to_s } self.sys_inputs = sys_inputs end |
Instance Attribute Details
#font_paths ⇒ Object
Returns the value of attribute font_paths.
11 12 13 |
# File 'lib/typst.rb', line 11 def font_paths @font_paths end |
#input ⇒ Object
Returns the value of attribute input.
9 10 11 |
# File 'lib/typst.rb', line 9 def input @input end |
#root ⇒ Object
Returns the value of attribute root.
10 11 12 |
# File 'lib/typst.rb', line 10 def root @root end |
#sys_inputs ⇒ Object
Returns the value of attribute sys_inputs.
12 13 14 |
# File 'lib/typst.rb', line 12 def sys_inputs @sys_inputs end |
Class Method Details
.from_s(main_source, dependencies: {}, fonts: {}, sys_inputs: {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/typst.rb', line 25 def self.from_s(main_source, dependencies: {}, fonts: {}, sys_inputs: {}) dependencies = {} if dependencies.nil? fonts = {} if fonts.nil? 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 new(tmp_main_file, root: tmp_dir, font_paths: [relative_font_path], sys_inputs: sys_inputs) end end |
.from_zip(zip_file_path, main_file = nil, sys_inputs: {}) ⇒ Object
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/typst.rb', line 48 def self.from_zip(zip_file_path, main_file = nil, sys_inputs: {}) dependencies = {} 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| 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| fonts[Pathname.new(font_name).basename.to_s] = zipfile.file.read(font_name) end from_s(main_source, dependencies: dependencies, fonts: fonts, sys_inputs: sys_inputs) end end |
Instance Method Details
#write(output) ⇒ Object
21 22 23 |
# File 'lib/typst.rb', line 21 def write(output) File.open(output, "wb"){ |f| f.write(document) } end |