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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Canonical document stream types.
use serde::{Deserialize, Serialize};
use crate::{Manifest, McdPackage, markdown};
/// Parsed MCD document with a canonical block stream.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct McdDocument {
/// Source Markdown path inside the package.
pub source_path: String,
/// Blocks in source order.
pub blocks: Vec<DocumentBlock>,
}
impl McdDocument {
/// Parse the manifest entrypoint Markdown from a package.
pub fn from_package(package: &McdPackage, manifest: &Manifest) -> crate::Result<Self> {
let markdown = package.read_to_string(&manifest.entrypoint)?;
markdown::parse_markdown(&manifest.entrypoint, &markdown)
}
}
/// A canonical document block.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum DocumentBlock {
/// Markdown heading.
Heading {
/// Stable generated block id.
id: String,
/// Heading level.
level: u8,
/// Plain heading text.
text: String,
/// Source span when available.
#[serde(skip_serializing_if = "Option::is_none")]
source: Option<SourceSpan>,
/// Inline annotation markers in this block.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
annotations: Vec<AnnotationRef>,
},
/// Markdown paragraph.
Paragraph {
/// Stable generated block id.
id: String,
/// Plain paragraph text.
text: String,
/// Source span when available.
#[serde(skip_serializing_if = "Option::is_none")]
source: Option<SourceSpan>,
/// Inline annotation markers in this block.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
annotations: Vec<AnnotationRef>,
},
/// Markdown list.
List {
/// Stable generated block id.
id: String,
/// Plain list text.
text: String,
/// Source span when available.
#[serde(skip_serializing_if = "Option::is_none")]
source: Option<SourceSpan>,
/// Inline annotation markers in this block.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
annotations: Vec<AnnotationRef>,
},
/// Markdown code block.
CodeBlock {
/// Stable generated block id.
id: String,
/// Optional language/info string.
#[serde(skip_serializing_if = "Option::is_none")]
language: Option<String>,
/// Literal code text.
text: String,
/// Source span when available.
#[serde(skip_serializing_if = "Option::is_none")]
source: Option<SourceSpan>,
/// Inline annotation markers in this block.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
annotations: Vec<AnnotationRef>,
},
/// Markdown block quote.
Quote {
/// Stable generated block id.
id: String,
/// Plain quote text.
text: String,
/// Source span when available.
#[serde(skip_serializing_if = "Option::is_none")]
source: Option<SourceSpan>,
/// Inline annotation markers in this block.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
annotations: Vec<AnnotationRef>,
},
/// Markdown display math block.
MathBlock {
/// Stable generated block id.
id: String,
/// Literal math text.
text: String,
/// Source span when available.
#[serde(skip_serializing_if = "Option::is_none")]
source: Option<SourceSpan>,
},
/// MCD table placement.
TableRef {
/// Stable generated block id.
id: String,
/// Parsed table placement.
placement: crate::directives::TablePlacement,
/// Source span when available.
#[serde(skip_serializing_if = "Option::is_none")]
source: Option<SourceSpan>,
},
/// MCD image placement.
ImageRef {
/// Stable generated block id.
id: String,
/// Parsed image placement.
placement: crate::directives::ImagePlacement,
/// Source span when available.
#[serde(skip_serializing_if = "Option::is_none")]
source: Option<SourceSpan>,
},
}
impl DocumentBlock {
/// Return the stable block id.
#[must_use]
pub fn id(&self) -> &str {
match self {
Self::Heading { id, .. }
| Self::Paragraph { id, .. }
| Self::List { id, .. }
| Self::CodeBlock { id, .. }
| Self::Quote { id, .. }
| Self::MathBlock { id, .. }
| Self::TableRef { id, .. }
| Self::ImageRef { id, .. } => id,
}
}
/// Return annotation refs attached to the block.
#[must_use]
pub fn annotation_refs(&self) -> &[AnnotationRef] {
match self {
Self::Heading { annotations, .. }
| Self::Paragraph { annotations, .. }
| Self::List { annotations, .. }
| Self::Quote { annotations, .. } => annotations,
Self::TableRef { placement, .. } => &placement.annotations,
Self::ImageRef { placement, .. } => &placement.annotations,
Self::CodeBlock { .. } | Self::MathBlock { .. } => &[],
}
}
}
/// Annotation marker embedded in Markdown and resolved to sidecar metadata.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AnnotationRef {
/// Referenced annotation id.
pub id: String,
/// UTF-8 byte offset in the cleaned block text, when the marker is inline.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub text_offset: Option<usize>,
}
/// 1-based source span in the Markdown entrypoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceSpan {
/// First line.
pub start_line: usize,
/// First column.
pub start_column: usize,
/// Last line.
pub end_line: usize,
/// Last column.
pub end_column: usize,
}
impl std::fmt::Display for SourceSpan {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}:{}-{}:{}",
self.start_line, self.start_column, self.end_line, self.end_column
)
}
}