Compare commits

...

6 Commits
v0.4.0 ... main

Author SHA1 Message Date
bc69f35948 Allow it to compile on unsupported platforms
Some checks failed
golangci-lint / lint (push) Successful in 1m16s
test / test (1.23.x, 0.12.0) (push) Failing after 4s
test / test (1.23.x, 0.13.0) (push) Failing after 3s
test / test (1.23.x, 0.13.1) (push) Failing after 4s
test / test (1.23.x, 0.14.0) (push) Failing after 3s
2025-11-03 21:44:01 +01:00
bd9c1beac7 Add a way to change the working directory 2025-11-03 19:50:24 +01:00
79b86fb22c Add TODO comment 2025-11-03 19:48:51 +01:00
ef185b9601 Add missing license headers 2025-11-03 19:48:05 +01:00
815a4fea7b Add some test files. 2025-11-03 19:45:57 +01:00
473563756d Fix linter warning about yoda conditions 2025-11-03 19:31:11 +01:00
11 changed files with 88 additions and 4 deletions

View File

@ -3,6 +3,7 @@
"Dadido",
"Foogaloo",
"golangci",
"somepath",
"typst",
"Vogel"
]

View File

@ -1,3 +1,8 @@
// Copyright (c) 2025 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package typst_test
import (
@ -15,10 +20,10 @@ func TestCliOptions(t *testing.T) {
if len(args) != 2 {
t.Errorf("wrong number of arguments, expected 2, got %d", len(args))
}
if "--font-path" != args[0] {
if args[0] != "--font-path" {
t.Error("wrong font path option, expected --font-path, got", args[0])
}
if "somepath/to/somewhere"+string(os.PathListSeparator)+"another/to/somewhere" != args[1] {
if args[1] != "somepath/to/somewhere"+string(os.PathListSeparator)+"another/to/somewhere" {
t.Error("wrong font path option, expected my two paths concatenated, got", args[1])
}
}

13
cli.go
View File

@ -14,8 +14,11 @@ import (
// 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.
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
@ -27,8 +30,12 @@ func (c CLI) VersionString() (string, error) {
if c.ExecutablePath != "" {
execPath = c.ExecutablePath
}
if execPath == "" {
return "", fmt.Errorf("go-typst doesn't support this platform")
}
cmd := exec.Command(execPath, "--version")
cmd.Dir = c.WorkingDirectory // This doesn't do anything, but we will do it anyways for consistency.
var output, errBuffer bytes.Buffer
cmd.Stdout = &output
@ -60,8 +67,12 @@ func (c CLI) Compile(input io.Reader, output io.Writer, options *CLIOptions) err
if c.ExecutablePath != "" {
execPath = c.ExecutablePath
}
if execPath == "" {
return fmt.Errorf("go-typst doesn't support this platform")
}
cmd := exec.Command(execPath, args...)
cmd.Dir = c.WorkingDirectory
cmd.Stdin = input
cmd.Stdout = output

12
cli_js.go Normal file
View File

@ -0,0 +1,12 @@
// Copyright (c) 2025 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
//go:build js
package typst
// The path to the Typst executable.
// We leave that empty as we don't support this platform for now.
var ExecutablePath = ""

View File

@ -1,4 +1,4 @@
// Copyright (c) 2024 David Vogel
// Copyright (c) 2024-2025 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
@ -9,6 +9,7 @@ import (
"bytes"
"image"
_ "image/png"
"path/filepath"
"strconv"
"testing"
@ -60,3 +61,22 @@ func TestCLI_Compile(t *testing.T) {
t.Fatalf("Resulting image height is %d, expected %d.", imgConf.Height, inches*ppi)
}
}
// Test basic compile functionality with a given working directory.
func TestCLI_CompileWithWorkingDir(t *testing.T) {
cli := typst.CLI{
WorkingDirectory: filepath.Join(".", "test-files"),
}
r := bytes.NewBufferString(`#import "hello-world-template.typ": template
#show: doc => template()`)
var w bytes.Buffer
err := cli.Compile(r, &w, nil)
if err != nil {
t.Fatalf("Failed to compile document: %v.", err)
}
if w.Available() == 0 {
t.Errorf("No output was written.")
}
}

12
cli_wasi.go Normal file
View File

@ -0,0 +1,12 @@
// Copyright (c) 2025 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
//go:build wasip1
package typst
// The path to the Typst executable.
// We leave that empty as we don't support this platform for now.
var ExecutablePath = ""

View File

@ -1,3 +1,8 @@
// Copyright (c) 2025 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package typst_test
import (

View File

@ -1,3 +1,8 @@
// Copyright (c) 2025 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package typst_test
import (

View File

@ -0,0 +1,5 @@
#let template() = {
set page(width: auto, height: auto, margin: 5mm)
heading("Hello World!")
}

View File

@ -0,0 +1,3 @@
#set page(width: auto, height: auto, margin: 5mm)
#heading("Hello World!")

View File

@ -1,3 +1,8 @@
// Copyright (c) 2025 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package typst
import (