markup_fmt 0.27.0

Configurable HTML, Vue, Svelte, Astro, Angular, Jinja, Twig, Nunjucks, Vento, Mustache, Handlebars and XML formatter.
Documentation
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#[derive(Debug)]
/// Angular for loop: `@for ( ... )`.
///
/// See https://angular.dev/api/core/@for.
pub struct AngularFor<'s> {
    pub binding: (&'s str, usize),
    pub expr: (&'s str, usize),
    pub track: Option<(&'s str, usize)>,
    pub aliases: Vec<(&'s str, usize)>,
    pub children: Vec<Node<'s>>,
    pub empty: Option<Vec<Node<'s>>>,
}

#[derive(Debug)]
/// Angular conditional block: `@if ( condition )`.
///
/// See https://angular.dev/api/core/@if.
pub struct AngularIf<'s> {
    pub expr: (&'s str, usize),
    pub reference: Option<(&'s str, usize)>,
    pub children: Vec<Node<'s>>,
    pub else_if_blocks: Vec<AngularElseIf<'s>>,
    pub else_children: Option<Vec<Node<'s>>>,
}

#[derive(Debug)]
/// Angular else-if block: `@else if ( condition )`.
///
/// See https://angular.dev/api/core/@if.
pub struct AngularElseIf<'s> {
    pub expr: (&'s str, usize),
    pub reference: Option<(&'s str, usize)>,
    pub children: Vec<Node<'s>>,
}

#[derive(Debug)]
pub struct AngularGenericBlock<'s> {
    pub keyword: &'s str,
    pub header: Option<&'s str>,
    pub children: Vec<Node<'s>>,
}

#[derive(Debug)]
/// Angular interpolation: `{{ expression }}`.
///
/// See https://angular.dev/guide/templates/binding#render-dynamic-text-with-text-interpolation.
pub struct AngularInterpolation<'s> {
    pub expr: &'s str,
    pub start: usize,
}

#[derive(Debug)]
/// Angular let variable declaration: `@let name = expression`.
///
/// See https://angular.dev/api/core/@let.
pub struct AngularLet<'s> {
    pub name: &'s str,
    pub expr: (&'s str, usize),
}

#[derive(Debug)]
/// Angular switch statement: `@switch (expression)`.
///
/// See https://angular.dev/api/core/@switch.
pub struct AngularSwitch<'s> {
    pub expr: (&'s str, usize),
    pub arms: Vec<AngularSwitchArm<'s>>,
}

#[derive(Debug)]
/// `@case` or `@default` arm of an `AngularSwitch`.
///
/// See https://angular.dev/api/core/@switch.
pub struct AngularSwitchArm<'s> {
    pub keyword: &'static str,
    pub expr: Option<(&'s str, usize)>,
    pub children: Vec<Node<'s>>,
}

#[derive(Debug)]
/// Astro attribute: `{expression}` or `name={expression}`.
///
/// See https://docs.astro.build/en/reference/astro-syntax/#dynamic-attributes.
pub struct AstroAttribute<'s> {
    pub name: Option<&'s str>,
    pub expr: (&'s str, usize),
}

#[derive(Debug)]
/// Astro expression block: `{...}`.
///
/// See https://docs.astro.build/en/reference/astro-syntax/#dynamic-html.
pub struct AstroExpr<'s> {
    pub children: Vec<AstroExprChild<'s>>,
    pub has_line_comment: bool,
    pub start: usize,
}

#[derive(Debug)]
/// See https://docs.astro.build/en/core-concepts/astro-syntax/#dynamic-html.
pub enum AstroExprChild<'s> {
    Script(&'s str),
    Template(Vec<Node<'s>>),
}

#[derive(Debug)]
pub enum Attribute<'s> {
    Astro(AstroAttribute<'s>),
    JinjaBlock(JinjaBlock<'s, Attribute<'s>>),
    JinjaComment(JinjaComment<'s>),
    JinjaTag(JinjaTag<'s>),
    JsComment(JsComment<'s>),
    Native(NativeAttribute<'s>),
    Svelte(SvelteAttribute<'s>),
    SvelteAttachment(SvelteAttachment<'s>),
    VentoTagOrBlock(NodeKind<'s>),
    VueDirective(VueDirective<'s>),
}

#[derive(Debug)]
/// `<![CDATA[ ... ]]>`
///
/// See https://www.w3.org/TR/xml/#sec-cdata-sect
pub struct Cdata<'s> {
    pub raw: &'s str,
}

#[derive(Debug)]
/// Comment in HTML: `<!-- ... -->`.
///
/// See https://developer.mozilla.org/en-US/docs/Web/HTML/Comments
pub struct Comment<'s> {
    pub raw: &'s str,
}

#[derive(Debug)]
/// HTML doctype declaration: `<!DOCTYPE ...>`.
///
/// See https://developer.mozilla.org/en-US/docs/Glossary/Doctype
pub struct Doctype<'s> {
    pub keyword: &'s str,
    pub value: &'s str,
}

#[derive(Debug)]
/// HTML element with its attributes and children.
///
/// See https://developer.mozilla.org/en-US/docs/Web/HTML/Element
pub struct Element<'s> {
    pub tag_name: &'s str,
    pub attrs: Vec<Attribute<'s>>,
    pub first_attr_same_line: bool,
    pub children: Vec<Node<'s>>,
    pub self_closing: bool,
    pub void_element: bool,
}

#[derive(Debug)]
/// Front matter content in a file, typically enclosed in `---`.
///
/// See https://docs.astro.build/en/guides/markdown-content/.
pub struct FrontMatter<'s> {
    pub raw: &'s str,
    pub start: usize,
}

#[derive(Debug)]
/// Jinja block containing nested Jinja tags or HTML elements.
///
/// See https://jinja.palletsprojects.com/en/stable/templates/#list-of-control-structures.
pub struct JinjaBlock<'s, T> {
    pub body: Vec<JinjaTagOrChildren<'s, T>>,
}

#[derive(Debug)]
/// Jinja comment: `{# ... #}`.
///
/// See https://jinja.palletsprojects.com/en/stable/templates/#comments.
pub struct JinjaComment<'s> {
    pub raw: &'s str,
}

#[derive(Debug)]
/// Jinja interpolation: `{{ ... }}`.
///
/// See https://jinja.palletsprojects.com/en/stable/templates/#expressions.
pub struct JinjaInterpolation<'s> {
    pub expr: &'s str,
    pub start: usize,
    pub trim_prev: bool,
    pub trim_next: bool,
}

#[derive(Debug)]
/// Jinja tag: `{% ... %}`.
///
/// See https://jinja.palletsprojects.com/en/stable/templates/#list-of-control-structures.
pub struct JinjaTag<'s> {
    pub content: &'s str,
    pub start: usize,
}

#[derive(Debug)]
pub enum JinjaTagOrChildren<'s, T> {
    Tag(JinjaTag<'s>),
    Children(Vec<T>),
}

#[derive(Debug)]
pub struct JsComment<'s> {
    pub block: bool,
    pub raw: &'s str,
}

#[derive(Debug)]
/// Mustache block: `{{#variable}}{{/variable}}`.
///
/// See https://mustache.github.io/mustache.5.html
pub struct MustacheBlock<'s> {
    pub controls: Vec<MustacheBlockControl<'s>>,
    pub children: Vec<Vec<Node<'s>>>,
}

#[derive(Debug)]
pub struct MustacheBlockControl<'s> {
    pub name: &'s str,
    pub prefix: &'s str,
    pub content: Option<&'s str>,
    pub wc_before: bool,
    pub wc_after: bool,
}

#[derive(Debug)]
/// Mustache interpolation: `{{expression}}`.
///
/// See https://mustache.github.io/mustache.5.html
pub struct MustacheInterpolation<'s> {
    pub content: &'s str,
}

#[derive(Debug)]
/// Standard HTML attribute.
///
/// See https://developer.mozilla.org/en-US/docs/Glossary/Attribute
pub struct NativeAttribute<'s> {
    pub name: &'s str,
    pub value: Option<(&'s str, usize)>,
    pub quote: Option<char>,
}

#[derive(Debug)]
pub struct Node<'s> {
    pub kind: NodeKind<'s>,
    pub raw: &'s str,
}

#[derive(Debug)]
pub enum NodeKind<'s> {
    AngularFor(AngularFor<'s>),
    AngularGenericBlocks(Vec<AngularGenericBlock<'s>>),
    AngularIf(AngularIf<'s>),
    AngularInterpolation(AngularInterpolation<'s>),
    AngularLet(AngularLet<'s>),
    AngularSwitch(AngularSwitch<'s>),
    AstroExpr(AstroExpr<'s>),
    Cdata(Cdata<'s>),
    Comment(Comment<'s>),
    Doctype(Doctype<'s>),
    Element(Element<'s>),
    FrontMatter(FrontMatter<'s>),
    JinjaBlock(JinjaBlock<'s, Node<'s>>),
    JinjaComment(JinjaComment<'s>),
    JinjaInterpolation(JinjaInterpolation<'s>),
    JinjaTag(JinjaTag<'s>),
    MustacheBlock(MustacheBlock<'s>),
    MustacheInterpolation(MustacheInterpolation<'s>),
    SvelteAtTag(SvelteAtTag<'s>),
    SvelteAwaitBlock(Box<SvelteAwaitBlock<'s>>),
    SvelteEachBlock(SvelteEachBlock<'s>),
    SvelteIfBlock(SvelteIfBlock<'s>),
    SvelteInterpolation(SvelteInterpolation<'s>),
    SvelteKeyBlock(SvelteKeyBlock<'s>),
    SvelteSnippetBlock(SvelteSnippetBlock<'s>),
    Text(TextNode<'s>),
    VentoBlock(VentoBlock<'s>),
    VentoComment(VentoComment<'s>),
    VentoEval(VentoEval<'s>),
    VentoInterpolation(VentoInterpolation<'s>),
    VentoTag(VentoTag<'s>),
    VueInterpolation(VueInterpolation<'s>),
    XmlDecl(XmlDecl<'s>),
}

#[derive(Debug)]
pub struct Root<'s> {
    pub children: Vec<Node<'s>>,
}

#[derive(Debug)]
/// Svelte `@` tag: (`@render`, `@const`, etc).
///
/// See https://svelte.dev/docs/svelte/@render.
pub struct SvelteAtTag<'s> {
    pub name: &'s str,
    pub expr: (&'s str, usize),
}

#[derive(Debug)]
/// Svelte attribute: `{expression}` or `name={expression}`.
///
/// See https://svelte.dev/docs/svelte/basic-markup#Element-attributes.
pub struct SvelteAttribute<'s> {
    pub name: Option<&'s str>,
    pub expr: (&'s str, usize),
}

#[derive(Debug)]
/// Svelte attachment: `{@attach expression}`.
///
/// See https://svelte.dev/docs/svelte/@attach.
pub struct SvelteAttachment<'s> {
    pub expr: (&'s str, usize),
}

#[derive(Debug)]
/// Svelte await block `{#await expression}...{:then name}...{:catch name}...{/await}`.
///
/// See https://svelte.dev/docs/svelte/await.
pub struct SvelteAwaitBlock<'s> {
    pub expr: (&'s str, usize),
    pub then_binding: Option<Option<(&'s str, usize)>>, // binding can be optional with `then` keyword only
    pub catch_binding: Option<Option<(&'s str, usize)>>, // binding can be optional with `catch` keyword only
    pub children: Vec<Node<'s>>,
    pub then_block: Option<SvelteThenBlock<'s>>,
    pub catch_block: Option<SvelteCatchBlock<'s>>,
}

#[derive(Debug)]
/// The `{:catch error}...` part of a `SvelteAwaitBlock`.
pub struct SvelteCatchBlock<'s> {
    pub binding: Option<(&'s str, usize)>,
    pub children: Vec<Node<'s>>,
}

#[derive(Debug)]
/// The `{:then value}...` part of a `SvelteAwaitBlock`.
pub struct SvelteThenBlock<'s> {
    pub binding: Option<(&'s str, usize)>,
    pub children: Vec<Node<'s>>,
}

#[derive(Debug)]
/// Svelte each block: `{#each expression as name}...{/each}`.
///
/// See https://svelte.dev/docs/svelte/each.
pub struct SvelteEachBlock<'s> {
    pub expr: (&'s str, usize),
    pub binding: Option<(&'s str, usize)>,
    pub index: Option<&'s str>,
    pub key: Option<(&'s str, usize)>,
    pub children: Vec<Node<'s>>,
    pub else_children: Option<Vec<Node<'s>>>,
}

#[derive(Debug)]
/// Svelte if block: `{#if expression}...{:else if expression}...{/if}`.
///
/// See https://svelte.dev/docs/svelte/if.
pub struct SvelteIfBlock<'s> {
    pub expr: (&'s str, usize),
    pub children: Vec<Node<'s>>,
    pub else_if_blocks: Vec<SvelteElseIfBlock<'s>>,
    pub else_children: Option<Vec<Node<'s>>>,
}

#[derive(Debug)]
/// The `{:else if condition}...` part of a `SvelteIfBlock`.
pub struct SvelteElseIfBlock<'s> {
    pub expr: (&'s str, usize),
    pub children: Vec<Node<'s>>,
}

#[derive(Debug)]
/// Svelte interpolation: `{expression}`.
///
/// See https://svelte.dev/docs/svelte/basic-markup#Text-expressions.
pub struct SvelteInterpolation<'s> {
    pub expr: (&'s str, usize),
}

#[derive(Debug)]
/// Svelte key block: `{#key expression}...{/key}`.
///
/// See https://svelte.dev/docs/svelte/key.
pub struct SvelteKeyBlock<'s> {
    pub expr: (&'s str, usize),
    pub children: Vec<Node<'s>>,
}

#[derive(Debug)]
/// Svelte snippet block: `{#snippet name()}...{/snippet}`.
///
/// See https://svelte.dev/docs/svelte/snippet.
pub struct SvelteSnippetBlock<'s> {
    pub signature: (&'s str, usize),
    pub children: Vec<Node<'s>>,
}

#[derive(Debug)]
/// Plain text node.
pub struct TextNode<'s> {
    pub raw: &'s str,
    pub line_breaks: usize,
    pub start: usize,
}

#[derive(Debug)]
/// Vento block: `{{ keyword ... }}...{{ /keyword }}`
///
/// See https://vento.js.org/syntax/blocks.
pub struct VentoBlock<'s> {
    pub body: Vec<VentoTagOrChildren<'s>>,
}

#[derive(Debug)]
/// Vento comment: `{{# ... #}}`.
///
/// See https://vento.js.org/syntax/comments/.
pub struct VentoComment<'s> {
    pub raw: &'s str,
}

#[derive(Debug)]
/// Vento eval block for JavaScript evaluation: `{{> ... }}`.
///
/// See https://vento.js.org/syntax/javascript/.
pub struct VentoEval<'s> {
    pub raw: &'s str,
    pub start: usize,
}

#[derive(Debug)]
/// Vento interpolation `{{ ... }}`.
///
/// See https://vento.js.org/syntax/print/.
pub struct VentoInterpolation<'s> {
    pub expr: &'s str,
    pub start: usize,
    pub trim_prev: bool,
    pub trim_next: bool,
}

#[derive(Debug)]
/// Vento tag: `{{ keyword ... }}`.
///
/// See https://vento.js.org/syntax/include/.
pub struct VentoTag<'s> {
    pub tag: &'s str,
    pub trim_prev: bool,
    pub trim_next: bool,
}

#[derive(Debug)]
pub enum VentoTagOrChildren<'s> {
    Tag(VentoTag<'s>),
    Children(Vec<Node<'s>>),
}

#[derive(Debug)]
/// Vue directive: `v-if`, `v-for`, etc.
///
/// See https://vuejs.org/guide/essentials/template-syntax.html#directives.
pub struct VueDirective<'s> {
    pub name: &'s str,
    pub arg_and_modifiers: Option<&'s str>,
    pub value: Option<(&'s str, usize)>,
}

#[derive(Debug)]
/// Vue interpolation: `{{ expression }}`.
///
/// See https://vuejs.org/guide/essentials/template-syntax.html#text-interpolation.
pub struct VueInterpolation<'s> {
    pub expr: &'s str,
    pub start: usize,
}

#[derive(Debug)]
/// XML declaration.
///
/// See https://www.w3.org/TR/xml/#sec-prolog-dtd
pub struct XmlDecl<'s> {
    pub attrs: Vec<NativeAttribute<'s>>,
}