typst 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -1
  3. data/README.typ +8 -1
  4. data/Rakefile +7 -0
  5. data/lib/typst.rb +58 -31
  6. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85a0ad3847a1a278f6f47ab94fc447860fb583736774ab4c7c81e5fc871d8d7f
4
- data.tar.gz: e9a4f127a9f9b2780a2b58efc413f3cbef4f62b0fbae1d2649e670ef552a87da
3
+ metadata.gz: f77a7d7e2a7130f282f136e290f924e4bc85915a787731d2a43c5907ad28c9f5
4
+ data.tar.gz: 1225f0ba1f3acfae310faccff69a4a623272bc3ec4af817cc2a4f65ac987167c
5
5
  SHA512:
6
- metadata.gz: b75ae52ebb916a9a9fd34b7a77816ddf86f38aaeb3878a1135e135bbb468f712751b2bcd288dc947d884b8cdca64a3fd267b38f405dc8f12976dda9d8e66d059
7
- data.tar.gz: bccf2c3206193e12ae7ec4805b61c1d953abda079866eca4063d221973d6491cee3d8c959fb66cfc3b4b5994cd915f85d19e748617f998c4c816ddd67dac3205
6
+ metadata.gz: e0797e3299103e8e8623a505ba191661c7cdd1738377218c2e3d0553d20d75000424acad0d617524e0c87d42453dacdbedfbf17a17320165de23c17fb2e2f553
7
+ data.tar.gz: 8cc01bc1a9198949208be7a31dc212a455a733a9a5b653a294799b25a2992ff9535515675da45f8322f5e5ec9b5d61c55d264cc9d2b84216957b67976b879d7b
data/README.md CHANGED
@@ -36,7 +36,7 @@ pages = Typst::Svg.new("readme.typ").pages
36
36
  Typst::Html.new("readme.typ", "README").write("readme.html")
37
37
 
38
38
  # Or return HTML content
39
- markup = Typst::Html.new("readme.typ", "README").markup
39
+ markup = Typst::Html.new("readme.typ", title: "README").markup
40
40
  # => "\n<!DOCTYPE html>\n<html>\n<head>\n<title>README</title>\n</head>\n<bo..."
41
41
 
42
42
  # Compile from a string to PDF
@@ -70,6 +70,13 @@ icon = File.read("icon.svg")
70
70
  font_bytes = File.read("Example.ttf")
71
71
 
72
72
  t = Typst::Pdf.from_s(main, dependencies: { "template.typ" => template, "icon.svg" => icon }, fonts: { "Example.ttf" => font_bytes })
73
+
74
+ # From a zip file that includes a main.typ
75
+ # zip file include flat dependencies included and a fonts directory
76
+ Typst::Pdf::from_zip("working_directory.zip")
77
+
78
+ # From a zip with a named main typst file
79
+ Typst::Pdf::from_zip("working_directory.zip", "hello.typ")
73
80
  ```
74
81
 
75
82
  ## Contributors & Acknowledgements
data/README.typ CHANGED
@@ -39,7 +39,7 @@ pages = Typst::Svg.new("readme.typ").pages
39
39
  Typst::Html.new("readme.typ", "README").write("readme.html")
40
40
 
41
41
  # Or return HTML content
42
- markup = Typst::Html.new("readme.typ", "README").markup
42
+ markup = Typst::Html.new("readme.typ", title: "README").markup
43
43
  # => "\n<!DOCTYPE html>\n<html>\n<head>\n<title>README</title>\n</head>\n<bo..."
44
44
 
45
45
  # Compile from a string to PDF
@@ -73,6 +73,13 @@ icon = File.read("icon.svg")
73
73
  font_bytes = File.read("Example.ttf")
74
74
 
75
75
  t = Typst::Pdf.from_s(main, dependencies: { "template.typ" => template, "icon.svg" => icon }, fonts: { "Example.ttf" => font_bytes })
76
+
77
+ # From a zip file that includes a main.typ
78
+ # zip file include flat dependencies included and a fonts directory
79
+ Typst::Pdf::from_zip("working_directory.zip")
80
+
81
+ # From a zip with a named main typst file
82
+ Typst::Pdf::from_zip("working_directory.zip", "hello.typ")
76
83
  ```
77
84
 
78
85
  == Contributors & Acknowledgements
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rake/extensiontask"
3
+ require "rake/testtask"
3
4
  require "rubygems/package_task"
4
5
  require "bundler"
5
6
 
@@ -22,3 +23,9 @@ Rake::ExtensionTask.new("typst", spec) do |ext|
22
23
  ext.cross_compile = true
23
24
  ext.cross_platform = CROSS_PLATFORMS
24
25
  end
26
+
27
+ Rake::TestTask.new do |t|
28
+ t.libs << "test"
29
+ t.test_files = FileList['test/*_test.rb']
30
+ t.verbose = true
31
+ end
data/lib/typst.rb CHANGED
@@ -2,10 +2,27 @@ require_relative "typst/typst"
2
2
  require "cgi"
3
3
  require "pathname"
4
4
  require "tmpdir"
5
+ require "zip/filesystem"
5
6
 
6
7
  module Typst
7
8
  class Base
9
+ attr_accessor :input
10
+ attr_accessor :root
11
+ attr_accessor :font_paths
12
+
13
+ def initialize(input, root: ".", font_paths: [])
14
+ self.input = input
15
+ self.root = Pathname.new(root).expand_path.to_s
16
+ self.font_paths = font_paths.collect{ |fp| Pathname.new(fp).expand_path.to_s }
17
+ end
18
+
19
+ def write(output)
20
+ File.open(output, "w"){ |f| f.write(document) }
21
+ end
22
+
8
23
  def self.from_s(main_source, dependencies: {}, fonts: {})
24
+ dependencies = {} if dependencies.nil?
25
+ fonts = {} if fonts.nil?
9
26
  Dir.mktmpdir do |tmp_dir|
10
27
  tmp_main_file = Pathname.new(tmp_dir).join("main.typ")
11
28
  File.write(tmp_main_file, main_source)
@@ -16,7 +33,6 @@ module Typst
16
33
  end
17
34
 
18
35
  relative_font_path = Pathname.new(tmp_dir).join("fonts")
19
- puts fonts
20
36
  fonts.each do |font_name, font_bytes|
21
37
  Pathname.new(relative_font_path).mkpath
22
38
  tmp_font_file = relative_font_path.join(font_name)
@@ -26,43 +42,56 @@ module Typst
26
42
  new(tmp_main_file, root: tmp_dir, font_paths: [relative_font_path])
27
43
  end
28
44
  end
45
+
46
+ def self.from_zip(zip_file_path, main_file = nil)
47
+ dependencies = {}
48
+ fonts = {}
49
+
50
+ Zip::File.open(zip_file_path) do |zipfile|
51
+ file_names = zipfile.dir.glob("*").collect{ |f| f.name }
52
+ case
53
+ when file_names.include?(main_file) then tmp_main_file = main_file
54
+ when file_names.include?("main.typ") then tmp_main_file = "main.typ"
55
+ when file_names.size == 1 then tmp_main_file = file_names.first
56
+ else raise "no main file found"
57
+ end
58
+ main_source = zipfile.file.read(tmp_main_file)
59
+ file_names.delete(tmp_main_file)
60
+ file_names.delete("fonts/")
61
+
62
+ file_names.each do |dep_name|
63
+ dependencies[dep_name] = zipfile.file.read(dep_name)
64
+ end
65
+
66
+ font_file_names = zipfile.dir.glob("fonts/*").collect{ |f| f.name }
67
+ font_file_names.each do |font_name|
68
+ fonts[Pathname.new(font_name).basename.to_s] = zipfile.file.read(font_name)
69
+ end
70
+
71
+ from_s(main_source, dependencies: dependencies, fonts: fonts)
72
+ end
73
+ end
29
74
  end
30
75
 
31
76
  class Pdf < Base
32
- attr_accessor :input
33
- attr_accessor :root
34
- attr_accessor :font_paths
35
77
  attr_accessor :bytes
36
78
 
37
- def initialize(input, root: ".", font_paths: ["fonts"])
38
- self.input = input
39
- self.root = root
40
- self.font_paths = font_paths
41
-
42
- @bytes = Typst::_to_pdf(input, root, font_paths, File.dirname(__FILE__))
79
+ def initialize(input, root: ".", font_paths: [])
80
+ super(input, root: root, font_paths: font_paths)
81
+ @bytes = Typst::_to_pdf(self.input, self.root, self.font_paths, File.dirname(__FILE__))
43
82
  end
44
83
 
45
84
  def document
46
85
  bytes.pack("C*").to_s
47
86
  end
48
-
49
- def write(output)
50
- File.open(output, "w"){ |f| f.write(document) }
51
- end
52
87
  end
53
88
 
54
89
  class Svg < Base
55
- attr_accessor :input
56
- attr_accessor :root
57
- attr_accessor :font_paths
58
90
  attr_accessor :pages
59
91
 
60
- def initialize(input, root: ".", font_paths: ["fonts"])
61
- self.input = input
62
- self.root = root
63
- self.font_paths = font_paths
64
-
65
- @pages = Typst::_to_svg(input, root, font_paths, File.dirname(__FILE__))
92
+ def initialize(input, root: ".", font_paths: [])
93
+ super(input, root: root, font_paths: font_paths)
94
+ @pages = Typst::_to_svg(self.input, self.root, self.font_paths, File.dirname(__FILE__))
66
95
  end
67
96
 
68
97
  def write(output)
@@ -88,12 +117,13 @@ module Typst
88
117
  attr_accessor :svg
89
118
  attr_accessor :html
90
119
 
91
- def initialize(input, title = nil, root: ".", font_paths: ["fonts"])
120
+ def initialize(input, title: nil, root: ".", font_paths: [])
121
+ super(input, root: root, font_paths: font_paths)
92
122
  title = title || File.basename(input, File.extname(input))
93
- @title = CGI::escapeHTML(title)
94
- @svg = Svg.new(input, root: root, font_paths: font_paths)
123
+ self.title = CGI::escapeHTML(title)
124
+ self.svg = Svg.new(self.input, root: self.root, font_paths: self.font_paths)
95
125
  end
96
-
126
+
97
127
  def markup
98
128
  %{
99
129
  <!DOCTYPE html>
@@ -107,9 +137,6 @@ module Typst
107
137
  </html>
108
138
  }
109
139
  end
110
-
111
- def write(output)
112
- File.open(output, "w"){ |f| f.write(markup) }
113
- end
140
+ alias_method :document, :markup
114
141
  end
115
142
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typst
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Flinn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-27 00:00:00.000000000 Z
11
+ date: 2023-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rb_sys
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  - !ruby/object:Gem::Version
80
80
  version: '0'
81
81
  requirements: []
82
- rubygems_version: 3.2.3
82
+ rubygems_version: 3.4.22
83
83
  signing_key:
84
84
  specification_version: 4
85
85
  summary: Ruby binding to typst, a new markup-based typesetting system that is powerful