diff --git a/Project.toml b/Project.toml index 3a58344..94f230b 100644 --- a/Project.toml +++ b/Project.toml @@ -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] @@ -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" diff --git a/docs/source/guides/package_interoperability.md b/docs/source/guides/package_interoperability.md index bd31812..3cd8ab9 100644 --- a/docs/source/guides/package_interoperability.md +++ b/docs/source/guides/package_interoperability.md @@ -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 diff --git a/docs/source/references/utilities.md b/docs/source/references/utilities.md index 63ab75a..b096468 100644 --- a/docs/source/references/utilities.md +++ b/docs/source/references/utilities.md @@ -9,5 +9,5 @@ math ContextError TypstContext context -set_context +reset_context ``` diff --git a/src/Typstry.jl b/src/Typstry.jl index 7912880..ae15d6f 100644 --- a/src/Typstry.jl +++ b/src/Typstry.jl @@ -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: @@ -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") @@ -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 diff --git a/src/strings/strings.jl b/src/strings/strings.jl index 0a5736d..84cd197 100644 --- a/src/strings/strings.jl +++ b/src/strings/strings.jl @@ -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 diff --git a/src/utilities/typst_context.jl b/src/utilities/typst_context.jl index bc3d792..ff09cc9 100644 --- a/src/utilities/typst_context.jl +++ b/src/utilities/typst_context.jl @@ -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, @@ -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} @@ -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) @@ -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 | |:---------------|:----------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| @@ -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 diff --git a/src/utilities/utilities.jl b/src/utilities/utilities.jl index 39385e0..0c7b6a6 100644 --- a/src/utilities/utilities.jl +++ b/src/utilities/utilities.jl @@ -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) """