mirror of
https://github.com/Dadido3/go-typst.git
synced 2025-11-04 13:19:36 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bc69f35948 | |||
| bd9c1beac7 | |||
| 79b86fb22c | |||
| ef185b9601 | |||
| 815a4fea7b | |||
| 473563756d |
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@ -3,6 +3,7 @@
|
|||||||
"Dadido",
|
"Dadido",
|
||||||
"Foogaloo",
|
"Foogaloo",
|
||||||
"golangci",
|
"golangci",
|
||||||
|
"somepath",
|
||||||
"typst",
|
"typst",
|
||||||
"Vogel"
|
"Vogel"
|
||||||
]
|
]
|
||||||
|
|||||||
@ -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
|
package typst_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -15,10 +20,10 @@ func TestCliOptions(t *testing.T) {
|
|||||||
if len(args) != 2 {
|
if len(args) != 2 {
|
||||||
t.Errorf("wrong number of arguments, expected 2, got %d", len(args))
|
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])
|
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])
|
t.Error("wrong font path option, expected my two paths concatenated, got", args[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
cli.go
11
cli.go
@ -14,8 +14,11 @@ import (
|
|||||||
|
|
||||||
// TODO: Add docker support to CLI, by calling docker run instead
|
// 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.
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add method for querying the Typst version resulting in a semver object
|
// 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 != "" {
|
if c.ExecutablePath != "" {
|
||||||
execPath = c.ExecutablePath
|
execPath = c.ExecutablePath
|
||||||
}
|
}
|
||||||
|
if execPath == "" {
|
||||||
|
return "", fmt.Errorf("go-typst doesn't support this platform")
|
||||||
|
}
|
||||||
|
|
||||||
cmd := exec.Command(execPath, "--version")
|
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
|
var output, errBuffer bytes.Buffer
|
||||||
cmd.Stdout = &output
|
cmd.Stdout = &output
|
||||||
@ -60,8 +67,12 @@ func (c CLI) Compile(input io.Reader, output io.Writer, options *CLIOptions) err
|
|||||||
if c.ExecutablePath != "" {
|
if c.ExecutablePath != "" {
|
||||||
execPath = c.ExecutablePath
|
execPath = c.ExecutablePath
|
||||||
}
|
}
|
||||||
|
if execPath == "" {
|
||||||
|
return fmt.Errorf("go-typst doesn't support this platform")
|
||||||
|
}
|
||||||
|
|
||||||
cmd := exec.Command(execPath, args...)
|
cmd := exec.Command(execPath, args...)
|
||||||
|
cmd.Dir = c.WorkingDirectory
|
||||||
cmd.Stdin = input
|
cmd.Stdin = input
|
||||||
cmd.Stdout = output
|
cmd.Stdout = output
|
||||||
|
|
||||||
|
|||||||
12
cli_js.go
Normal file
12
cli_js.go
Normal 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 = ""
|
||||||
22
cli_test.go
22
cli_test.go
@ -1,4 +1,4 @@
|
|||||||
// Copyright (c) 2024 David Vogel
|
// Copyright (c) 2024-2025 David Vogel
|
||||||
//
|
//
|
||||||
// This software is released under the MIT License.
|
// This software is released under the MIT License.
|
||||||
// https://opensource.org/licenses/MIT
|
// https://opensource.org/licenses/MIT
|
||||||
@ -9,6 +9,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"image"
|
"image"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -60,3 +61,22 @@ func TestCLI_Compile(t *testing.T) {
|
|||||||
t.Fatalf("Resulting image height is %d, expected %d.", imgConf.Height, inches*ppi)
|
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
12
cli_wasi.go
Normal 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 = ""
|
||||||
@ -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
|
package typst_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@ -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
|
package typst_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
5
test-files/hello-world-template.typ
Normal file
5
test-files/hello-world-template.typ
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#let template() = {
|
||||||
|
set page(width: auto, height: auto, margin: 5mm)
|
||||||
|
|
||||||
|
heading("Hello World!")
|
||||||
|
}
|
||||||
3
test-files/hello-world.typ
Normal file
3
test-files/hello-world.typ
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#set page(width: auto, height: auto, margin: 5mm)
|
||||||
|
|
||||||
|
#heading("Hello World!")
|
||||||
@ -1,3 +1,8 @@
|
|||||||
|
// Copyright (c) 2025 David Vogel
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
package typst
|
package typst
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user