Strings
This reference documents the Julia to Typst interface, a custom string corresponding to Typst source text, and its implementation of the String
interface.
Typstry
Typstry.Strings.ContextError
— TypeContextError <: Exception
ContextError(::Type, ::Type, ::Symbol)
An Exception
indicating that a context
key returned a value of an incorrect type.
Examples
julia> ContextError(Mode, String, :mode)
ContextError(Mode, String, :mode)
Typstry.Strings.Mode
— TypeMode
An Enum
erated type used to specify that the current Typst syntactical context is code
, markup
, or math
.
Examples
julia> Mode
Enum Mode:
code = 0
markup = 1
math = 2
Typstry.Strings.Typst
— TypeTypst{T}
Typst(::T)
A wrapper used to pass values to show(::IO, ::MIME"text/typst", ::Typst)
.
Examples
julia> Typst(1)
Typst{Int64}(1)
julia> Typst("a")
Typst{String}("a")
Typstry.Strings.TypstString
— TypeTypstString <: AbstractString
TypstString(::Any; context...)
Format the value as a Typst formatted string.
Optional Julia settings and Typst parameters are passed to show(::IO, ::MIME"text/typst", ::Typst)
in an IOContext
. See also show_typst
for a list of supported types.
This type implements the String
interface. However, the interface is undocumented, which may result in unexpected behavior.
Examples
julia> TypstString(1)
typst"$1$"
julia> TypstString(1 + 2im; mode = math)
typst"(1 + 2i)"
Typstry.Strings.TypstText
— TypeTypstText{T}
TypstText(::Any)
A wrapper whose show_typst
method uses print
.
Examples
julia> TypstText(1)
TypstText{Int64}(1)
julia> TypstText("a")
TypstText{String}("a")
Typstry.Strings.@typst_str
— Macro@typst_str("s")
typst"s"
Construct a TypstString
.
Control characters are escaped, except double quotation marks and backslashes in the same manner as @raw_str
. Values may be interpolated by calling the TypstString
constructor, except using a backslash instead of the type name. Interpolation syntax may be escaped in the same manner as quotation marks.
Print directly to an IO
using show(::IO, ::MIME"text/typst", ::Typst)
.
See also the performance tip to Avoid string interpolation for I/O.
Examples
julia> x = 1;
julia> typst"$ \(x; mode = math) / \(x + 1; mode = math) $"
typst"$ 1 / 2 $"
julia> typst"\(x//2)"
typst"$1 / 2$"
julia> typst"\(x // 2; mode = math)"
typst"(1 / 2)"
julia> typst"\\(x)"
typst"\\(x)"
Typstry.Strings.code
— ConstantTypstry.Strings.markup
— Constantmarkup
A Typst syntactical Mode
at the top-level of source text and enclosed within square brackets.
julia> markup
markup::Mode = 1
Typstry.Strings.math
— ConstantTypstry.Strings.context
— Functioncontext(x)
Provide formatting data for show(::IO, ::MIME"text/typst", ::Typst)
.
Implement this function for a custom type to specify its custom settings and parameters. Passing a value wrapped in Typst
will merge!
its custom context with defaults, such that the defaults may be overwritten. To be compatible with merging contexts and constructing an IOContext
, methods must return an AbstractDict{Symbol}
.
Setting | Default | Type | Description |
---|---|---|---|
:backticks | 3 | Int | The number of backticks to enclose raw text markup, which may be increased to disambiguiate nested raw text. |
:block | false | Bool | When :mode => math , specifies whether the enclosing dollar signs are padded with a space to render the element inline or its own block. |
:depth | 0 | Int | The current level of nesting within container types to specify the degree of indentation. |
:mode | markup | Mode | The current Typst syntactical context where code follows the number sign, markup is at the top-level and enclosed in square brackets, and math is enclosed in dollar signs. |
:parenthesize | true | Bool | Whether to enclose some mathematical elements in parentheses to specify their operator precedence and avoid ambiguity. |
:tab_size | 2 | Int | The number of spaces used by some elements with multi-line Typst formatting, which is repeated for each level of depth |
Typstry.Strings.show_typst
— Methodshow_typst(io, 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
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
and the Typst Documentation, respectively.
Some types, particularly containers, may call show(::IO, ::MIME"text/typst", ::Typst)
to format a value, which may use additional settings and parameters.
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, <:Integer} | :mode | |
Rational | :block , :mode , :parenthesize | |
Regex | :mode | |
Signed | :mode | |
StepRangeLen{<:Integer, <:Integer, <:Integer} | :mode | |
Tuple | :block , :depth , :mode , :tab_size | :delim , :gap |
Typst | ||
TypstString | ||
TypstText | :mode | |
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 |
Base
Base.IOBuffer
— TypeIOBuffer(::TypstString)
See also TypstString
.
Examples
julia> IOBuffer(typst"a")
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=1, maxsize=Inf, ptr=1, mark=-1)
Base.codeunit
— Functioncodeunit(::TypstString)
codeunit(::TypstString, ::Integer)
See also TypstString
.
Examples
julia> codeunit(typst"a")
UInt8
julia> codeunit(typst"a", 1)
0x61
Base.isvalid
— FunctionBase.iterate
— Methoditerate(::TypstString)
iterate(::TypstString, ::Integer)
See also TypstString
.
Examples
julia> iterate(typst"a")
('a', 2)
julia> iterate(typst"a", 1)
('a', 2)
Base.ncodeunits
— FunctionBase.pointer
— Functionpointer(::TypstString)
See also TypstString
.
Base.repr
— Functionrepr(::MIME, ::TypstString; kwargs...)
See also TypstString
.
This method patches incorrect output from the assumption in repr
that the parameter is already in the requested MIME
type when the MIME
type satisfies istextmime
and the parameter is an AbstractString
.
Examples
julia> repr("text/plain", typst"a")
"typst\"a\""
julia> repr("text/typst", typst"a")
typst"a"
Base.show
— MethodBase.show
— Methodshow(::IO, ::MIME"text/plain", ::ContextError)
Examples
julia> show(stdout, "text/plain", ContextError(Mode, String, :mode))
ContextError(Mode, String, :mode)
Base.show
— Methodshow(::IO, ::MIME"text/typst", ::Union{Typst, TypstString, TypstText})
Print in Typst format.
This method provides formatting data to show_typst
specified by a default and custom context
.
See also Typst
, TypstString
, and TypstText
.
Examples
julia> show(stdout, "text/typst", typst"a")
a
julia> show(stdout, "text/typst", Typst("a"))
"a"
julia> show(stdout, "text/typst", Typst(Text("a")))
#"a"
Base.showerror
— Methodshowerror(::IO, ::ContextError)
Examples
julia> showerror(stdout, ContextError(Mode, String, :mode))
ContextError: the `context` key `:mode` expected a value of type `Mode` but received `String`