mdt_core 0.7.0

update markdown content anywhere using comments as template tags
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
use markdown::ParseOptions;
use markdown::mdast::Html;
use markdown::mdast::Node;
use markdown::to_mdast;
use serde::Deserialize;
use serde::Serialize;

use super::MdtError;
use super::MdtResult;
use crate::Position;
use crate::lexer::tokenize;
use crate::patterns::closing_pattern;
use crate::patterns::consumer_pattern;
use crate::patterns::inline_pattern;
use crate::patterns::provider_pattern;
use crate::tokens::Token;
use crate::tokens::TokenGroup;

/// A diagnostic produced during parsing. These are issues that don't prevent
/// parsing from completing but indicate problems in the source content.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ParseDiagnostic {
	/// A block was opened but never closed.
	UnclosedBlock {
		name: String,
		line: usize,
		column: usize,
	},
	/// An unknown transformer name was encountered.
	UnknownTransformer {
		name: String,
		line: usize,
		column: usize,
	},
	/// A transformer received the wrong number of arguments.
	InvalidTransformerArgs {
		name: String,
		expected: String,
		got: usize,
		line: usize,
		column: usize,
	},
}

/// Parse markdown content and return all blocks (provider and consumer) found
/// within it.
pub fn parse(content: impl AsRef<str>) -> MdtResult<Vec<Block>> {
	let content = content.as_ref();
	let html_nodes = get_html_nodes(content)?;
	let token_groups = tokenize(html_nodes)?;
	build_blocks_from_groups(&token_groups)
}

/// Parse markdown content and return blocks together with diagnostics.
/// Unlike `parse()`, this does not error on unclosed blocks — instead they
/// are collected as diagnostics.
pub fn parse_with_diagnostics(
	content: impl AsRef<str>,
) -> MdtResult<(Vec<Block>, Vec<ParseDiagnostic>)> {
	let content = content.as_ref();
	let html_nodes = get_html_nodes(content)?;
	let token_groups = tokenize(html_nodes)?;
	build_blocks_from_groups_with_diagnostics(&token_groups)
}

/// Build blocks from already-tokenized groups. This is the shared logic used
/// by both markdown parsing and source file scanning.
pub fn build_blocks_from_groups(token_groups: &[TokenGroup]) -> MdtResult<Vec<Block>> {
	build_blocks_inner(token_groups, false)
}

/// Like `build_blocks_from_groups`, but silently discards unclosed blocks
/// instead of returning an error. Used for source files where HTML comments
/// may appear in string literals without matching close tags.
pub fn build_blocks_from_groups_lenient(token_groups: &[TokenGroup]) -> MdtResult<Vec<Block>> {
	build_blocks_inner(token_groups, true)
}

/// Build blocks from token groups, collecting diagnostics instead of
/// hard-erroring. Unknown transformers and unclosed blocks are reported
/// as diagnostics rather than causing parse failure.
pub fn build_blocks_from_groups_with_diagnostics(
	token_groups: &[TokenGroup],
) -> MdtResult<(Vec<Block>, Vec<ParseDiagnostic>)> {
	let mut pending: Vec<BlockCreator> = vec![];
	let mut blocks: Vec<Block> = vec![];
	let mut diagnostics: Vec<ParseDiagnostic> = vec![];

	for group in token_groups {
		match classify_group_with_diagnostics(group, &mut diagnostics) {
			GroupKind::Provider {
				name,
				transformers,
				arguments,
			} => {
				pending.push(BlockCreator {
					name,
					r#type: BlockType::Provider,
					opening: group.position,
					closing: None,
					transformers,
					arguments,
				});
			}
			GroupKind::Consumer {
				name,
				transformers,
				arguments,
			} => {
				pending.push(BlockCreator {
					name,
					r#type: BlockType::Consumer,
					opening: group.position,
					closing: None,
					transformers,
					arguments,
				});
			}
			GroupKind::Inline {
				name,
				transformers,
				arguments,
			} => {
				pending.push(BlockCreator {
					name,
					r#type: BlockType::Inline,
					opening: group.position,
					closing: None,
					transformers,
					arguments,
				});
			}
			GroupKind::Close { name } => {
				let pos = pending.iter().rposition(|bc| bc.name == name);
				if let Some(idx) = pos {
					let mut creator = pending.remove(idx);
					creator.closing = Some(group.position);
					blocks.push(creator.into_block()?);
				}
			}
			GroupKind::Unknown => {}
		}
	}

	// Unclosed blocks become diagnostics instead of errors.
	for creator in pending {
		diagnostics.push(ParseDiagnostic::UnclosedBlock {
			name: creator.name,
			line: creator.opening.start.line,
			column: creator.opening.start.column,
		});
	}

	Ok((blocks, diagnostics))
}

fn build_blocks_inner(token_groups: &[TokenGroup], lenient: bool) -> MdtResult<Vec<Block>> {
	let mut pending: Vec<BlockCreator> = vec![];
	let mut blocks: Vec<Block> = vec![];

	for group in token_groups {
		match classify_group(group) {
			GroupKind::Provider {
				name,
				transformers,
				arguments,
			} => {
				pending.push(BlockCreator {
					name,
					r#type: BlockType::Provider,
					opening: group.position,
					closing: None,
					transformers,
					arguments,
				});
			}
			GroupKind::Consumer {
				name,
				transformers,
				arguments,
			} => {
				pending.push(BlockCreator {
					name,
					r#type: BlockType::Consumer,
					opening: group.position,
					closing: None,
					transformers,
					arguments,
				});
			}
			GroupKind::Inline {
				name,
				transformers,
				arguments,
			} => {
				pending.push(BlockCreator {
					name,
					r#type: BlockType::Inline,
					opening: group.position,
					closing: None,
					transformers,
					arguments,
				});
			}
			GroupKind::Close { name } => {
				// Find the most recent matching open block (search from the end)
				let pos = pending.iter().rposition(|bc| bc.name == name);
				if let Some(idx) = pos {
					let mut creator = pending.remove(idx);
					creator.closing = Some(group.position);
					blocks.push(creator.into_block()?);
				}
				// If no matching open block is found, silently ignore the close
				// tag. This keeps parsing lenient.
			}
			GroupKind::Unknown => {}
		}
	}

	// Any remaining unclosed blocks are errors in strict mode,
	// silently discarded in lenient mode.
	if !lenient {
		if let Some(creator) = pending.into_iter().next() {
			return Err(MdtError::MissingClosingTag(creator.name));
		}
	}

	Ok(blocks)
}

pub fn get_html_nodes(content: impl AsRef<str>) -> MdtResult<Vec<Html>> {
	let options = ParseOptions::gfm();
	let mdast =
		to_mdast(content.as_ref(), &options).map_err(|e| MdtError::Markdown(e.to_string()))?;
	let mut html_nodes = vec![];
	collect_html(&mdast, &mut html_nodes);

	Ok(html_nodes)
}

fn collect_html(node: &Node, nodes: &mut Vec<Html>) {
	match node {
		Node::Html(html) => nodes.push(html.clone()),
		_ => {
			if let Some(node) = node.children() {
				for child in node {
					collect_html(child, nodes);
				}
			}
		}
	}
}

enum GroupKind {
	Provider {
		name: String,
		transformers: Vec<Transformer>,
		arguments: Vec<String>,
	},
	Consumer {
		name: String,
		transformers: Vec<Transformer>,
		arguments: Vec<String>,
	},
	Inline {
		name: String,
		transformers: Vec<Transformer>,
		arguments: Vec<String>,
	},
	Close {
		name: String,
	},
	Unknown,
}

/// Classify a token group as a provider, consumer, close tag, or unknown.
fn classify_group(group: &TokenGroup) -> GroupKind {
	if group.matches_pattern(&provider_pattern()).unwrap_or(false) {
		let (name, transformers, arguments) =
			extract_name_transformers_and_arguments(group, &Token::ProviderTag);
		return GroupKind::Provider {
			name,
			transformers,
			arguments,
		};
	}

	if group.matches_pattern(&consumer_pattern()).unwrap_or(false) {
		let (name, transformers, arguments) =
			extract_name_transformers_and_arguments(group, &Token::ConsumerTag);
		return GroupKind::Consumer {
			name,
			transformers,
			arguments,
		};
	}

	if group.matches_pattern(&inline_pattern()).unwrap_or(false) {
		let (name, transformers, arguments) =
			extract_name_transformers_and_arguments(group, &Token::InlineTag);
		return GroupKind::Inline {
			name,
			transformers,
			arguments,
		};
	}

	if group.matches_pattern(&closing_pattern()).unwrap_or(false) {
		let name = extract_close_name(group);
		return GroupKind::Close { name };
	}

	GroupKind::Unknown
}

/// Like `classify_group` but also collects diagnostics for unknown
/// transformers.
fn classify_group_with_diagnostics(
	group: &TokenGroup,
	diagnostics: &mut Vec<ParseDiagnostic>,
) -> GroupKind {
	if group.matches_pattern(&provider_pattern()).unwrap_or(false) {
		let (name, transformers, arguments, unknown) =
			extract_name_transformers_arguments_with_diagnostics(group, &Token::ProviderTag);
		for unknown_name in unknown {
			diagnostics.push(ParseDiagnostic::UnknownTransformer {
				name: unknown_name,
				line: group.position.start.line,
				column: group.position.start.column,
			});
		}
		return GroupKind::Provider {
			name,
			transformers,
			arguments,
		};
	}

	if group.matches_pattern(&consumer_pattern()).unwrap_or(false) {
		let (name, transformers, arguments, unknown) =
			extract_name_transformers_arguments_with_diagnostics(group, &Token::ConsumerTag);
		for unknown_name in unknown {
			diagnostics.push(ParseDiagnostic::UnknownTransformer {
				name: unknown_name,
				line: group.position.start.line,
				column: group.position.start.column,
			});
		}
		return GroupKind::Consumer {
			name,
			transformers,
			arguments,
		};
	}

	if group.matches_pattern(&inline_pattern()).unwrap_or(false) {
		let (name, transformers, arguments, unknown) =
			extract_name_transformers_arguments_with_diagnostics(group, &Token::InlineTag);
		for unknown_name in unknown {
			diagnostics.push(ParseDiagnostic::UnknownTransformer {
				name: unknown_name,
				line: group.position.start.line,
				column: group.position.start.column,
			});
		}
		return GroupKind::Inline {
			name,
			transformers,
			arguments,
		};
	}

	if group.matches_pattern(&closing_pattern()).unwrap_or(false) {
		let name = extract_close_name(group);
		return GroupKind::Close { name };
	}

	GroupKind::Unknown
}

/// Extract the block name, positional arguments, and transformers from a
/// provider or consumer token group.
///
/// Arguments appear as `:STRING` sequences between the block name and the
/// first `|` pipe. Transformers appear after pipes.
fn extract_name_transformers_and_arguments(
	group: &TokenGroup,
	tag_token: &Token,
) -> (String, Vec<Transformer>, Vec<String>) {
	let mut name = String::new();
	let mut transformers = Vec::new();
	let mut arguments = Vec::new();
	let mut found_tag = false;
	let mut found_name = false;
	let mut in_transformers = false;

	let mut iter = group.tokens.iter().peekable();

	while let Some(token) = iter.next() {
		if !found_tag {
			if token.same_type(tag_token) {
				found_tag = true;
			}
			continue;
		}

		if !found_name {
			if let Token::Ident(ident) = token {
				name.clone_from(ident);
				found_name = true;
			}
			continue;
		}

		if !in_transformers {
			match token {
				Token::ArgumentDelimiter => {
					// Skip whitespace before the string value.
					while let Some(Token::Whitespace(_) | Token::Newline) = iter.peek() {
						iter.next();
					}
					if let Some(Token::String(s, _)) = iter.next() {
						arguments.push(s.clone());
					}
					continue;
				}
				Token::Pipe => {
					in_transformers = true;
					if let Some(transformer) = parse_transformer(&mut iter) {
						transformers.push(transformer);
					}
					continue;
				}
				_ => continue,
			}
		}

		if matches!(token, Token::Pipe) {
			if let Some(transformer) = parse_transformer(&mut iter) {
				transformers.push(transformer);
			}
		}
	}

	(name, transformers, arguments)
}

/// Like `extract_name_transformers_and_arguments` but also collects unknown
/// transformer names for diagnostics.
fn extract_name_transformers_arguments_with_diagnostics(
	group: &TokenGroup,
	tag_token: &Token,
) -> (String, Vec<Transformer>, Vec<String>, Vec<String>) {
	let mut name = String::new();
	let mut transformers = Vec::new();
	let mut arguments = Vec::new();
	let mut unknown_transformers = Vec::new();
	let mut found_tag = false;
	let mut found_name = false;
	let mut in_transformers = false;

	let mut iter = group.tokens.iter().peekable();

	while let Some(token) = iter.next() {
		if !found_tag {
			if token.same_type(tag_token) {
				found_tag = true;
			}
			continue;
		}

		if !found_name {
			if let Token::Ident(ident) = token {
				name.clone_from(ident);
				found_name = true;
			}
			continue;
		}

		if !in_transformers {
			match token {
				Token::ArgumentDelimiter => {
					while let Some(Token::Whitespace(_) | Token::Newline) = iter.peek() {
						iter.next();
					}
					if let Some(Token::String(s, _)) = iter.next() {
						arguments.push(s.clone());
					}
					continue;
				}
				Token::Pipe => {
					in_transformers = true;
					match parse_transformer_with_unknown(&mut iter) {
						TransformerParseResult::Ok(transformer) => {
							transformers.push(transformer);
						}
						TransformerParseResult::Unknown(unknown_name) => {
							unknown_transformers.push(unknown_name);
						}
						TransformerParseResult::NoIdent => {}
					}
					continue;
				}
				_ => continue,
			}
		}

		if matches!(token, Token::Pipe) {
			match parse_transformer_with_unknown(&mut iter) {
				TransformerParseResult::Ok(transformer) => transformers.push(transformer),
				TransformerParseResult::Unknown(unknown_name) => {
					unknown_transformers.push(unknown_name);
				}
				TransformerParseResult::NoIdent => {}
			}
		}
	}

	(name, transformers, arguments, unknown_transformers)
}

/// Result of attempting to parse a transformer from the token stream.
enum TransformerParseResult {
	/// Successfully parsed a known transformer.
	Ok(Transformer),
	/// Found an identifier but it wasn't a known transformer name.
	Unknown(String),
	/// No identifier found after pipe.
	NoIdent,
}

/// Parse a transformer, returning information about unknown transformer names.
fn parse_transformer_with_unknown(
	iter: &mut std::iter::Peekable<std::slice::Iter<'_, Token>>,
) -> TransformerParseResult {
	// Skip whitespace
	while let Some(Token::Whitespace(_) | Token::Newline) = iter.peek() {
		iter.next();
	}

	let transformer_name = match iter.next() {
		Some(Token::Ident(name)) => name.clone(),
		_ => return TransformerParseResult::NoIdent,
	};

	let transformer_type = match transformer_name.as_str() {
		"trim" => TransformerType::Trim,
		"trimStart" | "trim_start" => TransformerType::TrimStart,
		"trimEnd" | "trim_end" => TransformerType::TrimEnd,
		"wrap" => TransformerType::Wrap,
		"indent" => TransformerType::Indent,
		"codeblock" | "codeBlock" | "code_block" => TransformerType::CodeBlock,
		"code" => TransformerType::Code,
		"replace" => TransformerType::Replace,
		"prefix" => TransformerType::Prefix,
		"suffix" => TransformerType::Suffix,
		"linePrefix" | "line_prefix" => TransformerType::LinePrefix,
		"lineSuffix" | "line_suffix" => TransformerType::LineSuffix,
		"if" => TransformerType::If,
		_ => return TransformerParseResult::Unknown(transformer_name),
	};

	let args = parse_transformer_args(iter);

	TransformerParseResult::Ok(Transformer {
		r#type: transformer_type,
		args,
	})
}

/// Parse a single transformer from the token stream (after the pipe).
fn parse_transformer(
	iter: &mut std::iter::Peekable<std::slice::Iter<'_, Token>>,
) -> Option<Transformer> {
	match parse_transformer_with_unknown(iter) {
		TransformerParseResult::Ok(transformer) => Some(transformer),
		TransformerParseResult::Unknown(_) | TransformerParseResult::NoIdent => None,
	}
}

/// Parse transformer arguments (`:value` pairs) from the token stream.
fn parse_transformer_args(
	iter: &mut std::iter::Peekable<std::slice::Iter<'_, Token>>,
) -> Vec<Argument> {
	let mut args = Vec::new();

	loop {
		// Skip whitespace
		while let Some(Token::Whitespace(_) | Token::Newline) = iter.peek() {
			iter.next();
		}

		match iter.peek() {
			Some(Token::ArgumentDelimiter) => {
				iter.next(); // consume ':'

				// Skip whitespace
				while let Some(Token::Whitespace(_) | Token::Newline) = iter.peek() {
					iter.next();
				}

				match iter.next() {
					Some(Token::String(s, _)) => args.push(Argument::String(s.clone())),
					Some(Token::Int(n)) => {
						args.push(Argument::Number(OrderedFloat(*n as f64)));
					}
					Some(Token::Float(n)) => {
						args.push(Argument::Number(OrderedFloat(*n)));
					}
					Some(Token::Ident(s)) if s == "true" => args.push(Argument::Boolean(true)),
					Some(Token::Ident(s)) if s == "false" => args.push(Argument::Boolean(false)),
					_ => break,
				}
			}
			_ => break,
		}
	}

	args
}

/// Extract the block name from a close tag token group.
fn extract_close_name(group: &TokenGroup) -> String {
	for token in &group.tokens {
		if let Token::CloseTag = token {
			// The name is the next Ident token after CloseTag
			let mut found_close = false;
			for t in &group.tokens {
				if found_close {
					if let Token::Ident(name) = t {
						return name.clone();
					}
				}
				if matches!(t, Token::CloseTag) {
					found_close = true;
				}
			}
		}
	}
	String::new()
}

struct BlockCreator {
	name: String,
	r#type: BlockType,
	opening: Position,
	closing: Option<Position>,
	transformers: Vec<Transformer>,
	arguments: Vec<String>,
}

impl BlockCreator {
	pub fn into_block(self) -> MdtResult<Block> {
		let Some(closing) = self.closing else {
			return Err(MdtError::MissingClosingTag(self.name));
		};

		let block = Block {
			name: self.name,
			r#type: self.r#type,
			opening: self.opening,
			closing,
			transformers: self.transformers,
			arguments: self.arguments,
		};

		Ok(block)
	}
}

/// <!-- {=mdtBlockDocs|trim|linePrefix:"/// ":true} -->
/// A parsed template block representing either a provider or consumer.
///
/// Provider blocks are defined in `*.t.md` template files using `{@name}...{/name}` tag syntax (wrapped in HTML comments). They supply content that gets distributed to matching consumers.
///
/// Consumer blocks appear in any scanned file using `{=name}...{/name}` tag syntax (wrapped in HTML comments). Their content is replaced with the matching provider's content (after applying any transformers) when `mdt update` is run.
///
/// Each block tracks its [`name`](Block::name) for provider-consumer matching, its [`BlockType`], the [`Position`] of its opening and closing tags, and any [`Transformer`]s to apply during content injection.
/// <!-- {/mdtBlockDocs} -->
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Block {
	/// The name of the block used for matching providers to consumers.
	pub name: String,
	/// Whether this is a provider or consumer block.
	pub r#type: BlockType,
	/// Position of the opening tag (e.g., `{@name}` or `{=name}`).
	pub opening: Position,
	/// Position of the closing tag (e.g., `{/name}`).
	pub closing: Position,
	/// Transformers to apply when injecting provider content into this
	/// consumer.
	pub transformers: Vec<Transformer>,
	/// Positional arguments on the block tag.
	/// For providers, these are parameter names (variable names).
	/// For consumers, these are argument values (string literals).
	pub arguments: Vec<String>,
}

/// <!-- {=mdtTransformerDocs|trim|linePrefix:"/// ":true} -->
/// A content transformer applied to provider content during injection into a consumer block.
///
/// Transformers are specified using pipe-delimited syntax after the block name in a consumer tag:
///
/// ```markdown
/// <!-- {=blockName|trim|indent:"  "|linePrefix:"/// "} -->
/// ```
///
/// Transformers are applied in left-to-right order. Each transformer has a [`TransformerType`] and zero or more [`Argument`]s passed via colon-delimited syntax (e.g., `indent:"  "`).
///
/// Available transformers: `trim`, `trimStart`, `trimEnd`, `indent`, `prefix`, `suffix`, `linePrefix`, `lineSuffix`, `wrap`, `codeBlock`, `code`, `replace`, `if`.
/// <!-- {/mdtTransformerDocs} -->
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Transformer {
	/// The kind of transformation to apply (e.g., `Trim`, `Indent`,
	/// `LinePrefix`).
	pub r#type: TransformerType,
	/// Arguments passed to the transformer via colon-delimited syntax.
	pub args: Vec<Argument>,
}

/// <!-- {=mdtArgumentDocs|trim|linePrefix:"/// ":true} -->
/// An argument value passed to a [`Transformer`].
///
/// Arguments are specified after the transformer name using colon-delimited syntax:
///
/// ```markdown
/// <!-- {=block|replace:"old":"new"|indent:"  "} -->
/// ```
///
/// Three types are supported:
///
/// - **String** — Quoted text, e.g. `"hello"` or `'hello'`
/// - **Number** — Integer or floating-point, e.g. `42` or `3.14`
/// - **Boolean** — `true` or `false`
/// <!-- {/mdtArgumentDocs} -->
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Argument {
	/// A quoted string value, e.g. `"hello"` or `'world'`.
	String(String),
	/// A numeric value (integer or float), e.g. `42` or `3.14`.
	Number(OrderedFloat),
	/// A boolean value: `true` or `false`.
	Boolean(bool),
}

/// A float wrapper that implements `Eq` via approximate comparison,
/// allowing `Argument` to derive `PartialEq` cleanly.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct OrderedFloat(pub f64);

impl PartialEq for OrderedFloat {
	fn eq(&self, other: &Self) -> bool {
		float_cmp::approx_eq!(f64, self.0, other.0)
	}
}

impl std::fmt::Display for OrderedFloat {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}", self.0)
	}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum TransformerType {
	/// Trim all whitespace from the start and end of the content.
	Trim,
	/// Trim all whitespace from the start of the content.
	TrimStart,
	/// Trim all whitespace from the end of the content.
	TrimEnd,
	/// Wrap the content in the given string.
	Wrap,
	/// Indent each line with the given string.
	Indent,
	/// Wrap the content in a codeblock with the provided language string.
	CodeBlock,
	/// Wrap the content with inline code `` `content` ``.
	Code,
	/// Replace all instances of the given string with the replacement string.
	Replace,
	/// Add a prefix string before the content.
	Prefix,
	/// Add a suffix string after the content.
	Suffix,
	/// Add a prefix string before each line.
	LinePrefix,
	/// Add a suffix string after each line.
	LineSuffix,
	/// Conditionally include content based on a data value.
	/// If the value at the given dot-separated path is truthy, the content is
	/// included unchanged. Otherwise, the content is replaced with an empty
	/// string.
	If,
}

impl std::fmt::Display for TransformerType {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::Trim => write!(f, "trim"),
			Self::TrimStart => write!(f, "trimStart"),
			Self::TrimEnd => write!(f, "trimEnd"),
			Self::Wrap => write!(f, "wrap"),
			Self::Indent => write!(f, "indent"),
			Self::CodeBlock => write!(f, "codeBlock"),
			Self::Code => write!(f, "code"),
			Self::Replace => write!(f, "replace"),
			Self::Prefix => write!(f, "prefix"),
			Self::Suffix => write!(f, "suffix"),
			Self::LinePrefix => write!(f, "linePrefix"),
			Self::LineSuffix => write!(f, "lineSuffix"),
			Self::If => write!(f, "if"),
		}
	}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum BlockType {
	/// These are the blocks that are used to provide a value to any consumers.
	/// Their names can be referenced by consumers to hoist content. They should
	/// only exist within the confines of a `*.t.md` file.
	///
	/// ```md
	/// <!-- {@exampleProvider} -->
	/// <!-- {/exampleProvider} -->
	/// ```
	Provider,
	/// Consumers are blocks that have their content hoisted from a provider
	/// with the same name. They will be updated to the latest content whenever
	/// the `mdt` command is run.
	///
	/// ```md
	/// <!-- {=exampleConsumer} -->
	/// <!-- {/exampleConsumer} -->
	/// ```
	Consumer,
	/// Inline blocks render minijinja template content directly from the
	/// block's first positional argument (without resolving a provider).
	///
	/// ```md
	/// <!-- {~version:"{{ pkg.version }}"} -->0.0.0<!-- {/version} -->
	/// ```
	Inline,
}

impl std::fmt::Display for BlockType {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::Provider => write!(f, "provider"),
			Self::Consumer => write!(f, "consumer"),
			Self::Inline => write!(f, "inline"),
		}
	}
}