mirror of
https://github.com/Dadido3/go-typst.git
synced 2025-11-20 11:49:36 +00:00
Add support for multiple Typst callers
- Add Caller interface type - Rename CLIOptions to Options
This commit is contained in:
parent
b419177edc
commit
1294d5f009
@ -21,7 +21,7 @@ Use at your own discretion for production systems.
|
|||||||
## Features
|
## Features
|
||||||
|
|
||||||
- PDF, SVG and PNG generation.
|
- 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.
|
- 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.
|
- 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.
|
- Errors from Typst CLI are returned as structured Go error objects with detailed information, such as line numbers and file paths.
|
||||||
|
|||||||
19
caller.go
Normal file
19
caller.go
Normal file
@ -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
|
||||||
|
}
|
||||||
13
cli.go
13
cli.go
@ -12,16 +12,13 @@ import (
|
|||||||
"os/exec"
|
"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 {
|
type CLI struct {
|
||||||
ExecutablePath string // The Typst executable path can be overridden here. Otherwise the default path will be used.
|
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.
|
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.
|
// VersionString returns the version string as returned by Typst.
|
||||||
func (c CLI) VersionString() (string, error) {
|
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.
|
// Compile takes a Typst document from input, and renders it into the output writer.
|
||||||
// The options parameter is optional.
|
// 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"}
|
args := []string{"c"}
|
||||||
if options != nil {
|
if options != nil {
|
||||||
args = append(args, options.Args()...)
|
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.
|
// 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.
|
// 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 *CLIOptions, variables map[string]any) error {
|
func (c CLI) CompileWithVariables(input io.Reader, output io.Writer, options *Options, variables map[string]any) error {
|
||||||
varBuffer := bytes.Buffer{}
|
varBuffer := bytes.Buffer{}
|
||||||
|
|
||||||
if err := InjectValues(&varBuffer, variables); err != nil {
|
if err := InjectValues(&varBuffer, variables); err != nil {
|
||||||
|
|||||||
@ -37,7 +37,7 @@ func TestCLI_Compile(t *testing.T) {
|
|||||||
|
|
||||||
#lorem(5)`)
|
#lorem(5)`)
|
||||||
|
|
||||||
opts := typst.CLIOptions{
|
opts := typst.Options{
|
||||||
Format: typst.OutputFormatPNG,
|
Format: typst.OutputFormatPNG,
|
||||||
PPI: ppi,
|
PPI: ppi,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -63,7 +63,7 @@ func TestErrors1(t *testing.T) {
|
|||||||
func TestErrors2(t *testing.T) {
|
func TestErrors2(t *testing.T) {
|
||||||
cli := typst.CLI{}
|
cli := typst.CLI{}
|
||||||
|
|
||||||
opts := typst.CLIOptions{
|
opts := typst.Options{
|
||||||
Pages: "a",
|
Pages: "a",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -45,7 +45,8 @@ const (
|
|||||||
PDFStandardUA_1 PDFStandard = "ua-1" // PDF/UA-1 (Available since Typst 0.14.0)
|
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).
|
Root string // Configures the project root (for absolute paths).
|
||||||
Input map[string]string // String key-value pairs visible through `sys.inputs`.
|
Input map[string]string // String key-value pairs visible through `sys.inputs`.
|
||||||
FontPaths []string // Adds additional directories that are recursively searched for fonts.
|
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.
|
// 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 != "" {
|
if c.Root != "" {
|
||||||
result = append(result, "--root", c.Root)
|
result = append(result, "--root", c.Root)
|
||||||
}
|
}
|
||||||
@ -12,8 +12,8 @@ import (
|
|||||||
"github.com/Dadido3/go-typst"
|
"github.com/Dadido3/go-typst"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCliOptions(t *testing.T) {
|
func TestOptions(t *testing.T) {
|
||||||
o := typst.CLIOptions{
|
o := typst.Options{
|
||||||
FontPaths: []string{"somepath/to/somewhere", "another/to/somewhere"},
|
FontPaths: []string{"somepath/to/somewhere", "another/to/somewhere"},
|
||||||
}
|
}
|
||||||
args := o.Args()
|
args := o.Args()
|
||||||
Loading…
Reference in New Issue
Block a user