Skip to content

Commit

Permalink
Move preamble into the context
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobjpeters committed Nov 8, 2024
1 parent ec902fc commit c02e14b
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 149 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
## v0.5.0

- `TypstError` renamed to `TypstCommandError`
- `set_preamble` no longer returns its value
- `TypstContext`
- `preamble` and `set_preamble` replaced by `context::TypstContext` and `set_context`
- New `TypstString(::TypstContext, ::Any)` constructor
- `repr(::MIME"text/typst", ::TypstString)` now returns a `String` instead of a `TypstString`
- `render` takes a `context = TypstContext()` instead of extra keyword parameters
Expand Down
11 changes: 6 additions & 5 deletions src/Typstry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ using .Strings:
using .TypstContexts: TypstContext, context, set_context

include("commands/Commands.jl")
using .Commands: Preamble, TypstCommandErrors.TypstCommandError, TypstCommands.TypstCommand,
using .Commands:
TypstCommandErrors.TypstCommandError, TypstCommands.TypstCommand,
@typst_cmd, julia_mono, render, typst
using .Preamble: preamble, set_preamble

export
ContextError, Mode, TypstCommandError, TypstCommand, TypstContext, TypstString, TypstText, Typst,
@typst_cmd, @typst_str,
code, context, julia_mono, markup, math, preamble, render, set_context, set_preamble, show_typst, typst
ContextError, Mode, TypstCommandError, TypstCommand,
TypstContext, TypstString, TypstText, Typst,
@typst_cmd, @typst_str, code, context, julia_mono,
markup, math, render, set_context, show_typst, typst

Strings.compile_workload(Strings.examples)

Expand Down
21 changes: 11 additions & 10 deletions src/commands/Commands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Typstry.Commands
module Commands

import Base: show
using ..Typstry: Utilities, TypstContext, TypstString, TypstText, Typst, Strings._show_typst
using ..Typstry: Strings, Utilities, TypstContext, TypstString, TypstText, Typst
using .Strings: TypstContexts, ShowTypst._show_typst
using .TypstContexts: context, merge_contexts!
using .Utilities: ContextErrors.unwrap
using Artifacts: @artifact_str

Expand All @@ -27,9 +29,6 @@ typst_command_error(tc) = TypstCommandError(tc)
include("TypstCommands.jl")
using .TypstCommands: TypstCommand

include("Preamble.jl")
using .Preamble: preamble

include("TypstCommandErrors.jl")
using .TypstCommandErrors: TypstCommandError

Expand Down Expand Up @@ -92,14 +91,16 @@ Use with a [`TypstCommand`](@ref) and one of [`addenv`](@ref),
"""
const julia_mono = artifact"JuliaMono"

render!(tc) = merge_contexts!(tc, context)

"""
render(value;
input = "input.typ",
output = "output.pdf",
open = true,
ignorestatus = true,
preamble = preamble,
context...)
context = TypstContext()
)
Render to a document using
[`show(::IO,\u00A0::MIME"text/typst",\u00A0::Typst)`](@ref).
Expand All @@ -123,12 +124,12 @@ function render(value;
output = "output.pdf",
open = true,
ignorestatus = true,
preamble = preamble,
context = TypstContext()
)
Base.open(input; truncate = true) do file
_show_typst(file, context, preamble)
_show_typst(file, context, value)
tc = render!(context)
print(file, unwrap(tc, TypstString, :preamble))
_show_typst(file, tc, value)
println(file)
end
run(TypstCommand(TypstCommand(
Expand Down Expand Up @@ -188,7 +189,7 @@ function show(io::IO, m::Union{
output = input * "." * format(m)

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

nothing
Expand Down
39 changes: 0 additions & 39 deletions src/commands/Preamble.jl

This file was deleted.

112 changes: 56 additions & 56 deletions src/strings/ShowTypst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using Dates:
day, hour, minute, month, second, year
using ..Strings:
TypstContexts, Utilities, TypstStrings.TypstString, Typst, TypstText,
@typst_str, code, markup, math, unwrap
code, markup, math, ContextErrors.unwrap
using .TypstContexts: TypstContext, context, default_context, merge_contexts!
using .Utilities: enclose, join_with

Expand Down Expand Up @@ -165,68 +165,13 @@ function dates(x::Period)
"duration", (duration(x),), (TypstText(readuntil(buffer, " ")),)
end


function _show_typst(io, tc, x)
_tc = TypstContext(x)
merge!(merge_contexts!(_tc, context), tc)
show_typst(io, _tc, x)
end
_show_typst(io, x; kwargs...) = _show_typst(io, TypstContext(; kwargs...), x)

"""
show_typst(::IO = stdout, ::TypstContext = TypstContext(), x)
Print in Typst format with Julia settings and Typst parameters provided by an `IOContext`.
Implement this function for a custom type to specify its Typst formatting.
A setting is a value used in Julia, whose type varies across settings.
A parameter is passed directly to a Typst function and must be a [`TypstString`](@ref)
with the same name as in Typst, except that dashes are replaced with underscores.
Settings each have a default value, whereas the default values of parameters are handled by Typst functions.
Some settings, such as `block`, correspond with a parameter but may also be used in Julia.
For additional information on settings and parameters, see also [`context`](@ref)
and the [Typst Documentation](https://typst.app/docs/), respectively.
!!! info
Some types, particularly containers, may call
[`show(::IO,\u00A0::MIME"text/typst",\u00A0::Typst)`](@ref)
to format a value, which may use additional settings and parameters.
!!! warning
This function's methods are incomplete.
Please file an issue or create a pull-request for missing methods.
| Type | Settings | Parameters |
|:----------------------------------------------------------|:-----------------------------------------|:--------------------------------------------------------|
| `AbstractArray` | `:block`, `:depth`, `:mode`, `:tab_size` | `:delim`, `:gap` |
| `AbstractChar` | | |
| `AbstractFloat` | `:mode` | |
| `AbstractMatrix` | `:block`, `:depth`, `:mode`, `:tab_size` | `:augment`, `:column_gap`, `:delim`, `:gap`, `:row_gap` |
| `AbstractString` | | |
| `Bool` | `:mode` | |
| `Complex{Bool}` | `:block`, `:mode`, `:parenthesize` | |
| `Complex` | `:block`, `:mode`, `:parenthesize` | |
| `Irrational` | `:mode` | |
| `Nothing` | `:mode` | |
| `OrdinalRange{<:Integer,\u00A0<:Integer}` | `:mode` | |
| `Rational` | `:block`, `:mode`, `:parenthesize` | |
| `Regex` | `:mode` | |
| `Signed` | `:mode` | |
| `StepRangeLen{<:Integer,\u00A0<:Integer,\u00A0<:Integer}` | `:mode` | |
| `Tuple` | `:block`, `:depth`, `:mode`, `:tab_size` | `:delim`, `:gap` |
| `Typst` | | |
| `TypstString` | | |
| `TypstText` | | |
| `Unsigned` | `:mode` | |
| `VersionNumber` | `:mode` | |
| `Docs.HTML` | `:block`, `:depth`, `:mode`, `:tab_size` | |
| `Docs.Text` | `:mode` | |
| `Dates.Date` | `:mode`, `:indent` | |
| `Dates.DateTime` | `:mode`, `:indent` | |
| `Dates.Period` | `:mode`, `:indent` | |
| `Dates.Time` | `:mode`, `:indent` | |
"""
show_typst(io, tc, x::AbstractChar) = show_typst(io, tc, string(x))
show_typst(io, tc, x::AbstractFloat) =
if isinf(x)
Expand Down Expand Up @@ -351,4 +296,59 @@ end
show_typst(tc, x) = _show_typst(stdout, tc, x)
show_typst(x) = show_typst(TypstContext(), x)

@doc """
show_typst(::IO = stdout, ::TypstContext, x)
show_typst(::TypstContext = TypstContext(), x)
Print in Typst format with Julia settings and Typst
parameters provided by the [`TypstContext`](@ref).
Implement the three-parameter form of this function
for a custom type to specify its Typst formatting.
A setting is a value used in Julia, whose type varies across settings.
A parameter is passed directly to a Typst function and must be a [`TypstString`](@ref)
with the same name as in Typst, except that dashes are replaced with underscores.
Settings each have a default value specified by [`context`](@ref),
whereas the default values of parameters are handled in Typst functions.
Some settings, such as `block`, correspond with a parameter but may also be used in Julia.
!!! tip
Please create an issue or pull-request to implement new methods.
!!! info
Some types, particularly containers, may call
[`show(::IO,\u00A0::MIME"text/typst",\u00A0::Typst)`](@ref)
to format a value, which may use additional settings and parameters.
| Type | Settings | Parameters |
|:----------------------------------------------------------|:-----------------------------------------|:--------------------------------------------------------|
| `AbstractArray` | `:block`, `:depth`, `:mode`, `:tab_size` | `:delim`, `:gap` |
| `AbstractChar` | | |
| `AbstractFloat` | `:mode` | |
| `AbstractMatrix` | `:block`, `:depth`, `:mode`, `:tab_size` | `:augment`, `:column_gap`, `:delim`, `:gap`, `:row_gap` |
| `AbstractString` | | |
| `Bool` | `:mode` | |
| `Complex{Bool}` | `:block`, `:mode`, `:parenthesize` | |
| `Complex` | `:block`, `:mode`, `:parenthesize` | |
| `Irrational` | `:mode` | |
| `Nothing` | `:mode` | |
| `OrdinalRange{<:Integer,\u00A0<:Integer}` | `:mode` | |
| `Rational` | `:block`, `:mode`, `:parenthesize` | |
| `Regex` | `:mode` | |
| `Signed` | `:mode` | |
| `StepRangeLen{<:Integer,\u00A0<:Integer,\u00A0<:Integer}` | `:mode` | |
| `Tuple` | `:block`, `:depth`, `:mode`, `:tab_size` | `:delim`, `:gap` |
| `Typst` | | |
| `TypstString` | | |
| `TypstText` | | |
| `Unsigned` | `:mode` | |
| `VersionNumber` | `:mode` | |
| `Docs.HTML` | `:block`, `:depth`, `:mode`, `:tab_size` | |
| `Docs.Text` | `:mode` | |
| `Dates.Date` | `:mode`, `:indent` | |
| `Dates.DateTime` | `:mode`, `:indent` | |
| `Dates.Period` | `:mode`, `:indent` | |
| `Dates.Time` | `:mode`, `:indent` | |
""" show_typst

end # ShowTypst
26 changes: 16 additions & 10 deletions src/strings/Strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module Strings
import Base: show
using Dates: Date, DateTime, Day, Period, Time
using ..Typstry: Utilities
using .Utilities: ContextErrors.unwrap
using .Utilities: ContextErrors
using .ContextErrors: unwrap
using .Docs: HTML, Text
using .Meta: isexpr, parse
using PrecompileTools: @compile_workload
Expand Down Expand Up @@ -86,6 +87,15 @@ struct TypstText{T}
value::T
end

include("TypstContexts.jl")
using .TypstContexts: TypstContext, context

include("TypstStrings.jl")
using .TypstStrings: TypstString

include("ShowTypst.jl")
using .ShowTypst: _show_typst

"""
@typst_str("s")
typst"s"
Expand Down Expand Up @@ -150,15 +160,6 @@ macro typst_str(s::String)
:(TypstString(TypstText($_s)))
end

include("TypstContexts.jl")
using .TypstContexts: TypstContext

include("TypstStrings.jl")
using .TypstStrings: TypstString

include("ShowTypst.jl")
using .ShowTypst: _show_typst

# `Typstry`

@doc """
Expand Down Expand Up @@ -262,4 +263,9 @@ const examples = [
Time(0) => Time
]

get!(context.context, :preamble, TypstString(TypstText("""
#set page(margin: 1em, height: auto, width: auto, fill: white)
#set text(16pt, font: "JuliaMono")
""")))

end # Strings
Loading

0 comments on commit c02e14b

Please sign in to comment.