Skip to content

Commit

Permalink
Add convenience functions to compile and watch.
Browse files Browse the repository at this point in the history
  • Loading branch information
GunnarFarneback committed Oct 31, 2024
1 parent 5f8dbb4 commit 11d04c5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ julia> render(1:4);
- Render documents using the Typst compiler
- Display in IJulia.jl, Pluto.jl, and QuartoRunner.jl notebooks
- Use the [JuliaMono](https://github.com/cormullion/juliamono) typeface
- Convenience functions to run `typst compile` and `typst watch`:
- `compile("input.typ", "output.pdf")`
- `watch("input.typ", "output.pdf")`

### Planned

Expand Down
32 changes: 32 additions & 0 deletions src/Commands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,36 @@ TypstError: failed to `run` a `TypstCommand(String[])`
showerror(io::IO, te::TypstError) = print(io,
"TypstError: failed to `run` a `", TypstCommand, "(", te.command.parameters, ")`")

"""
compile(args...)
Run `typst compile` with the provided `args`.
# Examples
compile("input.typ")
compile("input.typ", "output.pdf")
"""
compile(args...) = (run(TypstCommand(["compile", args...])); nothing)

"""
watch(args...)
Run `typst watch` with the provided `args`. This will repeat
compilation of the input file every time it is changed. Use Ctrl-C to
quit the command.
# Examples
watch("input.typ")
watch("input.typ", "output.pdf")
"""
function watch(args...)
try
run(TypstCommand(["watch", args...]))
catch e
e isa InterruptException || rethrow(e)
end
end

end # Commands
4 changes: 2 additions & 2 deletions src/Typstry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ using .Strings: ContextError, Mode, Typst, TypstString, TypstText, @typst_str, c
export ContextError, Mode, Typst, TypstString, TypstText, @typst_str, code, markup, math, context, show_typst

include("Commands.jl")
using .Commands: TypstCommand, TypstError, @typst_cmd, julia_mono, preamble, render, set_preamble
export TypstCommand, TypstError, @typst_cmd, julia_mono, preamble, render, set_preamble
using .Commands: TypstCommand, TypstError, @typst_cmd, julia_mono, preamble, render, set_preamble, compile, watch
export TypstCommand, TypstError, @typst_cmd, julia_mono, preamble, render, set_preamble, compile, watch

"""
compile_workload(examples)
Expand Down
15 changes: 15 additions & 0 deletions test/interface/TestCommands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ const tc_ignorestatus = ignorestatus(tc_error)
@testset "`julia_mono`" begin @test julia_mono isa String end

@testset "`render`" begin end

@testset "`compile`" begin
mktempdir() do tmpdir
infile = joinpath(tmpdir, "test.typ")
outfile1 = joinpath(tmpdir, "test.pdf")
outfile2 = joinpath(tmpdir, "out.pdf")
write(infile, "= Test Document\n")
compile(infile)
@test isfile(outfile1)
compile(infile, outfile2)
@test isfile(outfile2)
end
end

@testset "`watch`" begin end
end

@testset "`Base`" begin
Expand Down

0 comments on commit 11d04c5

Please sign in to comment.