typst-rails 0.2.1 → 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.
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,6 +85,28 @@ 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
 
@@ -101,12 +124,44 @@ module TypstRails
101
124
  # via {TypstRails::Backends::Registry.register}.
102
125
  attr_accessor :backend
103
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
+
104
155
  # Initializes a new Configuration with default values.
105
156
  #
106
157
  # Default values:
107
158
  # - `typst_executable_path`: "typst" (assumes typst is in PATH)
108
159
  # - `default_root_path`: nil (each render gets a private temporary root)
109
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})
110
165
  #
111
166
  # @return [Configuration] A new configuration instance
112
167
  #
@@ -119,6 +174,46 @@ module TypstRails
119
174
  @typst_executable_path = "typst"
120
175
  @default_root_path = nil
121
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
+ }
122
217
  end
123
218
 
124
219
  # Validates the configuration settings.
@@ -126,10 +221,17 @@ module TypstRails
126
221
  # Ensures that:
127
222
  # - typst_executable_path is a non-empty string
128
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
129
228
  #
130
229
  # @return [true] if configuration is valid
131
230
  # @raise [ArgumentError] if typst_executable_path is invalid
132
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
133
235
  #
134
236
  # @example Validate configuration
135
237
  # config = Configuration.new
@@ -144,7 +246,36 @@ module TypstRails
144
246
  raise ArgumentError, "default_root_path must be a String, Pathname, or nil"
145
247
  end
146
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
+
147
253
  true
148
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
149
280
  end
150
281
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typst-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David J. Berube
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2026-11-14 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: nokogiri
@@ -181,21 +180,27 @@ files:
181
180
  - MIT-LICENSE
182
181
  - README.md
183
182
  - lib/tasks/typst_rails/tasks.rake
183
+ - lib/typst-rails.rb
184
184
  - lib/typst_rails.rb
185
185
  - lib/typst_rails/backends.rb
186
186
  - lib/typst_rails/backends/base.rb
187
187
  - lib/typst_rails/backends/cli.rb
188
188
  - lib/typst_rails/backends/gem.rb
189
189
  - lib/typst_rails/backends/registry.rb
190
+ - lib/typst_rails/data_serializer.rb
191
+ - lib/typst_rails/diagnostics.rb
190
192
  - lib/typst_rails/erb_source.rb
193
+ - lib/typst_rails/errors.rb
191
194
  - lib/typst_rails/framework_detection.rb
192
195
  - lib/typst_rails/handler.rb
193
196
  - lib/typst_rails/helpers.rb
194
197
  - lib/typst_rails/markdown_converter.rb
195
198
  - lib/typst_rails/rage_integration.rb
199
+ - lib/typst_rails/rails_rendering.rb
196
200
  - lib/typst_rails/railtie.rb
197
201
  - lib/typst_rails/renderer.rb
198
202
  - lib/typst_rails/sinatra_integration.rb
203
+ - lib/typst_rails/template_rendering.rb
199
204
  - lib/typst_rails/version.rb
200
205
  - lib/typst_rails/view_helpers.rb
201
206
  homepage: https://github.com/durable-oss/typst-rails
@@ -209,7 +214,6 @@ metadata:
209
214
  bug_tracker_uri: https://github.com/durable-oss/typst-rails/issues
210
215
  documentation_uri: https://rubydoc.info/gems/typst-rails
211
216
  rubygems_mfa_required: 'true'
212
- post_install_message:
213
217
  rdoc_options: []
214
218
  require_paths:
215
219
  - lib
@@ -224,8 +228,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
224
228
  - !ruby/object:Gem::Version
225
229
  version: '0'
226
230
  requirements: []
227
- rubygems_version: 3.3.27
228
- signing_key:
231
+ rubygems_version: 3.6.9
229
232
  specification_version: 4
230
233
  summary: Typst typesetting integration for Ruby applications
231
234
  test_files: []