Skip to content

Commit

Permalink
Use mutation rather than Preferences.jl to customize the context
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobjpeters committed Nov 9, 2024
1 parent 6527044 commit b690155
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 61 deletions.
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Artifacts = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Typst_jll = "eb4b1da6-20f6-5c66-9826-fdb8ad410d0e"

[weakdeps]
Expand All @@ -22,6 +21,5 @@ MarkdownExtension = "Markdown"
[compat]
LaTeXStrings = "1.1"
PrecompileTools = "1.0.1"
Preferences = "1.0"
Typst_jll = "0.7, 0.8 - 0.12"
julia = "1.10"
2 changes: 1 addition & 1 deletion docs/source/guides/package_interoperability.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ IJulia.jl, Pluto.jl, and QuartoNotebookRunner.jl each display
`show` with the `application/pdf`, `image/png`, and `image/svg+xml` `MIME` types.

!!! tip
Use [`set_context`](@ref) to customize the default formatting in these environments.
Set mappings in [`context`](@ref) to customize the default formatting in these environments.

## Typst Packages

Expand Down
2 changes: 1 addition & 1 deletion docs/source/references/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ math
ContextError
TypstContext
context
set_context
reset_context
```
8 changes: 4 additions & 4 deletions src/Typstry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
module Typstry

import Base:
IOBuffer, ==, addenv, codeunit, detach, eltype, firstindex, getindex, get, hash, ignorestatus, isvalid, iterate,
keys, lastindex, length, ncodeunits, pointer, repr, run, setcpuaffinity, setenv, showerror, show
IOBuffer, ==, addenv, codeunit, copy, detach, eltype, firstindex, getindex, getkey, get,
hash, ignorestatus, isvalid, iterate, keys, lastindex, length, mergewith, merge!, merge,
ncodeunits, pointer, repr, run, setcpuaffinity, setenv, setindex!, showerror, show, sizehint!
import Typst_jll
using Artifacts: @artifact_str
using Dates:
Expand All @@ -13,7 +14,6 @@ using .Docs: HTML, Text
using .Iterators: Stateful
using .Meta: isexpr, parse
using PrecompileTools: @compile_workload
using Preferences: @load_preference, @set_preferences!

include("utilities/utilities.jl")
include("strings/strings.jl")
Expand All @@ -23,7 +23,7 @@ export
ContextError, Mode, TypstCommandError, TypstCommand,
TypstContext, TypstString, TypstText, Typst,
@typst_cmd, @typst_str, code, context, julia_mono,
markup, math, render, set_context, show_typst, typst
markup, math, render, reset_context, show_typst, typst

"""
examples
Expand Down
15 changes: 11 additions & 4 deletions src/strings/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ base_type(::Typst) = Typst
base_type(x)
""" base_type

get!(context.context, :preamble, typst"""
#set page(margin: 1em, height: auto, width: auto, fill: white)
#set text(16pt, font: "JuliaMono")
""")
merge!(default_context, TypstContext(;
backticks = 3,
block = false,
depth = 0,
mode = markup,
parenthesize = true,
preamble = TypstString(TypstText(
"#set page(margin: 1em, height: auto, width: auto, fill: white)\n#set text(16pt, font: \"JuliaMono\")\n")),
tab_size = 2
))
reset_context()

for (key, value) in pairs(context)
@eval begin
Expand Down
109 changes: 60 additions & 49 deletions src/utilities/typst_context.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ Calls to [`show_typst`](@ref) from the following methods:
specify the [`TypstContext`](@ref) by combining the following contexts:
1. A default [`context`](@ref)
2. The context specified by [`set_context`](@ref),
which is combined with the `context` upon initialization
3. The `TypstContext` constructor implemented for the given type,
the context specified in the call by keyword parameters,
1. The [`context`](@ref)
2. Any context specified by implementing the `TypstContext` constructor for the given type
3. The context specified in the call by keyword parameters,
a given `TypstContext`, or the `IOContext` key `:typst_context`,
depending on the calling method
4. Any context specified by a recursive call in `show_typst` to format values,
Expand All @@ -32,18 +30,26 @@ Duplicate keys are handled such that each successive context overwrites
those of previous contexts, prioritized in order as listed.
In other words, the default `context` has the lowest priority while
recursive calls to `show_typst` have the highest priority.
# Interfaces
This type implements the dictionary and iteration interfaces.
However, it is immutable such that it does not support inserting, deleting, or setting a key-value pair.
However, it does not support removing mappings.
- `copy(::TypstContext)`
- `eltype(::TypstContext)`
- `get(::TypstContext,\u00A0::Symbol,\u00A0default)`
- `get(::Union{Function, Type},\u00A0::TypstContext,\u00A0::Symbol)`
- `getkey(::TypstContext, ::Any, ::Any)`
- `get(::TypstContext,\u00A0::Any,\u00A0::Any)`
- `get(::Union{Function, Type},\u00A0::TypstContext,\u00A0::Any)`
- `iterate(::TypstContext,\u00A0state)`
- `iterate(::TypstContext)`
- `length(::TypstContext)`
- `mergewith(::Any, ::TypstContext, ::AbstractDict...)`
- `merge!(::TypstContext, ::AbstractDict...)`
- `merge(::TypstContext, ::AbstractDict...)`
- `setindex!(::TypstContext, ::Any, ::Any)`
- `show(::IO,\u00A0::TypstContext)`
- `sizehint!(::TypstContext, ::Any)`
"""
struct TypstContext <: AbstractDict{Symbol, Any}
context::Dict{Symbol, Any}
Expand All @@ -53,16 +59,28 @@ end

TypstContext(_) = TypstContext()

copy(tc::TypstContext) = merge!(TypstContext(), tc)

eltype(tc::TypstContext) = eltype(tc.context)

get(tc::TypstContext, key::Symbol, default) = get(tc.context, key, default)
get(f::Union{Function, Type}, tc::TypstContext, key::Symbol) = get(f, tc.context, key)
getkey(tc::TypstContext, key, default) = getkey(tc.context, key, default)

get(tc::TypstContext, key, default) = get(tc.context, key, default)
get(f::Union{Function, Type}, tc::TypstContext, key) = get(f, tc.context, key)

iterate(tc::TypstContext, state) = iterate(tc.context, state)
iterate(tc::TypstContext) = iterate(tc.context)

length(tc::TypstContext) = length(tc.context)

mergewith(combine, tc::TypstContext, ds::AbstractDict...) = mergewith!(combine, copy(tc), ds)

merge!(tc::TypstContext, ds::AbstractDict...) = (merge!(tc.context, ds...); tc)

merge(tc::TypstContext, ds::AbstractDict...) = merge!(copy(tc), ds...)

setindex!(tc::TypstContext, value, key) = (tc.context[key] = value; tc)

function show(io::IO, tc::TypstContext)
print(io, TypstContext, "(")
if !isempty(tc)
Expand All @@ -75,29 +93,25 @@ function show(io::IO, tc::TypstContext)
print(io, ")")
end

"""
default_context
"""
const default_context = TypstContext(;
backticks = 3,
block = false,
depth = 0,
mode = markup,
parenthesize = true,
tab_size = 2
)
sizehint!(tc::TypstContext, n) = sizehint!(tc.context, n)

"""
merge_contexts!(tc, context)
default_context
"""
merge_contexts!(tc, context) = mergewith!((x, _) -> x, tc.context, context)
const default_context = TypstContext()

"""
context
A `const`ant [`TypstContext`](@ref) used default formatting data when calling [`show_typst`](@ref).
A `const`ant [`TypstContext`](@ref) used to provide
default formatting data when calling [`show_typst`](@ref).
See also [`reset_context`](@ref).
May be configured using [`set_context`](@ref).
!!! tip
Set mappings in this dictionary to customize the default formatting
in environments that display values using `show` with the
`application/pdf`, `image/png`, and `image/svg+xml` `MIME` types.
| Setting | Type | Description |
|:---------------|:----------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
Expand All @@ -123,33 +137,30 @@ TypstContext with 7 entries:
:depth => 0
```
"""
const context = let
tc = @load_preference "context" TypstContext()
merge_contexts!(tc, default_context)
tc
end

function _set_context(value)
@set_preferences! "context" value
@info "Restart Julia to reinitialize the `context`"
end

set_context(tc::TypstContext) = _set_context(tc)
set_context() = _set_context(nothing)
const context = TypstContext()

"""
set_context(::TypstContext)
set_context()
reset_context()
Use Preferences.jl such that after restarting Julia,
the [`context`](@ref) is initialized to the given
[`TypstContext`](@ref) merged with default settings.
Remove any custom mappings from the [`context`](@ref)
such that it is returned to its default state.
Specifying a key contained in the default settings will override it.
If a `TypstContext` is not provided, the `context` is reset to the default settings.
# Examples
!!! tip
Use this function to customize the default formatting in environments that display values using
`show` with the `application/pdf`, `image/png`, and `image/svg+xml` `MIME` types.
```jldoctest
julia> reset_context()
TypstContext with 7 entries:
:mode => markup
:parenthesize => true
:block => false
:preamble => TypstString(TypstText("#set page(margin: 1em, height: auto, …
:tab_size => 2
:backticks => 3
:depth => 0
```
"""
set_context
function reset_context()
_context = context.context
merge!(empty!(_context), default_context)
context
end
5 changes: 5 additions & 0 deletions src/utilities/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ math_pad(tc) =
else block(tc) ? "\$ " : "\$"
end

"""
merge_contexts!(tc, context)
"""
merge_contexts!(tc, context) = mergewith!((x, _) -> x, tc.context, context)

"""
show_array(io, x)
"""
Expand Down

0 comments on commit b690155

Please sign in to comment.