cdoc_parser/ast/
mod.rs

1pub mod parser;
2pub mod visitor;
3
4use crate::code_ast::types::CodeContent;
5use crate::common::Span;
6use cowstr::CowStr;
7use pulldown_cmark::LinkType;
8use serde::{Deserialize, Serialize};
9
10use linked_hash_map::LinkedHashMap;
11
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct Ast {
14    pub blocks: Vec<Block>,
15    pub source: CowStr,
16}
17
18#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
19pub struct Command {
20    pub function: CowStr,
21    pub label: Option<CowStr>,
22    pub parameters: Vec<Parameter>,
23    pub body: Option<Vec<Block>>,
24    pub span: Span,
25    pub global_idx: usize,
26}
27
28#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
29pub struct CodeBlock {
30    /// Label
31    pub label: Option<CowStr>,
32    /// Code source
33    pub source: CodeContent,
34    /// Code tags
35    pub attributes: Vec<CowStr>,
36    /// Display the block as a cell or listing (only used for notebooks)
37    pub display_cell: bool,
38    pub global_idx: usize,
39    pub span: Span,
40}
41
42#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
43pub struct Math {
44    pub source: CowStr,
45    pub label: Option<CowStr>,
46    pub display_block: bool,
47    pub span: Span,
48}
49
50#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
51pub enum Inline {
52    /// Plain text
53    Text(CowStr),
54    Styled(Vec<Inline>, Style),
55    /// Inline code
56    Code(CowStr),
57    /// A code block. May originate from markdown fenced code blocks or notebook code cells.
58    CodeBlock(CodeBlock),
59    SoftBreak,
60    HardBreak,
61    /// Horizontal rule
62    Rule,
63    /// An inline image (usually originates from a markdown image spec)
64    Image(LinkType, CowStr, CowStr, Vec<Inline>),
65    /// An inline link (usually originates from a markdown link spec)
66    Link(LinkType, CowStr, CowStr, Vec<Inline>),
67    /// Unescaped html.
68    Html(CowStr),
69    /// Math element (may be inline or display)
70    /// The trailing space element is necessary due to the way parsing currently works with
71    /// pulldown_cmark.
72    Math(Math),
73    Command(Command),
74}
75
76#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
77pub struct Parameter {
78    pub key: Option<CowStr>,
79    pub value: Value,
80    pub span: Span,
81}
82
83#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
84pub struct Reference {
85    pub obj_type: String,
86    pub attr: LinkedHashMap<CowStr, CowStr>,
87    pub num: usize,
88}
89
90// #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
91// #[serde(tag = "type", rename_all = "lowercase")]
92// pub enum Reference {
93//     Math {
94//         display_inline: bool,
95//     },
96//     Code {
97//         tags: Vec<CodeAttr>,
98//     },
99//     Command {
100//         function: String,
101//         parameters: HashMap<String, String>,
102//     },
103// }
104
105#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
106pub enum Value {
107    Flag(CowStr),
108    Content(Vec<Block>),
109    String(CowStr),
110}
111
112#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
113pub enum Block {
114    Heading {
115        lvl: u8,
116        id: Option<CowStr>,
117        classes: Vec<CowStr>,
118        inner: Vec<Inline>,
119    },
120    Plain(Vec<Inline>),
121    Paragraph(Vec<Inline>),
122    BlockQuote(Vec<Inline>),
123    /// A list - ordered or unordered.
124    List(Option<u64>, Vec<Block>),
125    ListItem(Vec<Block>),
126}
127
128#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
129pub struct CodeMeta {
130    pub id: String,
131    pub editable: bool,
132    pub folded: bool,
133    pub custom: LinkedHashMap<String, String>,
134}
135
136#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
137pub enum Style {
138    Emphasis,
139    Strong,
140    Strikethrough,
141    Underline,
142}