Class: TypstRails::Renderer

Inherits:
Object
  • Object
show all
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

Examples:

Basic standalone usage

typst_source = "#let data = json(\"typst_data.json\")\n= #data.title"
renderer = TypstRails::Renderer.new(typst_source)
pdf_data = renderer.render(nil, { title: "My Document" })
File.binwrite("output.pdf", pdf_data)

With view context (Rails)

# In a Rails controller:
# @title = "Monthly Report"
# render template: "reports/monthly" # Uses .typ template file

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

Instance Method Summary collapse

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.

Examples:

renderer = Renderer.new("= Hello, Typst!")

Parameters:

  • source (String)

    The Typst template source code

Raises:

  • (ArgumentError)

    if source is nil

  • (ArgumentError)

    if source is too large (exceeds MAX_SOURCE_SIZE)



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.

Parameters:

  • method_name (Symbol)

    The name of the missing method

  • args (Array)

    Arguments to pass to the method

  • block (Proc)

    Block to pass to the method

Returns:

  • (Object)

    The result of calling the method on view_context

Raises:

  • (NoMethodError)

    if neither the renderer nor view_context respond to the method

See Also:

  • #respond_to_missing?


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

#sourceString (readonly)

Returns The Typst template source code.

Returns:

  • (String)

    The Typst template source code



62
63
64
# File 'lib/typst_rails/renderer.rb', line 62

def source
  @source
end

#view_contextObject? (readonly)

Returns The view context providing access to helpers and instance variables.

Returns:

  • (Object, nil)

    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.

Examples:

Render with plain data

renderer = Renderer.new("= #data.title")
pdf = renderer.render(nil, { title: "Report" })

Render with view context (Rails)

renderer = Renderer.new(typst_source)
pdf = renderer.render(view_context, { extra_data: "value" })

Parameters:

  • view_context (Object, nil) (defaults to: nil)

    Optional view context providing access to helpers and instance variables

  • local_assigns (Hash) (defaults to: {})

    A hash of local variables available in the template

Returns:

  • (String)

    Binary string containing the PDF data

Raises:

  • (ArgumentError)

    if local_assigns is not a Hash

  • (ArgumentError)

    if source is empty

  • (TypstRails::Error)

    if Typst compilation fails



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