Skip to content

Commit

Permalink
Organize code
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobjpeters committed Nov 8, 2024
1 parent 99a4d07 commit f696b22
Show file tree
Hide file tree
Showing 14 changed files with 572 additions and 561 deletions.
4 changes: 0 additions & 4 deletions ext/LaTeXStringsExtension.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import Typstry: show_typst
using LaTeXStrings: LaTeXString, @L_str
using Typstry: compile_workload, show_raw

# Strings

"""
show_typst(io, ::LaTeXString)
Expand All @@ -18,8 +16,6 @@ Print in Typst format for LaTeXStrings.jl.
"""
show_typst(io, tc, x::LaTeXString) = show_raw(print, io, tc, x, "latex")

# Internals

const examples = [L"a" => LaTeXString]

compile_workload(examples)
Expand Down
4 changes: 0 additions & 4 deletions ext/MarkdownExtension.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import Typstry: show_typst
using Markdown: MD, @md_str
using Typstry: compile_workload, show_raw

# Strings

"""
show_typst(io, ::Markdown.MD)
Expand All @@ -25,8 +23,6 @@ show_typst(io, tc, x::MD) = show_raw(io, tc, x, "markdown") do io, x
print(io, read(buffer, String)[begin:end - 1])
end

# Internals

const examples = [md"# A" => MD]

compile_workload(examples)
Expand Down
36 changes: 36 additions & 0 deletions src/Typstry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,42 @@ export
@typst_cmd, @typst_str, code, context, julia_mono,
markup, math, render, set_context, show_typst, typst

"""
examples
A constant `Vector` of Julia values and their corresponding
`Type`s implemented for [`show_typst`](@ref).
"""
const examples = [
Any[true, 1, 1.2, 1 // 2] => AbstractArray
'a' => AbstractChar
1.2 => AbstractFloat
Any[true 1; 1.2 1 // 2] => AbstractMatrix
"a" => AbstractString
true => Bool
im => Complex{Bool}
1 + 2im => Complex
π => Irrational
nothing => Nothing
0:2:6 => OrdinalRange{<:Integer, <:Integer}
1 // 2 => Rational
r"[a-z]" => Regex
1 => Signed
StepRangeLen(0, 2, 4) => StepRangeLen{<:Integer, <:Integer, <:Integer}
(true, 1, 1.2, 1 // 2) => Tuple
Typst(1) => Typst
typst"[\"a\"]" => TypstString
TypstText([1, 2, 3, 4]) => TypstText
0xff => Unsigned
v"1.2.3" => VersionNumber
html"<p>a</p>" => HTML
text"[\"a\"]" => Text
Date(1) => Date
DateTime(1) => DateTime
Day(1) => Period
Time(0) => Time
]

compile_workload(examples)

end # Typstry
110 changes: 28 additions & 82 deletions src/commands/commands.jl
Original file line number Diff line number Diff line change
@@ -1,60 +1,7 @@

"""
typst_command_error(tc)
"""
typst_command_error(tc) = TypstCommandError(tc)

include("typst_command.jl")
include("typst_command_error.jl")

# Internals

"""
format(::Union{MIME"application/pdf", MIME"image/png", MIME"image/svg+xml"})
Return the image format acronym corresponding to the given `MIME`.
# Examples
```jldoctest
julia> Typstry.format(MIME"application/pdf"())
"pdf"
julia> Typstry.format(MIME"image/png"())
"png"
julia> Typstry.format(MIME"image/svg+xml"())
"svg"
```
"""
format(::MIME"application/pdf") = "pdf"
format(::MIME"image/png") = "png"
format(::MIME"image/svg+xml") = "svg"

# `Typstry`

"""
@typst_cmd("s")
typst`s`
Construct a [`TypstCommand`](@ref) where each parameter is separated by a space.
This does not support interpolation; use the constructor instead.
# Examples
```jldoctest
julia> typst`help`
typst`help`
julia> typst`compile input.typ output.typ`
typst`compile input.typ output.typ`
```
"""
macro typst_cmd(parameters::String)
:(TypstCommand($(isempty(parameters) ? String[] : map(string, split(parameters, " ")))))
end

"""
julia_mono
Expand Down Expand Up @@ -103,7 +50,7 @@ function render(value;
)
Base.open(input; truncate = true) do file
tc = render!(context)
print(file, unwrap(tc, TypstString, :preamble))
print(file, preamble(tc))
_show_typst(file, tc, value)
println(file)
end
Expand All @@ -112,32 +59,6 @@ function render(value;
ignorestatus))
end

"""
typst(::AbstractString; catch_interrupt = true, ignorestatus = true)
Convenience function intended for interactive use, emulating the typst
command line interface. Be aware, however, that it strictly splits
on spaces and does not provide any shell-style escape mechanism,
so it will not work if there are, e.g., filenames with spaces.
When `catch_interrupt` is true, CTRL-C quietly quits the command.
When [`ignorestatus`](@ref) is true, a Typst failure will not imply a julia error.
If the `"TYPST_FONT_PATHS"` environment variable is not set,
it is temporarily set to [`julia_mono`](@ref).
"""
function typst(parameters::AbstractString; catch_interrupt = true, ignorestatus = true)
tc = addenv(TypstCommand(TypstCommand(split(parameters)); ignorestatus),
"TYPST_FONT_PATHS" => get(ENV, "TYPST_FONT_PATHS", julia_mono))
if catch_interrupt
try run(tc)
catch e e isa InterruptException || rethrow()
end
else run(tc)
end
nothing
end

"""
show(::IO, ::Union{
MIME"application/pdf", MIME"image/png", MIME"image/svg+xml"
Expand All @@ -163,9 +84,34 @@ function show(io::IO, m::Union{
input = tempname()
output = input * "." * format(m)

render(t; input, output, open = false, ignorestatus = false,
context = unwrap(io, :typst_context, TypstContext()))
render(t; input, output, open = false, ignorestatus = false, context = typst_context(io))
write(io, read(output))

nothing
end

"""
typst(::AbstractString; catch_interrupt = true, ignorestatus = true)
Convenience function intended for interactive use, emulating the typst
command line interface. Be aware, however, that it strictly splits
on spaces and does not provide any shell-style escape mechanism,
so it will not work if there are, e.g., filenames with spaces.
When `catch_interrupt` is true, CTRL-C quietly quits the command.
When [`ignorestatus`](@ref) is true, a Typst failure will not imply a julia error.
If the `"TYPST_FONT_PATHS"` environment variable is not set,
it is temporarily set to [`julia_mono`](@ref).
"""
function typst(parameters::AbstractString; catch_interrupt = true, ignorestatus = true)
tc = addenv(TypstCommand(TypstCommand(split(parameters)); ignorestatus),
"TYPST_FONT_PATHS" => get(ENV, "TYPST_FONT_PATHS", julia_mono))
if catch_interrupt
try run(tc)
catch e e isa InterruptException || rethrow()
end
else run(tc)
end
nothing
end
53 changes: 33 additions & 20 deletions src/commands/typst_command.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@

"""
apply(f, tc, args...; kwargs...)
"""
function apply(f, tc, args...; kwargs...)
_tc = deepcopy(tc)
_tc.compiler = f(_tc.compiler, args...; kwargs...)
_tc
end

"""
TypstCommand(::AbstractVector{<:AbstractString})
TypstCommand(::TypstCommand; kwargs...)
Expand All @@ -21,31 +12,31 @@ Keyword parameters have the same semantics as for a `Cmd`.
This type implements the `Cmd` interface.
However, the interface is undocumented, which may result in unexpected behavior.
- `addenv(::TypstCommand, env...; ::Bool = true)`
- `addenv(::TypstCommand,\u00A0env...;\u00A0::Bool\u00A0=\u00A0true)`
- Can be used with [`julia_mono`](@ref)
- `addenv(::TypstCommand, "TYPST_FONT_PATHS" => julia_mono)`
- `addenv(::TypstCommand,\u00A0"TYPST_FONT_PATHS"\u00A0=>\u00A0julia_mono)`
- `detach(::TypstCommand)`
- `eltype(::Type{TypstCommand})`
- `firstindex(::TypstCommand)`
- `getindex(::TypstCommand, i)`
- `hash(::TypstCommand, ::UInt)`
- `getindex(::TypstCommand,\u00A0i)`
- `hash(::TypstCommand,\u00A0::UInt)`
- `ignorestatus(::TypstCommand)`
- Do not throw a [`TypstCommandError`](@ref) if the Typst compiler throws an error.
Errors thrown by the Typst compiler are printed to `stderr` regardless.
- `iterate(::TypstCommand, i)`
- `iterate(::TypstCommand,\u00A0i)`
- `iterate(::TypstCommand)`
- `keys(::TypstCommand)`
- `lastindex(::TypstCommand)`
- `length(::TypstCommand)`
- `run(::TypstCommand, args...; ::Bool = true)`
- `run(::TypstCommand,\u00A0args...;\u00A0::Bool\u00A0=\u00A0true)`
- Errors thrown by the Typst compiler will be printed to `stderr`.
Then, a Julia [`TypstCommandError`](@ref) will be
thrown unless the [`ignorestatus`](@ref) flag is set.
- `setcpuaffinity(::TypstCommand, cpus)`
- `setenv(::TypstString, env...; kwargs...)`
- `setcpuaffinity(::TypstCommand,\u00A0cpus)`
- `setenv(::TypstString,\u00A0env...;\u00A0kwargs...)`
- Can be used with [`julia_mono`](@ref)
- `setenv(::TypstCommand, "TYPST_FONT_PATHS" => julia_mono)`
- `show(::IO, ::MIME"text/plain", ::TypstCommand)`
- `setenv(::TypstCommand,\u00A0"TYPST_FONT_PATHS"\u00A0=>\u00A0julia_mono)`
- `show(::IO,\u00A0::MIME"text/plain",\u00A0::TypstCommand)`
# Examples
Expand All @@ -67,6 +58,28 @@ mutable struct TypstCommand
new(tc.parameters, ignorestatus, Cmd(tc.compiler; kwargs...))
end

"""
@typst_cmd("s")
typst`s`
Construct a [`TypstCommand`](@ref) where each parameter is separated by a space.
This does not support interpolation; use the constructor instead.
# Examples
```jldoctest
julia> typst`help`
typst`help`
julia> typst`compile input.typ output.typ`
typst`compile input.typ output.typ`
```
"""
macro typst_cmd(parameters::String)
:(TypstCommand($(isempty(parameters) ? String[] : map(string, split(parameters, " ")))))
end

tc::TypstCommand == _tc::TypstCommand =
tc.compiler == _tc.compiler &&
tc.parameters == _tc.parameters &&
Expand Down Expand Up @@ -103,7 +116,7 @@ length(tc::TypstCommand) = length(tc.parameters) + 1

function run(tc::TypstCommand, args...; wait::Bool = true)
process = run(ignorestatus(Cmd(`$(tc.compiler) $(tc.parameters)`)), args...; wait)
tc.ignore_status || success(process) || throw(typst_command_error(tc))
tc.ignore_status || success(process) || throw(TypstCommandError(tc))
process
end

Expand Down
4 changes: 2 additions & 2 deletions src/commands/typst_command_error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ An `Exception` indicating a failure to [`run`](@ref) a [`TypstCommand`](@ref).
Implements the `Exception` interface.
- `showerror(::IO, ::TypstCommandError)`
- `show(::IO, ::MIME"text/plain", ::TypstCommandError)`
- `showerror(::IO,\u00A0::TypstCommandError)`
- `show(::IO,\u00A0::MIME"text/plain",\u00A0::TypstCommandError)`
# Examples
```jldoctest
Expand Down
Loading

0 comments on commit f696b22

Please sign in to comment.