1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
pub mod parser;
pub mod visitor;

use crate::code_ast::types::CodeContent;
use crate::common::Span;
use cowstr::CowStr;
use pulldown_cmark::LinkType;
use serde::{Deserialize, Serialize};

use linked_hash_map::LinkedHashMap;

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Ast {
    pub blocks: Vec<Block>,
    pub source: CowStr,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Command {
    pub function: CowStr,
    pub label: Option<CowStr>,
    pub parameters: Vec<Parameter>,
    pub body: Option<Vec<Block>>,
    pub span: Span,
    pub global_idx: usize,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CodeBlock {
    /// Label
    pub label: Option<CowStr>,
    /// Code source
    pub source: CodeContent,
    /// Code tags
    pub attributes: Vec<CowStr>,
    /// Display the block as a cell or listing (only used for notebooks)
    pub display_cell: bool,
    pub global_idx: usize,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Math {
    pub source: CowStr,
    pub label: Option<CowStr>,
    pub display_block: bool,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Inline {
    /// Plain text
    Text(CowStr),
    Styled(Vec<Inline>, Style),
    /// Inline code
    Code(CowStr),
    /// A code block. May originate from markdown fenced code blocks or notebook code cells.
    CodeBlock(CodeBlock),
    SoftBreak,
    HardBreak,
    /// Horizontal rule
    Rule,
    /// An inline image (usually originates from a markdown image spec)
    Image(LinkType, CowStr, CowStr, Vec<Inline>),
    /// An inline link (usually originates from a markdown link spec)
    Link(LinkType, CowStr, CowStr, Vec<Inline>),
    /// Unescaped html.
    Html(CowStr),
    /// Math element (may be inline or display)
    /// The trailing space element is necessary due to the way parsing currently works with
    /// pulldown_cmark.
    Math(Math),
    Command(Command),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Parameter {
    pub key: Option<CowStr>,
    pub value: Value,
    pub span: Span,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct Reference {
    pub obj_type: String,
    pub attr: LinkedHashMap<CowStr, CowStr>,
    pub num: usize,
}

// #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
// #[serde(tag = "type", rename_all = "lowercase")]
// pub enum Reference {
//     Math {
//         display_inline: bool,
//     },
//     Code {
//         tags: Vec<CodeAttr>,
//     },
//     Command {
//         function: String,
//         parameters: HashMap<String, String>,
//     },
// }

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Value {
    Flag(CowStr),
    Content(Vec<Block>),
    String(CowStr),
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Block {
    Heading {
        lvl: u8,
        id: Option<CowStr>,
        classes: Vec<CowStr>,
        inner: Vec<Inline>,
    },
    Plain(Vec<Inline>),
    Paragraph(Vec<Inline>),
    BlockQuote(Vec<Inline>),
    /// A list - ordered or unordered.
    List(Option<u64>, Vec<Block>),
    ListItem(Vec<Block>),
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct CodeMeta {
    pub id: String,
    pub editable: bool,
    pub folded: bool,
    pub custom: LinkedHashMap<String, String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Style {
    Emphasis,
    Strong,
    Strikethrough,
    Underline,
}