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
pub mod parser;
pub mod visitor;

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

use crate::raw::CodeAttr;
use linked_hash_map::LinkedHashMap;
use std::collections::HashMap;

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Ast(pub Vec<Block>);

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

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

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

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Inline {
    /// Plain text
    Text(String),
    Styled(Vec<Inline>, Style),
    /// Inline code
    Code(String),
    /// 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, String, String, Vec<Inline>),
    /// An inline link (usually originates from a markdown link spec)
    Link(LinkType, String, String, Vec<Inline>),
    /// Unescaped html.
    Html(String),
    /// 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<String>,
    pub value: Value,
    pub pos: PosInfo,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct Reference {
    pub obj_type: String,
    pub attr: LinkedHashMap<String, String>,
    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(String),
    Content(Vec<Block>),
    String(String),
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Block {
    Heading {
        lvl: u8,
        id: Option<String>,
        classes: Vec<String>,
        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,
}