typst-rails 0.1.0 → 0.2.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +191 -1
- data/README.md +347 -33
- data/lib/typst-rails.rb +7 -0
- data/lib/typst_rails/backends/base.rb +24 -1
- data/lib/typst_rails/backends/cli.rb +47 -12
- data/lib/typst_rails/backends/gem.rb +19 -2
- data/lib/typst_rails/data_serializer.rb +294 -0
- data/lib/typst_rails/diagnostics.rb +284 -0
- data/lib/typst_rails/erb_source.rb +36 -0
- data/lib/typst_rails/errors.rb +139 -0
- data/lib/typst_rails/framework_detection.rb +13 -4
- data/lib/typst_rails/handler.rb +51 -9
- data/lib/typst_rails/helpers.rb +26 -112
- data/lib/typst_rails/markdown_converter.rb +248 -0
- data/lib/typst_rails/rage_integration.rb +55 -8
- data/lib/typst_rails/rails_rendering.rb +58 -0
- data/lib/typst_rails/railtie.rb +7 -0
- data/lib/typst_rails/renderer.rb +223 -107
- data/lib/typst_rails/sinatra_integration.rb +11 -12
- data/lib/typst_rails/template_rendering.rb +252 -0
- data/lib/typst_rails/version.rb +1 -1
- data/lib/typst_rails/view_helpers.rb +28 -0
- data/lib/typst_rails.rb +140 -5
- metadata +12 -3
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pathname"
|
|
4
|
+
require "stringio"
|
|
5
|
+
require "typst_rails/erb_source"
|
|
6
|
+
require "typst_rails/framework_detection"
|
|
7
|
+
require "typst_rails/helpers"
|
|
8
|
+
require "typst_rails/rails_rendering"
|
|
9
|
+
require "typst_rails/renderer"
|
|
10
|
+
|
|
11
|
+
module TypstRails
|
|
12
|
+
# Renders a `.typ` template file to PDF outside a request: in a background
|
|
13
|
+
# job, a mailer, a rake task, or a script.
|
|
14
|
+
#
|
|
15
|
+
# The template runs through ERB first, with +locals+ as local variables,
|
|
16
|
+
# +assigns+ as instance variables, and the {Helpers} methods such as
|
|
17
|
+
# `escape_typst`. Both hashes are also written to typst_data.json, as in
|
|
18
|
+
# the Rails, Sinatra, and Rage integrations.
|
|
19
|
+
#
|
|
20
|
+
# Relative names resolve against +root+, which defaults to `app/views`
|
|
21
|
+
# under Rails.root in a Rails app and to the current directory elsewhere.
|
|
22
|
+
# The `.typ` extension is optional, and `name.pdf.typ` is tried after
|
|
23
|
+
# `name.typ`. A relative name that resolves outside the root, such as
|
|
24
|
+
# `"../secrets"`, raises ArgumentError. Absolute paths are used as given.
|
|
25
|
+
#
|
|
26
|
+
# Rails view helpers, such as route helpers and `render` for partials, are
|
|
27
|
+
# not available. Use {TypstRails.render_with_rails} for templates that
|
|
28
|
+
# need them.
|
|
29
|
+
#
|
|
30
|
+
# @param template [String, Pathname] template name or path, such as
|
|
31
|
+
# `"reports/monthly"`
|
|
32
|
+
# @param locals [Hash] ERB local variables and typst_data.json entries
|
|
33
|
+
# @param assigns [Hash] ERB instance variables and typst_data.json
|
|
34
|
+
# entries; locals win on a name clash
|
|
35
|
+
# @param root [String, Pathname, nil] directory relative names resolve against
|
|
36
|
+
# @return [String] binary PDF data
|
|
37
|
+
# @raise [ArgumentError] if the name is blank or resolves outside the root
|
|
38
|
+
# @raise [TypstRails::Error] if the template is missing or compilation fails
|
|
39
|
+
#
|
|
40
|
+
# @example In a background job
|
|
41
|
+
# pdf = TypstRails.render("reports/monthly", locals: { month: "October" })
|
|
42
|
+
# File.binwrite("monthly.pdf", pdf)
|
|
43
|
+
#
|
|
44
|
+
# @example In a mailer
|
|
45
|
+
# attachments["invoice.pdf"] = TypstRails.render("invoices/show", assigns: { invoice: invoice })
|
|
46
|
+
def self.render(template, locals: {}, assigns: {}, root: nil)
|
|
47
|
+
TemplateRendering.render(template, locals: locals, assigns: assigns, root: root)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Renders a `.typ` view through a Rails controller and returns the PDF.
|
|
51
|
+
#
|
|
52
|
+
# Unlike {TypstRails.render}, the template goes through ActionView, the
|
|
53
|
+
# same as a `render template:` in a controller action. Rails resolves the
|
|
54
|
+
# name with its own view lookup, and app helpers, route helpers, and
|
|
55
|
+
# partials work. This method calls
|
|
56
|
+
# `controller.render(template:, formats: [:pdf], locals:, assigns:)`.
|
|
57
|
+
#
|
|
58
|
+
# The default controller is ApplicationController when it is defined and
|
|
59
|
+
# inherits from ActionController::Base, otherwise ActionController::Base.
|
|
60
|
+
#
|
|
61
|
+
# Route helpers that build full URLs need a host, from
|
|
62
|
+
# `config.action_controller.default_url_options` or the controller's
|
|
63
|
+
# renderer defaults, because there is no request to take one from.
|
|
64
|
+
#
|
|
65
|
+
# @param template [String, Symbol, Pathname] Rails template name, such as
|
|
66
|
+
# `"reports/monthly"`
|
|
67
|
+
# @param locals [Hash] view locals
|
|
68
|
+
# @param assigns [Hash] view instance variables
|
|
69
|
+
# @param controller [Class, nil] controller class to render with, such as
|
|
70
|
+
# `AdminController`
|
|
71
|
+
# @return [String] binary PDF data
|
|
72
|
+
# @raise [TypstRails::Error] if ActionController is not loaded
|
|
73
|
+
# @raise [ActionView::MissingTemplate] if Rails cannot find the template
|
|
74
|
+
#
|
|
75
|
+
# @example In a job
|
|
76
|
+
# pdf = TypstRails.render_with_rails("invoices/show", assigns: { invoice: invoice })
|
|
77
|
+
def self.render_with_rails(template, locals: {}, assigns: {}, controller: nil)
|
|
78
|
+
RailsRendering.render(template, locals: locals, assigns: assigns, controller: controller)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Renders a template and attaches the PDF to +attachable+ by calling
|
|
82
|
+
# `attachable.attach(io:, filename:, content_type:)`.
|
|
83
|
+
#
|
|
84
|
+
# Any object with that method works. An ActiveStorage attachment, such as
|
|
85
|
+
# `report.file` for `has_one_attached :file`, is the usual one.
|
|
86
|
+
#
|
|
87
|
+
# The template renders with {TypstRails.render}, or with
|
|
88
|
+
# {TypstRails.render_with_rails} when +via+ is `:rails`. Other options go
|
|
89
|
+
# to whichever one renders.
|
|
90
|
+
#
|
|
91
|
+
# @param attachable [#attach] receives the PDF
|
|
92
|
+
# @param template [String, Pathname] template name or path
|
|
93
|
+
# @param filename [String] filename to attach the PDF under
|
|
94
|
+
# @param locals [Hash] template locals
|
|
95
|
+
# @param content_type [String] content type to attach the PDF with
|
|
96
|
+
# @param via [Symbol] `:typst_rails` (the default) or `:rails`
|
|
97
|
+
# @param options [Hash] other options for the render method
|
|
98
|
+
# (`assigns:` and `root:`, or `assigns:` and `controller:`)
|
|
99
|
+
# @return [Object] whatever +attachable.attach+ returns
|
|
100
|
+
# @raise [ArgumentError] if +attachable+ does not respond to `attach`, or
|
|
101
|
+
# +via+ is unknown
|
|
102
|
+
# @raise [TypstRails::Error] if rendering fails
|
|
103
|
+
#
|
|
104
|
+
# @example
|
|
105
|
+
# TypstRails.render_and_attach(report.file, "reports/monthly",
|
|
106
|
+
# filename: "monthly.pdf", locals: { report: report })
|
|
107
|
+
#
|
|
108
|
+
# @example Through ActionView
|
|
109
|
+
# TypstRails.render_and_attach(report.file, "reports/monthly",
|
|
110
|
+
# filename: "monthly.pdf", via: :rails)
|
|
111
|
+
# rubocop:disable Metrics/ParameterLists -- render options plus the attach options
|
|
112
|
+
def self.render_and_attach(attachable, template, filename:, locals: {}, content_type: "application/pdf",
|
|
113
|
+
via: :typst_rails, **options)
|
|
114
|
+
raise ArgumentError, "#{attachable.class} does not respond to attach" unless attachable.respond_to?(:attach)
|
|
115
|
+
|
|
116
|
+
pdf = case via
|
|
117
|
+
when :typst_rails then render(template, locals: locals, **options)
|
|
118
|
+
when :rails then render_with_rails(template, locals: locals, **options)
|
|
119
|
+
else raise ArgumentError, "via must be :typst_rails or :rails, got #{via.inspect}"
|
|
120
|
+
end
|
|
121
|
+
attachable.attach(io: StringIO.new(pdf), filename: filename, content_type: content_type)
|
|
122
|
+
end
|
|
123
|
+
# rubocop:enable Metrics/ParameterLists
|
|
124
|
+
|
|
125
|
+
# Renders `.typ` template files to PDF without a controller or request:
|
|
126
|
+
# from background jobs, mailers, rake tasks, and scripts. Backs
|
|
127
|
+
# {TypstRails.render} and {TypstRails.render_and_attach}, and the Sinatra
|
|
128
|
+
# and Rage helpers.
|
|
129
|
+
#
|
|
130
|
+
# A template file runs through ERB first, then compiles with Typst. Locals
|
|
131
|
+
# are ERB local variables and assigns are instance variables; both are also
|
|
132
|
+
# written to typst_data.json for templates that read it.
|
|
133
|
+
#
|
|
134
|
+
# @api private
|
|
135
|
+
module TemplateRendering
|
|
136
|
+
# File extension of Typst templates.
|
|
137
|
+
EXTENSION = ".typ"
|
|
138
|
+
|
|
139
|
+
# Mirrors the `assigns` reader Renderer expects from a view context, so
|
|
140
|
+
# data gets the same JSON conversion (models, dates) as in Rails.
|
|
141
|
+
ViewContext = Struct.new(:assigns)
|
|
142
|
+
|
|
143
|
+
# The `self` of a template rendered outside a request: it has the
|
|
144
|
+
# {Helpers} methods and whatever instance variables the assigns set.
|
|
145
|
+
class Scope
|
|
146
|
+
include Helpers
|
|
147
|
+
|
|
148
|
+
# @param assigns [Hash] instance variables to set, keyed by name
|
|
149
|
+
# without the leading `@`
|
|
150
|
+
def initialize(assigns = {})
|
|
151
|
+
assigns.each do |name, value|
|
|
152
|
+
instance_variable_set(:"@#{name}", value) if ErbSource::LOCAL_NAME.match?(name.to_s)
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
module_function
|
|
158
|
+
|
|
159
|
+
# Resolves a template name, runs it through ERB, and compiles it.
|
|
160
|
+
#
|
|
161
|
+
# @param template [String, Pathname] template path; see {.resolve}
|
|
162
|
+
# @param locals [Hash] ERB local variables, also written to typst_data.json
|
|
163
|
+
# @param assigns [Hash] ERB instance variables, also written to
|
|
164
|
+
# typst_data.json; locals win on a name clash
|
|
165
|
+
# @param root [String, Pathname, nil] template root; see {.default_root}
|
|
166
|
+
# @return [String] binary PDF data
|
|
167
|
+
# @raise [ArgumentError] if the template resolves outside the root
|
|
168
|
+
# @raise [TypstRails::Error] if the template is missing or compilation fails
|
|
169
|
+
def render(template, locals: {}, assigns: {}, root: nil)
|
|
170
|
+
raise ArgumentError, "locals must be a Hash" unless locals.is_a?(Hash)
|
|
171
|
+
raise ArgumentError, "assigns must be a Hash" unless assigns.is_a?(Hash)
|
|
172
|
+
|
|
173
|
+
path = resolve(template, root || default_root)
|
|
174
|
+
render_file(path, Scope.new(assigns), locals, assigns)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# Renders a template file in a given ERB scope. Shared by the Sinatra and
|
|
178
|
+
# Rage helpers, which pass their route or controller as the scope.
|
|
179
|
+
#
|
|
180
|
+
# @param path [String] path to the template file
|
|
181
|
+
# @param scope [Object] `self` for ERB
|
|
182
|
+
# @param locals [Hash] ERB local variables, also written to typst_data.json
|
|
183
|
+
# @param assigns [Hash] extra data for typst_data.json; locals win on a
|
|
184
|
+
# name clash
|
|
185
|
+
# @return [String] binary PDF data
|
|
186
|
+
# @raise [TypstRails::Error] if compilation fails
|
|
187
|
+
def render_file(path, scope, locals = {}, assigns = {})
|
|
188
|
+
source = ErbSource.render(File.read(path), scope, locals)
|
|
189
|
+
Renderer.new(source, template: path.to_s).render(ViewContext.new(assigns), locals)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# Turns a template name into the path of an existing file.
|
|
193
|
+
#
|
|
194
|
+
# Relative names resolve against +root+ and must stay inside it.
|
|
195
|
+
# Absolute paths are used as given. A name without the `.typ` extension
|
|
196
|
+
# matches `name.typ`, or failing that the Rails-style `name.pdf.typ`.
|
|
197
|
+
#
|
|
198
|
+
# @param template [String, Pathname] template name or path
|
|
199
|
+
# @param root [String, Pathname] directory relative names resolve against
|
|
200
|
+
# @return [String] absolute path of the template file
|
|
201
|
+
# @raise [ArgumentError] if the template is blank or resolves outside +root+
|
|
202
|
+
# @raise [TypstRails::Error] if no matching file exists
|
|
203
|
+
#
|
|
204
|
+
# @example
|
|
205
|
+
# TemplateRendering.resolve("reports/monthly", "app/views")
|
|
206
|
+
# #=> "/app/app/views/reports/monthly.typ"
|
|
207
|
+
def resolve(template, root)
|
|
208
|
+
name = template.to_s
|
|
209
|
+
raise ArgumentError, "template cannot be empty" if name.strip.empty?
|
|
210
|
+
|
|
211
|
+
base = File.expand_path(name, root.to_s)
|
|
212
|
+
check_inside_root!(base, root) unless absolute?(name)
|
|
213
|
+
|
|
214
|
+
found = candidates(base).find { |candidate| File.file?(candidate) }
|
|
215
|
+
raise Error, "Typst template not found: #{name} (looked for #{candidates(base).join(", ")})" unless found
|
|
216
|
+
|
|
217
|
+
found
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# The root relative template names resolve against: `app/views` under
|
|
221
|
+
# Rails.root when Rails is loaded, otherwise the current directory.
|
|
222
|
+
#
|
|
223
|
+
# @return [String]
|
|
224
|
+
def default_root
|
|
225
|
+
if FrameworkDetection.rails? && ::Rails.respond_to?(:root) && ::Rails.root
|
|
226
|
+
File.join(::Rails.root.to_s, "app", "views")
|
|
227
|
+
else
|
|
228
|
+
Dir.pwd
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# @api private
|
|
233
|
+
def candidates(base)
|
|
234
|
+
return [base] if File.extname(base) == EXTENSION
|
|
235
|
+
|
|
236
|
+
["#{base}#{EXTENSION}", "#{base}.pdf#{EXTENSION}"]
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# @api private
|
|
240
|
+
def absolute?(name)
|
|
241
|
+
Pathname.new(name).absolute?
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# @api private
|
|
245
|
+
def check_inside_root!(path, root)
|
|
246
|
+
root_path = File.expand_path(root.to_s)
|
|
247
|
+
return if path.start_with?(File.join(root_path, ""))
|
|
248
|
+
|
|
249
|
+
raise ArgumentError, "Typst template #{path} is outside the template root #{root_path}"
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
end
|
data/lib/typst_rails/version.rb
CHANGED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "erb"
|
|
4
|
+
require "typst_rails/helpers"
|
|
5
|
+
|
|
6
|
+
module TypstRails
|
|
7
|
+
# {Helpers} as mixed into ActionView, so `.typ` templates can call
|
|
8
|
+
# `escape_typst`, `markdown_to_typst`, and the rest directly.
|
|
9
|
+
#
|
|
10
|
+
# The Railtie includes this module into ActionView::Base, which every view
|
|
11
|
+
# shares, HTML views included.
|
|
12
|
+
#
|
|
13
|
+
# @example In app/views/reports/monthly.typ
|
|
14
|
+
# = <%= escape_typst(@title) %>
|
|
15
|
+
module ViewHelpers
|
|
16
|
+
include Helpers
|
|
17
|
+
|
|
18
|
+
# ActionView::Base already includes ERB::Util#url_encode, which encodes a
|
|
19
|
+
# space as "%20"; {Helpers#url_encode} would encode it as "+". Keep the
|
|
20
|
+
# ActionView behavior so existing HTML views are unaffected.
|
|
21
|
+
#
|
|
22
|
+
# @param text [String, nil]
|
|
23
|
+
# @return [String]
|
|
24
|
+
def url_encode(text)
|
|
25
|
+
ERB::Util.url_encode(text)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/typst_rails.rb
CHANGED
|
@@ -4,6 +4,7 @@ require "typst_rails/version"
|
|
|
4
4
|
require "typst_rails/backends"
|
|
5
5
|
require "typst_rails/renderer"
|
|
6
6
|
require "typst_rails/framework_detection"
|
|
7
|
+
require "typst_rails/template_rendering"
|
|
7
8
|
|
|
8
9
|
# Conditionally load framework integrations
|
|
9
10
|
require "typst_rails/railtie" if TypstRails::FrameworkDetection.rails?
|
|
@@ -84,10 +85,36 @@ module TypstRails
|
|
|
84
85
|
# config.typst_executable_path = "/usr/local/bin/typst"
|
|
85
86
|
# config.validate! # Ensures configuration is valid
|
|
86
87
|
class Configuration
|
|
88
|
+
# PDF standards accepted by {#pdf_standard}, mapped to the identifiers the
|
|
89
|
+
# Typst compiler uses for them (as listed by `typst compile --help`).
|
|
90
|
+
PDF_STANDARDS = {
|
|
91
|
+
pdf_1_4: "1.4",
|
|
92
|
+
pdf_1_5: "1.5",
|
|
93
|
+
pdf_1_6: "1.6",
|
|
94
|
+
pdf_1_7: "1.7",
|
|
95
|
+
pdf_2_0: "2.0",
|
|
96
|
+
a_1a: "a-1a",
|
|
97
|
+
a_1b: "a-1b",
|
|
98
|
+
a_2a: "a-2a",
|
|
99
|
+
a_2b: "a-2b",
|
|
100
|
+
a_2u: "a-2u",
|
|
101
|
+
a_3a: "a-3a",
|
|
102
|
+
a_3b: "a-3b",
|
|
103
|
+
a_3u: "a-3u",
|
|
104
|
+
a_4: "a-4",
|
|
105
|
+
a_4e: "a-4e",
|
|
106
|
+
a_4f: "a-4f",
|
|
107
|
+
ua_1: "ua-1"
|
|
108
|
+
}.freeze
|
|
109
|
+
|
|
87
110
|
# @return [String] Path to the Typst executable (defaults to "typst" in PATH)
|
|
88
111
|
attr_accessor :typst_executable_path
|
|
89
112
|
|
|
90
|
-
# @return [String, nil]
|
|
113
|
+
# @return [String, Pathname, nil] Typst root directory. When set, templates
|
|
114
|
+
# compile inside it, so they can load files beneath it with root-relative
|
|
115
|
+
# paths such as `image("/images/logo.png")`. The directory must be
|
|
116
|
+
# writable: each render creates a short-lived working directory in it.
|
|
117
|
+
# When nil, each render uses a private directory under Dir.tmpdir.
|
|
91
118
|
attr_accessor :default_root_path
|
|
92
119
|
|
|
93
120
|
# @return [Symbol, TypstRails::Backends::Base] Which compilation backend to use.
|
|
@@ -97,12 +124,44 @@ module TypstRails
|
|
|
97
124
|
# via {TypstRails::Backends::Registry.register}.
|
|
98
125
|
attr_accessor :backend
|
|
99
126
|
|
|
127
|
+
# @return [Symbol, Array<Symbol>, nil] PDF standard(s) Typst should enforce
|
|
128
|
+
# conformance with, such as `:ua_1` for accessible PDF/UA-1 output or
|
|
129
|
+
# `[:a_2b, :ua_1]` for several at once. See {PDF_STANDARDS} for the
|
|
130
|
+
# accepted keys. When nil (the default), Typst writes a plain PDF.
|
|
131
|
+
# PDF/UA-1 requires the document to set a title with
|
|
132
|
+
# `#set document(title: ...)`; compilation fails without one.
|
|
133
|
+
attr_accessor :pdf_standard
|
|
134
|
+
|
|
135
|
+
# @return [Array<String, Pathname>] Extra directories Typst searches,
|
|
136
|
+
# recursively, for font files. Defaults to an empty list. Assigning a
|
|
137
|
+
# single path wraps it in an Array. Relative paths are expanded against
|
|
138
|
+
# the process working directory when a document is compiled.
|
|
139
|
+
attr_reader :font_paths
|
|
140
|
+
|
|
141
|
+
# @return [Boolean] Whether Typst should skip the fonts installed on the
|
|
142
|
+
# system and use only {#font_paths} plus the fonts embedded in Typst.
|
|
143
|
+
# Defaults to false.
|
|
144
|
+
attr_accessor :ignore_system_fonts
|
|
145
|
+
|
|
146
|
+
# @return [#call, nil] Called with `(warnings, template)` after a
|
|
147
|
+
# successful compile that produced warnings: an array of
|
|
148
|
+
# {TypstRails::Diagnostics::Diagnostic} and the template's display name
|
|
149
|
+
# (or nil). The default logs them through Rails.logger.warn when Rails
|
|
150
|
+
# is loaded, and Kernel#warn otherwise. Set to nil to ignore warnings.
|
|
151
|
+
# Only the `:cli` backend reports warnings from successful compiles;
|
|
152
|
+
# the `typst` gem does not expose them.
|
|
153
|
+
attr_accessor :on_warning
|
|
154
|
+
|
|
100
155
|
# Initializes a new Configuration with default values.
|
|
101
156
|
#
|
|
102
157
|
# Default values:
|
|
103
158
|
# - `typst_executable_path`: "typst" (assumes typst is in PATH)
|
|
104
|
-
# - `default_root_path`: nil (
|
|
159
|
+
# - `default_root_path`: nil (each render gets a private temporary root)
|
|
105
160
|
# - `backend`: `:auto` (prefer the `typst` gem, fall back to the CLI)
|
|
161
|
+
# - `pdf_standard`: nil (plain PDF, no conformance enforced)
|
|
162
|
+
# - `font_paths`: [] (no extra font directories)
|
|
163
|
+
# - `ignore_system_fonts`: false (system fonts are searched)
|
|
164
|
+
# - `on_warning`: logs warnings (see {TypstRails::Diagnostics.log_warnings})
|
|
106
165
|
#
|
|
107
166
|
# @return [Configuration] A new configuration instance
|
|
108
167
|
#
|
|
@@ -115,17 +174,64 @@ module TypstRails
|
|
|
115
174
|
@typst_executable_path = "typst"
|
|
116
175
|
@default_root_path = nil
|
|
117
176
|
@backend = :auto
|
|
177
|
+
@pdf_standard = nil
|
|
178
|
+
@font_paths = []
|
|
179
|
+
@ignore_system_fonts = false
|
|
180
|
+
@on_warning = Diagnostics.method(:log_warnings)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# Sets the font directories. A single String or Pathname is wrapped in an
|
|
184
|
+
# Array, and nil clears the list.
|
|
185
|
+
#
|
|
186
|
+
# @param paths [Array<String, Pathname>, String, Pathname, nil]
|
|
187
|
+
# @return [void]
|
|
188
|
+
#
|
|
189
|
+
# @example
|
|
190
|
+
# config.font_paths = Rails.root.join("app", "assets", "fonts")
|
|
191
|
+
# config.font_paths #=> [#<Pathname:/app/app/assets/fonts>]
|
|
192
|
+
def font_paths=(paths)
|
|
193
|
+
@font_paths = paths.is_a?(Array) ? paths : Array(paths)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Options that backends pass through to the Typst compiler.
|
|
197
|
+
#
|
|
198
|
+
# @return [Hash{Symbol => Object}] compiler options:
|
|
199
|
+
# - `:pdf_standards` [Array<String>] Typst PDF standard identifiers
|
|
200
|
+
# (e.g. `["ua-1"]`), empty when {#pdf_standard} is nil
|
|
201
|
+
# - `:font_paths` [Array<String>] font directories, expanded to absolute paths
|
|
202
|
+
# - `:ignore_system_fonts` [Boolean]
|
|
203
|
+
# @raise [ArgumentError] if {#pdf_standard} contains an unknown key
|
|
204
|
+
#
|
|
205
|
+
# @example
|
|
206
|
+
# config = TypstRails::Configuration.new
|
|
207
|
+
# config.pdf_standard = :ua_1
|
|
208
|
+
# config.font_paths = ["vendor/fonts"]
|
|
209
|
+
# config.compile_options
|
|
210
|
+
# #=> { pdf_standards: ["ua-1"], font_paths: ["/app/vendor/fonts"], ignore_system_fonts: false }
|
|
211
|
+
def compile_options
|
|
212
|
+
{
|
|
213
|
+
pdf_standards: pdf_standards,
|
|
214
|
+
font_paths: font_paths.map { |path| File.expand_path(path) },
|
|
215
|
+
ignore_system_fonts: ignore_system_fonts
|
|
216
|
+
}
|
|
118
217
|
end
|
|
119
218
|
|
|
120
219
|
# Validates the configuration settings.
|
|
121
220
|
#
|
|
122
221
|
# Ensures that:
|
|
123
222
|
# - typst_executable_path is a non-empty string
|
|
124
|
-
# - default_root_path (if set) is a
|
|
223
|
+
# - default_root_path (if set) is a String or Pathname
|
|
224
|
+
# - pdf_standard (if set) is a key, or Array of keys, of {PDF_STANDARDS}
|
|
225
|
+
# - font_paths is an Array of Strings or Pathnames
|
|
226
|
+
# - ignore_system_fonts is true or false
|
|
227
|
+
# - on_warning (if set) responds to #call
|
|
125
228
|
#
|
|
126
229
|
# @return [true] if configuration is valid
|
|
127
230
|
# @raise [ArgumentError] if typst_executable_path is invalid
|
|
128
231
|
# @raise [ArgumentError] if default_root_path is invalid
|
|
232
|
+
# @raise [ArgumentError] if pdf_standard is invalid
|
|
233
|
+
# @raise [ArgumentError] if font_paths or ignore_system_fonts is invalid
|
|
234
|
+
# @raise [ArgumentError] if on_warning is not callable
|
|
129
235
|
#
|
|
130
236
|
# @example Validate configuration
|
|
131
237
|
# config = Configuration.new
|
|
@@ -136,11 +242,40 @@ module TypstRails
|
|
|
136
242
|
|
|
137
243
|
raise ArgumentError, "typst_executable_path cannot be nil or empty" if typst_executable_path.empty?
|
|
138
244
|
|
|
139
|
-
if default_root_path && !default_root_path.is_a?(String)
|
|
140
|
-
raise ArgumentError, "default_root_path must be a String or nil"
|
|
245
|
+
if default_root_path && !default_root_path.is_a?(String) && !default_root_path.respond_to?(:to_path)
|
|
246
|
+
raise ArgumentError, "default_root_path must be a String, Pathname, or nil"
|
|
141
247
|
end
|
|
142
248
|
|
|
249
|
+
pdf_standards
|
|
250
|
+
validate_font_options!
|
|
251
|
+
raise ArgumentError, "on_warning must respond to #call, or be nil" if on_warning && !on_warning.respond_to?(:call)
|
|
252
|
+
|
|
143
253
|
true
|
|
144
254
|
end
|
|
255
|
+
|
|
256
|
+
private
|
|
257
|
+
|
|
258
|
+
def pdf_standards
|
|
259
|
+
Array(pdf_standard).map do |standard|
|
|
260
|
+
PDF_STANDARDS.fetch(standard) do
|
|
261
|
+
raise ArgumentError, "pdf_standard must be nil, or one or more of: " \
|
|
262
|
+
"#{PDF_STANDARDS.keys.map(&:inspect).join(", ")} (got #{standard.inspect})"
|
|
263
|
+
end
|
|
264
|
+
end.uniq
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def validate_font_options!
|
|
268
|
+
unless font_paths.is_a?(Array) && font_paths.all? { |path| path_like?(path) }
|
|
269
|
+
raise ArgumentError, "font_paths must be an Array of Strings or Pathnames"
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
return if [true, false].include?(ignore_system_fonts)
|
|
273
|
+
|
|
274
|
+
raise ArgumentError, "ignore_system_fonts must be true or false"
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def path_like?(path)
|
|
278
|
+
(path.is_a?(String) && !path.empty?) || path.respond_to?(:to_path)
|
|
279
|
+
end
|
|
145
280
|
end
|
|
146
281
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: typst-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- David J. Berube
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 1980-01-
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: nokogiri
|
|
@@ -180,20 +180,29 @@ files:
|
|
|
180
180
|
- MIT-LICENSE
|
|
181
181
|
- README.md
|
|
182
182
|
- lib/tasks/typst_rails/tasks.rake
|
|
183
|
+
- lib/typst-rails.rb
|
|
183
184
|
- lib/typst_rails.rb
|
|
184
185
|
- lib/typst_rails/backends.rb
|
|
185
186
|
- lib/typst_rails/backends/base.rb
|
|
186
187
|
- lib/typst_rails/backends/cli.rb
|
|
187
188
|
- lib/typst_rails/backends/gem.rb
|
|
188
189
|
- lib/typst_rails/backends/registry.rb
|
|
190
|
+
- lib/typst_rails/data_serializer.rb
|
|
191
|
+
- lib/typst_rails/diagnostics.rb
|
|
192
|
+
- lib/typst_rails/erb_source.rb
|
|
193
|
+
- lib/typst_rails/errors.rb
|
|
189
194
|
- lib/typst_rails/framework_detection.rb
|
|
190
195
|
- lib/typst_rails/handler.rb
|
|
191
196
|
- lib/typst_rails/helpers.rb
|
|
197
|
+
- lib/typst_rails/markdown_converter.rb
|
|
192
198
|
- lib/typst_rails/rage_integration.rb
|
|
199
|
+
- lib/typst_rails/rails_rendering.rb
|
|
193
200
|
- lib/typst_rails/railtie.rb
|
|
194
201
|
- lib/typst_rails/renderer.rb
|
|
195
202
|
- lib/typst_rails/sinatra_integration.rb
|
|
203
|
+
- lib/typst_rails/template_rendering.rb
|
|
196
204
|
- lib/typst_rails/version.rb
|
|
205
|
+
- lib/typst_rails/view_helpers.rb
|
|
197
206
|
homepage: https://github.com/durable-oss/typst-rails
|
|
198
207
|
licenses:
|
|
199
208
|
- MIT
|
|
@@ -219,7 +228,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
219
228
|
- !ruby/object:Gem::Version
|
|
220
229
|
version: '0'
|
|
221
230
|
requirements: []
|
|
222
|
-
rubygems_version: 3.
|
|
231
|
+
rubygems_version: 3.6.9
|
|
223
232
|
specification_version: 4
|
|
224
233
|
summary: Typst typesetting integration for Ruby applications
|
|
225
234
|
test_files: []
|