pulldown_typst/
lib.rs

1use std::num::NonZeroU8;
2pub mod markup;
3// TODO: remove this.
4use pulldown_cmark::CowStr;
5
6#[derive(Debug, PartialEq, Clone)]
7pub enum Event<'a> {
8    /// Start of a tagged element. Events that are yielded after this event
9    /// and before its corresponding `End` event are inside this element.
10    /// Start and end events are guaranteed to be balanced.
11    Start(Tag<'a>),
12    /// End of a tagged element.
13    End(Tag<'a>),
14    /// A text node.
15    Text(CowStr<'a>),
16    /// An inline code node.
17    Code(CowStr<'a>),
18    /// A soft line break.
19    Linebreak,
20    /// A hard line break.
21    Parbreak,
22    /// A page break.
23    PageBreak,
24    /// A line. The first field is the start point, the second is the end point, the
25    /// third field is the length, the fourth is the angle, and the fifth is the stroke.
26    ///
27    /// See <https://typst.app/docs/reference/visualize/line/>.
28    // TODO: make this strongly typed.
29    Line(
30        // start
31        Option<(CowStr<'a>, CowStr<'a>)>,
32        // end
33        Option<(CowStr<'a>, CowStr<'a>)>,
34        // length
35        Option<CowStr<'a>>,
36        // angle
37        Option<CowStr<'a>>,
38        // stroke
39        Option<CowStr<'a>>,
40    ),
41    /// A let binding. First argument is lhs, second is rhs.
42    ///
43    /// See <https://typst.app/docs/reference/scripting/#bindings>.
44    Let(CowStr<'a>, CowStr<'a>),
45    /// A function call. The first field is the target variable (without `#`), the
46    /// second is the function name, and the third is a list of arguments.
47    ///
48    /// If calling `document()`, prefer [`DocumentFunctionCall`].
49    // TODO: make this strongly typed.
50    FunctionCall(Option<CowStr<'a>>, CowStr<'a>, Vec<CowStr<'a>>),
51    /// A `document` function call. The field is the list of arguments.
52    ///
53    /// Prefer this over the more general `FunctionCall` as document calls must appear
54    /// before any content.
55    ///
56    /// See <https://typst.app/docs/reference/meta/document>.
57    // TODO: make this strongly typed.
58    DocumentFunctionCall(Vec<CowStr<'a>>),
59    /// A set rule.
60    ///
61    /// If setting document metadata, prefer [`DocumentSet`].
62    ///
63    /// See <https://typst.app/docs/reference/styling/#set-rules>.
64    // TODO: make this a tag.
65    Set(CowStr<'a>, CowStr<'a>, CowStr<'a>),
66    /// A `document` set rule. The first field is the parameter name, the second is the
67    /// parameter value.
68    ///
69    /// Prefer this over the more general [`Set`] as document set rules must appear
70    /// before any content.
71    ///
72    /// See <https://typst.app/docs/reference/meta/document>.
73    DocumentSet(CowStr<'a>, CowStr<'a>),
74
75    /// Raw string data what will be bassed through directly to typst. Prefer using
76    /// other strongly-typed rules.
77    Raw(CowStr<'a>),
78}
79
80/// Tags for elements that can contain other elements.
81#[derive(Clone, Debug, PartialEq)]
82pub enum Tag<'a> {
83    /// A paragraph of text and other inline elements.
84    Paragraph,
85
86    /// A show rule.
87    ///
88    /// See <https://typst.app/docs/reference/styling/#show-rules>.
89    Show(
90        ShowType,
91        CowStr<'a>,
92        Option<(CowStr<'a>, CowStr<'a>, CowStr<'a>)>,
93        Option<CowStr<'a>>,
94    ),
95
96    /// A heading. The first field indicates the level of the heading, the second if it
97    /// should be included in outline, and the third if it should be included in
98    /// bookmarks.
99    Heading(NonZeroU8, TableOfContents, Bookmarks),
100
101    /// A code block. The first argument is the
102    /// fenced value if it exists, the second is how it should be displayed.
103    CodeBlock(Option<CowStr<'a>>, CodeBlockDisplay),
104
105    /// A bullted list. The first field indicates the marker to use, the second is if
106    /// tight is desired. Contains only list items.
107    BulletList(Option<&'a str>, bool),
108    /// A numbered / enumerated list (also called an _enum_ by typst). The first field
109    /// indicates the starting number, the second is the [numbering
110    /// pattern](https://typst.app/docs/reference/meta/numbering/), the third is if
111    /// tight is desired. Contains only list items.
112    ///
113    /// See <https://typst.app/docs/reference/layout/enum/>.
114    NumberedList(u64, Option<NumberingPattern<'a>>, bool),
115    /// A list item.
116    Item,
117    /// A quote.
118    /// The second argument determines if it should be wrapped in quotes.
119    /// The third argument is the attribution value if it exists.
120    ///
121    /// See <https://typst.app/docs/reference/model/quote/>.
122    Quote(QuoteType, QuoteQuotes, Option<CowStr<'a>>),
123    // Span-level tags
124    Emphasis,
125    Strong,
126    Strikethrough,
127
128    /// A link. The first field is the type and the second is the destination URL.
129    Link(LinkType, CowStr<'a>),
130
131    /// A table. The first field is the alignment of each column.
132    Table(Vec<TableCellAlignment>),
133    /// A table header row. Must come after a #[Tag::Table].
134    TableHead,
135    /// A table row. Must come after a #[Tag::Table].
136    TableRow,
137    /// A table row. Must come after a #[Tag::TableRow].
138    TableCell,
139}
140
141/// How to display a code block.
142#[derive(Clone, Debug, PartialEq)]
143pub enum CodeBlockDisplay {
144    Block,
145    Inline,
146}
147
148/// Item appearance in bookmarks.
149#[derive(Clone, Debug, PartialEq)]
150pub enum Bookmarks {
151    Include,
152    Exclude,
153}
154
155/// Item appearance in the table of contents.
156#[derive(Clone, Debug, PartialEq)]
157pub enum TableOfContents {
158    Include,
159    Exclude,
160}
161
162/// The pattern to use whren numbering items.
163///
164/// See <https://typst.app/docs/reference/meta/numbering/>.
165#[derive(Clone, Debug, PartialEq)]
166pub struct NumberingPattern<'a>(&'a str);
167
168/// Type specifier for Show rules. See [Tag::Show](enum.Tag.html#variant.Show) for
169/// more information.
170// TODO: support different dests.
171#[derive(Clone, Debug, PartialEq, Copy)]
172pub enum ShowType {
173    ShowSet,
174    Function,
175}
176
177/// Type specifier for inline links. See [Tag::Link](enum.Tag.html#variant.Link) for
178/// more information.
179#[derive(Clone, Debug, PartialEq, Copy)]
180pub enum LinkType {
181    /// Link like `#link("https://example.com")`
182    Url,
183    /// Link like `#link("https://example.com")[my cool content]`
184    Content,
185    /// Autolink like `http://foo.bar/baz`.
186    Autolink,
187}
188
189/// Type specifier for a quote.
190#[derive(Clone, Debug, PartialEq, Copy)]
191pub enum QuoteType {
192    Block,
193    Inline,
194}
195
196/// Include a quote in quotes.
197/// See <https://typst.app/docs/reference/model/quote/#parameters-quotes>
198#[derive(Clone, Debug, PartialEq, Copy)]
199pub enum QuoteQuotes {
200    WrapInDoubleQuotes,
201    DoNotWrapInDoubleQuotes,
202    Auto,
203}
204
205/// Alignment of a table cell.
206#[derive(Clone, Debug, PartialEq, Copy)]
207pub enum TableCellAlignment {
208    Left,
209    Center,
210    Right,
211    None,
212}