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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use serde::Deserialize;
use std::collections::BTreeMap;
#[derive(Clone, Debug, PartialEq)]
pub enum ArgValue {
Ident(String),
Int(i64),
Str(String),
Array(Vec<ArgValue>),
}
impl ArgValue {
pub fn type_name(&self) -> &'static str {
match self {
ArgValue::Ident(_) => "ident",
ArgValue::Int(_) => "int",
ArgValue::Str(_) => "string",
ArgValue::Array(_) => "array",
}
}
pub fn as_str(&self) -> Option<&str> {
match self {
ArgValue::Str(s) | ArgValue::Ident(s) => Some(s.as_str()),
_ => None,
}
}
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ArgType {
String,
Int,
Ident,
Array,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub struct ArgSpec {
#[serde(rename = "type")]
pub ty: ArgType,
#[serde(default)]
pub required: bool,
#[serde(default)]
pub position: Option<usize>,
#[serde(default)]
pub oneof: Option<Vec<String>>,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ShortKindOpt {
Inline,
Block,
Both,
}
impl Default for ShortKindOpt {
fn default() -> Self {
ShortKindOpt::Inline
}
}
#[derive(Clone, Debug, Deserialize, Default, PartialEq, Eq)]
pub struct Shortcode {
#[serde(default)]
pub kind: ShortKindOpt,
#[serde(default)]
pub arguments: BTreeMap<String, ArgSpec>,
#[serde(default)]
pub template_html: Option<String>,
#[serde(default)]
pub template_llm: Option<String>,
}
#[derive(Clone, Debug, Default)]
pub struct Registry {
pub map: BTreeMap<String, Shortcode>,
}
impl Registry {
pub fn with_builtins() -> Self {
let mut m = BTreeMap::new();
m.insert(
"link".into(),
Shortcode {
kind: ShortKindOpt::Inline,
arguments: {
let mut a = BTreeMap::new();
a.insert(
"url".into(),
ArgSpec {
ty: ArgType::String,
required: true,
position: Some(1),
oneof: None,
},
);
a.insert(
"title".into(),
ArgSpec {
ty: ArgType::String,
required: false,
position: None,
oneof: None,
},
);
a
},
template_html: None,
template_llm: None,
},
);
m.insert(
"image".into(),
Shortcode {
kind: ShortKindOpt::Inline,
arguments: {
let mut a = BTreeMap::new();
a.insert(
"src".into(),
ArgSpec {
ty: ArgType::String,
required: true,
position: None,
oneof: None,
},
);
a.insert(
"alt".into(),
ArgSpec {
ty: ArgType::String,
required: false,
position: None,
oneof: None,
},
);
a
},
..Default::default()
},
);
m.insert(
"kbd".into(),
Shortcode {
kind: ShortKindOpt::Inline,
..Default::default()
},
);
// Inline subscript and superscript: replacements for the only inline
// HTML constructs Brief still wants to express. Both take their text
// via the `[content]` body — no arguments.
m.insert(
"sub".into(),
Shortcode {
kind: ShortKindOpt::Inline,
..Default::default()
},
);
m.insert(
"sup".into(),
Shortcode {
kind: ShortKindOpt::Inline,
..Default::default()
},
);
// Collapsible block: `@details(summary: "...") ... @end` ↔
// `<details><summary>...</summary>...</details>`.
m.insert(
"details".into(),
Shortcode {
kind: ShortKindOpt::Block,
arguments: {
let mut a = BTreeMap::new();
a.insert(
"summary".into(),
ArgSpec {
ty: ArgType::String,
required: true,
position: None,
oneof: None,
},
);
a
},
..Default::default()
},
);
m.insert(
"dl".into(),
Shortcode {
kind: ShortKindOpt::Block,
..Default::default()
},
);
m.insert(
"t".into(),
Shortcode {
kind: ShortKindOpt::Block,
arguments: {
let mut a = BTreeMap::new();
a.insert(
"align".into(),
ArgSpec {
ty: ArgType::Array,
required: false,
position: None,
oneof: None,
},
);
a
},
..Default::default()
},
);
m.insert(
"code".into(),
Shortcode {
kind: ShortKindOpt::Block,
arguments: {
let mut a = BTreeMap::new();
a.insert(
"lang".into(),
ArgSpec {
ty: ArgType::String,
required: false,
position: Some(1),
oneof: None,
},
);
a
},
..Default::default()
},
);
m.insert(
"callout".into(),
Shortcode {
kind: ShortKindOpt::Block,
arguments: {
let mut a = BTreeMap::new();
a.insert(
"kind".into(),
ArgSpec {
ty: ArgType::String,
required: true,
position: None,
oneof: Some(vec![
"note".into(),
"tip".into(),
"important".into(),
"warning".into(),
"caution".into(),
]),
},
);
a
},
..Default::default()
},
);
m.insert(
"math".into(),
Shortcode {
kind: ShortKindOpt::Both,
..Default::default()
},
);
m.insert(
"footnote".into(),
Shortcode {
kind: ShortKindOpt::Inline,
..Default::default()
},
);
m.insert(
"ref".into(),
Shortcode {
kind: ShortKindOpt::Inline,
arguments: {
let mut a = BTreeMap::new();
a.insert(
"title".into(),
ArgSpec {
ty: ArgType::String,
required: true,
position: Some(1),
oneof: None,
},
);
a
},
template_html: None,
template_llm: None,
},
);
Registry { map: m }
}
pub fn get(&self, name: &str) -> Option<&Shortcode> {
self.map.get(name)
}
pub fn extend(&mut self, other: BTreeMap<String, Shortcode>) {
for (k, v) in other {
self.map.insert(k, v);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ref_is_a_builtin_inline_shortcode() {
let reg = Registry::with_builtins();
let sc = reg.get("ref").expect("@ref must be a built-in");
assert!(matches!(sc.kind, ShortKindOpt::Inline));
let title = sc.arguments.get("title").expect("title arg");
assert!(title.required);
assert_eq!(title.position, Some(1));
assert!(matches!(title.ty, ArgType::String));
}
#[test]
fn dl_is_a_builtin_block_shortcode() {
let reg = Registry::with_builtins();
let sc = reg.get("dl").expect("@dl must be a built-in");
assert!(matches!(sc.kind, ShortKindOpt::Block));
assert!(sc.arguments.is_empty(), "@dl takes no arguments in v0.3");
}
}