diff --git a/examples/passing-values/README.md b/examples/passing-values/README.md new file mode 100644 index 0000000..492143d --- /dev/null +++ b/examples/passing-values/README.md @@ -0,0 +1,13 @@ +# Passing values example + +This example demonstrates how to pass values to Typst, which can be useful in rendering custom documents such as reports, invoices, and more. + +## How it works + +This example follows the [template pattern](https://typst.app/docs/tutorial/making-a-template/) described in the Typst documentation. +Here is a short overview of the files: + +- [template.typ](template.typ) defines a Typst template function that constructs a document based on parameters. +- [main.go](main.go) shows how to convert/encode Go values into Typst markup, and how to call/render the template with these converted values. +- [template-preview.typ](template-preview.typ) also invokes the template while providing mock data. + This is useful when you want to preview, update or debug the template. diff --git a/examples/passing-values/main.go b/examples/passing-values/main.go index 407a441..6c75bd3 100644 --- a/examples/passing-values/main.go +++ b/examples/passing-values/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "log" "os" "time" @@ -8,7 +9,7 @@ import ( "github.com/Dadido3/go-typst" ) -// DataEntry contains fake data to be passed to typst. +// DataEntry contains data to be passed to Typst. type DataEntry struct { Name string Size struct{ X, Y, Z float64 } @@ -25,21 +26,28 @@ var TestData = []DataEntry{ } func main() { - typstCLI := typst.CLI{} + var markup bytes.Buffer - r, err := os.Open("template.typ") - if err != nil { - log.Panicf("Failed to open template file for reading: %v.", err) + // Inject Go values as Typst markup. + if err := typst.InjectValues(&markup, map[string]any{"data": TestData, "customText": "This data is coming from a Go application."}); err != nil { + log.Panicf("Failed to inject values into Typst markup: %v.", err) } - defer r.Close() + // Import the template and invoke the template function with the custom data. + // Show is used to replace the current document with whatever content the template function in `template.typ` returns. + markup.WriteString(` +#import "template.typ": template +#show: doc => template(data, customText)`) + + // Compile the prepared markup with Typst and write the result it into `output.pdf`. f, err := os.Create("output.pdf") if err != nil { log.Panicf("Failed to create output file: %v.", err) } defer f.Close() - if err := typstCLI.CompileWithVariables(r, f, nil, map[string]any{"Data": TestData}); err != nil { + typstCLI := typst.CLI{} + if err := typstCLI.Compile(&markup, f, nil); err != nil { log.Panicf("Failed to compile document: %v.", err) } } diff --git a/examples/passing-values/main_test.go b/examples/passing-values/main_test.go new file mode 100644 index 0000000..96ccbb0 --- /dev/null +++ b/examples/passing-values/main_test.go @@ -0,0 +1,16 @@ +package main + +import ( + "testing" +) + +// Run the example as a test. +func TestMain(t *testing.T) { + defer func() { + if r := recover(); r != nil { + t.Error(r) + } + }() + + main() +} diff --git a/examples/passing-values/output.pdf b/examples/passing-values/output.pdf index 718a7ea..68cfaa8 100644 Binary files a/examples/passing-values/output.pdf and b/examples/passing-values/output.pdf differ diff --git a/examples/passing-values/template-test-data.typ b/examples/passing-values/template-preview.typ similarity index 60% rename from examples/passing-values/template-test-data.typ rename to examples/passing-values/template-preview.typ index 6132a68..2025097 100644 --- a/examples/passing-values/template-test-data.typ +++ b/examples/passing-values/template-preview.typ @@ -1,5 +1,12 @@ -#let Data = ( +// Prepare data that will be used as preview. +#let data = ( (Name: "Bell", Size: (X: 80, Y: 40, Z: 40), Created: datetime(year: 2010, month: 12, day: 1, hour: 12, minute: 13, second: 14), Numbers: (1, 2, 3)), (Name: "Bell", Size: (X: 80, Y: 40, Z: 40), Created: datetime(year: 2010, month: 12, day: 1, hour: 12, minute: 13, second: 14), Numbers: (10, 12, 15)), (Name: "Bell", Size: (X: 80, Y: 40, Z: 40), Created: datetime(year: 2010, month: 12, day: 1, hour: 12, minute: 13, second: 14), Numbers: (100, 109, 199, 200)), -) \ No newline at end of file +) + +#let customText = "Hey, this is example data to test the template." + +// Invoke the template with the preview data and replace this whole document with the result. +#import "template.typ": template +#show: doc => template(data, customText) diff --git a/examples/passing-values/template.typ b/examples/passing-values/template.typ index 6d3ef88..4bd7101 100644 --- a/examples/passing-values/template.typ +++ b/examples/passing-values/template.typ @@ -1,33 +1,36 @@ -#set page(paper: "a4") +#let template(data, customText) = { + set page(paper: "a4") -// Uncomment to use test data. -//#import "template-test-data.typ": Data + [= Example] -= List of items + customText -#show table.cell.where(y: 0): strong -#set table( - stroke: (x, y) => if y == 0 { - (bottom: 0.7pt + black) - }, -) + [== List of items] -#table( - columns: 5, - table.header( - [Name], - [Size], - [Example box], - [Created], - [Numbers], - ), - ..for value in Data { - ( - [#value.Name], - [#value.Size], - box(fill: black, width: 0.1mm * value.Size.X, height: 0.1mm * value.Size.Y), - value.Created.display(), - [#list(..for num in value.Numbers {([#num],)})], - ) - } -) + show table.cell.where(y: 0): strong + set table( + stroke: (x, y) => if y == 0 { + (bottom: 0.7pt + black) + }, + ) + + table( + columns: 5, + table.header( + [Name], + [Size], + [Example box], + [Created], + [Numbers], + ), + ..for value in data { + ( + [#value.Name], + [#value.Size], + box(fill: black, width: 0.1mm * value.Size.X, height: 0.1mm * value.Size.Y), + value.Created.display(), + [#list(..for num in value.Numbers {([#num],)})], + ) + } + ) +}