Module: TypstRails::SinatraIntegration

Defined in:
lib/typst_rails/sinatra_integration.rb

Overview

Integration for Sinatra framework

Defined Under Namespace

Classes: ViewContext

Class Method Summary collapse

Class Method Details

.setupObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/typst_rails/sinatra_integration.rb', line 10

def self.setup
  return unless defined?(::Sinatra)

  require "typst_rails/renderer"

  # Extend Sinatra with Typst rendering capabilities
  ::Sinatra::Base.class_eval do
    # Helper method to render Typst templates in Sinatra
    def typst(template, locals = {}, _options = {})
      typst_source = File.read(template)
      renderer = ::TypstRails::Renderer.new(typst_source)

      # Sinatra doesn't have the same view context as Rails,
      # so we create a simple wrapper
      view_context = ::TypstRails::SinatraIntegration::ViewContext.new(locals)
      pdf_data = renderer.render(view_context, locals)

      content_type "application/pdf"
      pdf_data
    end
  end
end