Class: TypstRails::Renderer
- Inherits:
-
Object
- Object
- TypstRails::Renderer
- Includes:
- Helpers
- Defined in:
- lib/typst_rails/renderer.rb
Overview
The Renderer class is responsible for taking Typst source code, compiling it using the Typst CLI, and returning the resulting PDF data. It can optionally use data passed from a view context (instance variables and locals) to make them available to the Typst template, typically via a temporary JSON file. This class works independently of any web framework.
Key features:
- Automatic data serialization to JSON for Typst templates
- Secure temporary file handling with proper cleanup
- Integration with view contexts for framework support
- Comprehensive error handling and logging
- Input validation and size limits for security
Defined Under Namespace
Classes: TempPaths
Constant Summary collapse
- MAX_SOURCE_SIZE =
Maximum source size (10MB) to prevent memory issues
10 * 1024 * 1024
Constants included from Helpers
Helpers::DEFAULT_ALLOWED_ATTRIBUTES, Helpers::DEFAULT_ALLOWED_TAGS
Instance Attribute Summary collapse
-
#source ⇒ String
readonly
The Typst template source code.
-
#view_context ⇒ Object?
readonly
The view context providing access to helpers and instance variables.
Instance Method Summary collapse
-
#initialize(source) ⇒ Renderer
constructor
Initializes the renderer with the Typst template source.
-
#render(view_context = nil, local_assigns = {}) ⇒ String
Renders the Typst template to PDF.
Methods included from Helpers
#escape_typst, #html_to_markdown, #html_to_typst, #include_markdown, #markdown_to_typst, #sanitize_html, #url_encode
Constructor Details
#initialize(source) ⇒ Renderer
Initializes the renderer with the Typst template source.
78 79 80 81 82 83 84 85 86 |
# File 'lib/typst_rails/renderer.rb', line 78 def initialize(source) raise ArgumentError, "Source cannot be nil" if source.nil? @source = source.to_s return unless @source.bytesize > MAX_SOURCE_SIZE raise ArgumentError, "Source is too large (#{@source.bytesize} bytes, max #{MAX_SOURCE_SIZE} bytes)" end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object (private)
Delegates missing methods to the view_context.
This allows the renderer to access framework helpers from the view context
(e.g., Rails helpers like link_to, image_tag, etc.) when preparing data.
350 351 352 353 354 355 356 |
# File 'lib/typst_rails/renderer.rb', line 350 def method_missing(method_name, *args, &block) if @view_context.respond_to?(method_name) @view_context.public_send(method_name, *args, &block) else super end end |
Instance Attribute Details
#source ⇒ String (readonly)
Returns The Typst template source code.
62 63 64 |
# File 'lib/typst_rails/renderer.rb', line 62 def source @source end |
#view_context ⇒ Object? (readonly)
Returns The view context providing access to helpers and instance variables.
65 66 67 |
# File 'lib/typst_rails/renderer.rb', line 65 def view_context @view_context end |
Instance Method Details
#render(view_context = nil, local_assigns = {}) ⇒ String
Renders the Typst template to PDF.
This method compiles the Typst source code to PDF, optionally injecting data from a view context and local variables. The data is serialized to JSON and made available to the Typst template via a temporary JSON file.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/typst_rails/renderer.rb', line 110 def render(view_context = nil, local_assigns = {}) raise ArgumentError, "local_assigns must be a Hash" unless local_assigns.is_a?(Hash) raise ArgumentError, "Source is empty" if @source.empty? @view_context = view_context data_for_typst = if view_context collect_data_for_typst(view_context, local_assigns) else local_assigns end compile_typst_source(@source, data_for_typst) end |