Class: Typstify::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/typstify/resolver.rb

Overview

Turns a template name ("invoices/show") into a file on disk, and refuses to look outside template_root while doing it.

This is the first of two containment layers. The second — and the one that actually matters — is the workspace: Typst compiles with the tmpdir as its root, so even a template that somehow got resolved elsewhere could not read past it. This layer exists to turn a traversal attempt into a clear error rather than a confusing "file not found".

Defined Under Namespace

Classes: Resolution

Constant Summary collapse

EXTENSIONS =

Data mode wins: a directory holding both show.typ and show.typ.erb renders the safe one.

{ ".typ" => :data, ".typ.erb" => :erb }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config = Typstify.config) ⇒ Resolver

Returns a new instance of Resolver.



23
24
25
# File 'lib/typstify/resolver.rb', line 23

def initialize(config = Typstify.config)
  @config = config
end

Instance Method Details

#call(name) ⇒ Resolution

Parameters:

  • name (String)

    e.g. "invoices/show", with or without extension

Returns:

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/typstify/resolver.rb', line 31

def call(name)
  stem = name.to_s.sub(/\.typ(\.erb)?\z/, "")
  root = @config.template_root.expand_path

  tried = EXTENSIONS.keys.map { |ext| candidate(root, stem, ext) }

  EXTENSIONS.each_with_index do |(_ext, mode), index|
    path = tried[index]
    return Resolution.new(name: stem, path: path, mode: mode) if path.file?
  end

  raise MissingTemplate.new(name, tried)
end