From 1294d5f009d9ea0665788481fe296e0dea209c21 Mon Sep 17 00:00:00 2001 From: David Vogel Date: Sat, 15 Nov 2025 20:38:04 +0100 Subject: [PATCH] Add support for multiple Typst callers - Add Caller interface type - Rename CLIOptions to Options --- README.md | 2 +- caller.go | 19 +++++++++++++++++++ cli.go | 13 +++++-------- cli_test.go | 2 +- errors_test.go | 2 +- cli-options.go => options.go | 5 +++-- cli-options_test.go => options_test.go | 4 ++-- 7 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 caller.go rename cli-options.go => options.go (97%) rename cli-options_test.go => options_test.go (91%) diff --git a/README.md b/README.md index 57762a6..3952800 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Use at your own discretion for production systems. ## Features - PDF, SVG and PNG generation. -- All Typst parameters are discoverable and documented in [cli-options.go](cli-options.go). +- All Typst parameters are discoverable and documented in [options.go](options.go). - Go-to-Typst Value Encoder: Seamlessly inject any Go values. - Encode and inject images as a Typst markup simply by [wrapping](image.go) `image.Image` types or byte slices with raw JPEG or PNG data. - Errors from Typst CLI are returned as structured Go error objects with detailed information, such as line numbers and file paths. diff --git a/caller.go b/caller.go new file mode 100644 index 0000000..269f5ef --- /dev/null +++ b/caller.go @@ -0,0 +1,19 @@ +package typst + +import "io" + +// TODO: Add an interface for the Typst caller and let CLI (and later docker and WASM) be implementations of that + +// TODO: Add docker support to CLI, by calling docker run instead + +// TODO: Add special type "Filename" (or similar) that implements a io.Reader/io.Writer that can be plugged into the input and output parameters of the Compile method + +// Caller contains all functions that can be +type Caller interface { + // VersionString returns the version string as returned by Typst. + VersionString() (string, error) + + // Compile takes a Typst document from the supplied input reader, and renders it into the output writer. + // The options parameter is optional, and can be nil. + Compile(input io.Reader, output io.Writer, options *Options) error +} diff --git a/cli.go b/cli.go index 36aefa9..533a992 100644 --- a/cli.go +++ b/cli.go @@ -12,16 +12,13 @@ import ( "os/exec" ) -// TODO: Add docker support to CLI, by calling docker run instead - -// TODO: Add an interface for the Typst caller and let CLI (and later docker and WASM) be implementations of that - type CLI struct { ExecutablePath string // The Typst executable path can be overridden here. Otherwise the default path will be used. WorkingDirectory string // The path where the Typst executable is run in. When left empty, the Typst executable will be run in the process's current directory. } -// TODO: Add method for querying the Typst version resulting in a semver object +// Ensure that CLI implements the Caller interface. +var _ Caller = CLI{} // VersionString returns the version string as returned by Typst. func (c CLI) VersionString() (string, error) { @@ -55,7 +52,7 @@ func (c CLI) VersionString() (string, error) { // Compile takes a Typst document from input, and renders it into the output writer. // The options parameter is optional. -func (c CLI) Compile(input io.Reader, output io.Writer, options *CLIOptions) error { +func (c CLI) Compile(input io.Reader, output io.Writer, options *Options) error { args := []string{"c"} if options != nil { args = append(args, options.Args()...) @@ -96,8 +93,8 @@ func (c CLI) Compile(input io.Reader, output io.Writer, options *CLIOptions) err // // Additionally this will inject the given map of variables into the global scope of the Typst document. // -// Deprecated: You should use InjectValues in combination with the normal Compile method instead. -func (c CLI) CompileWithVariables(input io.Reader, output io.Writer, options *CLIOptions, variables map[string]any) error { +// Deprecated: You should use typst.InjectValues in combination with the normal Compile method instead. +func (c CLI) CompileWithVariables(input io.Reader, output io.Writer, options *Options, variables map[string]any) error { varBuffer := bytes.Buffer{} if err := InjectValues(&varBuffer, variables); err != nil { diff --git a/cli_test.go b/cli_test.go index c7158c7..088cbea 100644 --- a/cli_test.go +++ b/cli_test.go @@ -37,7 +37,7 @@ func TestCLI_Compile(t *testing.T) { #lorem(5)`) - opts := typst.CLIOptions{ + opts := typst.Options{ Format: typst.OutputFormatPNG, PPI: ppi, } diff --git a/errors_test.go b/errors_test.go index 0486ae2..b7c2855 100644 --- a/errors_test.go +++ b/errors_test.go @@ -63,7 +63,7 @@ func TestErrors1(t *testing.T) { func TestErrors2(t *testing.T) { cli := typst.CLI{} - opts := typst.CLIOptions{ + opts := typst.Options{ Pages: "a", } diff --git a/cli-options.go b/options.go similarity index 97% rename from cli-options.go rename to options.go index 3b4910c..1c61930 100644 --- a/cli-options.go +++ b/options.go @@ -45,7 +45,8 @@ const ( PDFStandardUA_1 PDFStandard = "ua-1" // PDF/UA-1 (Available since Typst 0.14.0) ) -type CLIOptions struct { +// Options contains all parameters that can be passed to a Typst CLI. +type Options struct { Root string // Configures the project root (for absolute paths). Input map[string]string // String key-value pairs visible through `sys.inputs`. FontPaths []string // Adds additional directories that are recursively searched for fonts. @@ -76,7 +77,7 @@ type CLIOptions struct { } // Args returns a list of CLI arguments that should be passed to the executable. -func (c *CLIOptions) Args() (result []string) { +func (c *Options) Args() (result []string) { if c.Root != "" { result = append(result, "--root", c.Root) } diff --git a/cli-options_test.go b/options_test.go similarity index 91% rename from cli-options_test.go rename to options_test.go index 0e0ad36..4309833 100644 --- a/cli-options_test.go +++ b/options_test.go @@ -12,8 +12,8 @@ import ( "github.com/Dadido3/go-typst" ) -func TestCliOptions(t *testing.T) { - o := typst.CLIOptions{ +func TestOptions(t *testing.T) { + o := typst.Options{ FontPaths: []string{"somepath/to/somewhere", "another/to/somewhere"}, } args := o.Args()