kismesis 0.5.0

A static site generator with plugins and a custom markup language.
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
//! The types that the parser might output.

use std::{collections::HashMap, path::Path};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{KisTemplateId, KisTokenId, Kismesis};

use super::state::TextPos;

pub type Scoped<'a, T> = (T, KisTokenId);
pub type ScopedExpression<'a> = Scoped<'a, (Option<&'a Ranged<Expression>>, MultilineRange)>;

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// A Kismesis string, containing both string literals and expressions
pub enum StringParts {
	Expression(Ranged<Expression>),
	String(String),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// An HTML attribute, whose value can never be null
pub struct Attribute {
	pub name: Ranged<String>,
	pub value: Ranged<Expression>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// A Kismesis argument, which is simular to an attribute but its value is nullable
pub struct Argument {
	pub name: Ranged<String>,
	pub value: Option<Ranged<Expression>>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// An HTML tag
pub struct HtmlTag {
	/// The Kismesis name of the tag. It might get compiled to something else, like how `<container>` is compiled as `<div>`
	pub name: Ranged<String>,
	pub attributes: Vec<Attribute>,
	/// Subtags get compiled as children of the tag
	pub subtags: Vec<HtmlTag>,
	/// Children of the tag
	pub body: Vec<HtmlNodes>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// A Kismesis macro
pub struct Macro {
	pub name: Ranged<String>,
	pub arguments: Vec<Argument>,
	pub body: Vec<HtmlNodes>,
}

#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// A part of the document denotated by a heading and some content
pub struct Section {
	pub depth: Ranged<usize>,
	pub name: Ranged<Paragraph>,
	pub subtitle: Option<Ranged<Paragraph>>,
	pub content: Vec<Paragraph>,
}

impl Section {
	#[must_use]
	pub fn into_tag(self) -> HtmlTag {
		let mut tags = Vec::new();
		let hstr = format!("h{}", self.depth.value);
		let title = HtmlTag {
			name: Ranged {
				value: hstr,
				range: self.depth.range,
			},
			attributes: vec![],
			body: vec![HtmlNodes::Paragraph(self.name.value)],
			subtags: vec![],
		};
		let header = match self.subtitle {
			Some(subtitle) => {
				let subtitle = HtmlTag {
					name: Ranged {
						value: String::from("p"),
						range: subtitle.range,
					},
					attributes: vec![],
					body: vec![HtmlNodes::Paragraph(subtitle.value)],
					subtags: vec![],
				};
				HtmlTag {
					name: Ranged {
						value: String::from("hgroup"),
						range: MultilineRange::Single(TextPos::new()),
					},
					attributes: vec![],
					body: vec![HtmlNodes::HtmlTag(title), HtmlNodes::HtmlTag(subtitle)],
					subtags: vec![],
				}
			}
			None => title,
		};

		let mut content = Vec::new();

		for x in self.content {
			match (x.0.first(), x.0.len()) {
				(_, 2..) | (Some(HtmlNodes::String(_)), _) => {
					let x = HtmlTag {
						name: Ranged {
							value: "p".to_string(),
							range: MultilineRange::Single(TextPos::default()),
						},
						attributes: vec![],
						subtags: vec![],
						body: vec![HtmlNodes::Paragraph(Paragraph(x.0))],
					};
					content.push(HtmlNodes::HtmlTag(x));
				}
				(Some(x), 1) => content.push(x.clone()),
				_ => content.push(HtmlNodes::Paragraph(x)),
			}
		}

		tags.push(HtmlNodes::HtmlTag(header));
		tags.append(&mut content);

		HtmlTag {
			name: Ranged {
				value: String::from("section"),
				range: MultilineRange::Single(TextPos::new()),
			},
			attributes: vec![],
			body: tags,
			subtags: vec![],
		}
	}
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// The result of a plugin call
pub struct PlugCall {
	pub name: Ranged<String>,
	pub body: Vec<HtmlNodes>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// All AST nodes that end up in the final HTML in one way or another
pub enum HtmlNodes {
	Content(Content),
	For(ForTag),
	HtmlTag(HtmlTag),
	If(IfTag),
	MacroCall(Macro),
	Paragraph(Paragraph),
	PlugCall(Box<PlugCall>),
	Raw(String),
	String(Vec<StringParts>),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Paragraph(pub Vec<HtmlNodes>);

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Content {
	pub content: Option<usize>,
	pub position: MultilineRange,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// All AST nodes that can be at the topmost level of a file
pub enum TopNodes {
	Content(Content),
	Doctype(String),
	For(ForTag),
	HtmlTag(HtmlTag),
	If(IfTag),
	MacroCall(Macro),
	Paragraph(Paragraph),
	PlugCall(Box<PlugCall>),
	Raw(String),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// All AST nodes that can be in any level of a file except for the topmost
pub enum BodyTags {
	Content(Content),
	For(ForTag),
	HtmlTag(HtmlTag),
	If(IfTag),
	MacroCall(Macro),
	PlugCall(Box<PlugCall>),
	Raw(String),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// All Kismesis tags
pub enum Tag {
	Content(Content),
	Doctype(String),
	For(ForTag),
	Html(HtmlTag),
	If(IfTag),
	MacroCall(Macro),
	MacroDef(Macro),
	PlugCall(Box<PlugCall>),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// All AST nodes
pub enum BodyNodes {
	Content(Content),
	Doctype(String),
	For(ForTag),
	HtmlTag(HtmlTag),
	If(IfTag),
	LambdaDef(Lambda),
	MacroCall(Macro),
	MacroDef(Macro),
	Paragraph(Paragraph),
	PlugCall(Box<PlugCall>),
	Raw(String),
	SetStmt(String, String),
	VarDef(Variable),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// The completely parsed file, including macros, variables, lambdas and a template
pub struct ParsedFile {
	pub file_id: KisTokenId,
	pub body: Vec<TopNodes>,
	pub defined_macros: Vec<Macro>,
	pub defined_variables: Vec<Variable>,
	pub defined_lambdas: Vec<Lambda>,
	pub template: Option<KisTemplateId>,
}

// /// An Option with the possibility of something not being set
// pub enum VariableOption<T> {
// 	/// The variable was found and has a value
// 	Some(T),
// 	/// The variable was not found
// 	None,
// 	/// The variable was found, but it's not set
// 	Unset,
// }

impl ParsedFile {
	#[must_use]
	pub fn new(file_id: KisTokenId) -> Self {
		Self {
			file_id,
			body: vec![],
			defined_macros: vec![],
			defined_variables: vec![],
			defined_lambdas: vec![],
			template: None,
		}
	}

	// pub fn get_macro_template<'a>(
	// 	&'a self,
	// 	engine: &'a Kismesis,
	// 	predicate: impl Fn(&&Macro) -> bool,
	// ) -> Option<&Macro> {
	// 	self.defined_macros.iter().rfind(&predicate).or_else(|| self
	// 		.template
	// 		.as_ref()
	// 		.and_then(|x| {
	// 			engine
	// 				.get_template(x.clone())
	// 				.unwrap()
	// 				.get_macro_template(engine, &predicate)
	// 		}))
	// }

	#[must_use]
	pub fn get_path_slice<'a>(&'a self, engine: &'a Kismesis) -> Option<&Path> {
		engine.get_file(self.file_id)?.path.as_deref()
	}

	// pub fn get_variable_value<'a>(
	// 	&'a self,
	// 	engine: &'a Kismesis,
	// 	predicate: &str,
	// ) -> VariableOption<&Ranged<Expression>> {
	// 	for var in &self.defined_variables {
	// 		if var.name.value == predicate {
	// 			return VariableOption::Some(&var.value);
	// 		}
	// 	}

	// 	for var in &self.defined_lambdas {
	// 		if var.name.value == predicate {
	// 			match var.value {
	// 				Some(ref value) => return VariableOption::Some(value),
	// 				None => return VariableOption::Unset,
	// 			}
	// 		}
	// 	}

	// 	self.template
	// 		.as_ref()
	// 		.map_or(VariableOption::None, |template| {
	// 			engine
	// 				.get_template(template.clone())
	// 				.unwrap()
	// 				.get_variable_value(engine, predicate)
	// 		})
	// }

	#[must_use]
	pub fn get_macro_scope<'a>(&'a self, engine: &'a Kismesis) -> HashMap<String, Scoped<&Macro>> {
		let mut output = HashMap::new();
		if let Some(template) = self
			.template
			.as_ref()
			.and_then(|x| engine.get_template(x))
			.map(|x| x.get_macro_scope(engine))
		{
			output.extend(template);
		}

		output.extend(
			self.defined_macros
				.iter()
				.map(|x| (x.name.value.clone(), (x, self.file_id))),
		);

		output
	}

	#[must_use]
	pub fn get_variable_scope<'a>(
		&'a self,
		sub_scope: &'a [Self],
		engine: &'a Kismesis,
	) -> HashMap<String, ScopedExpression> {
		let mut out = HashMap::new();

		if let Some(template) = self
			.template
			.as_ref()
			.and_then(|x| engine.get_template(x))
			.map(|x| x.get_variable_scope(sub_scope, engine))
		{
			out.extend(template);
		}

		out.extend(self.defined_lambdas.iter().map(|x| {
			if !sub_scope.is_empty() {
				for scope in sub_scope {
					let find = scope
						.defined_variables
						.iter()
						.rfind(|y| y.name.value == x.name.value);
					if let Some(find) = find {
						return (
							find.name.value.clone(),
							((Some(&find.value), find.name.range.clone()), self.file_id),
						);
					}
				}
			}
			(
				x.name.value.clone(),
				((x.value.as_ref(), x.name.range.clone()), self.file_id),
			)
		}));

		out.extend(self.defined_variables.iter().map(|x| {
			(
				x.name.value.clone(),
				((Some(&x.value), x.name.range.clone()), self.file_id),
			)
		}));

		out.into_iter().collect()
	}

	#[must_use]
	pub fn get_local_macro_scope(&self) -> HashMap<String, Scoped<&Macro>> {
		let mut output = HashMap::new();
		output.extend(
			self.defined_macros
				.iter()
				.map(|x| (x.name.value.clone(), (x, self.file_id))),
		);

		output
	}

	#[must_use]
	pub fn get_local_variable_scope(&self) -> HashMap<String, ScopedExpression> {
		let mut out = HashMap::new();

		out.extend(self.defined_variables.iter().map(|x| {
			(
				x.name.value.clone(),
				((Some(&x.value), x.name.range.clone()), self.file_id),
			)
		}));

		out.into_iter().collect()
	}
}

impl From<Tag> for BodyNodes {
	fn from(value: Tag) -> Self {
		match value {
			Tag::Html(x) => Self::HtmlTag(x),
			Tag::MacroCall(x) => Self::MacroCall(x),
			Tag::MacroDef(x) => Self::MacroDef(x),
			Tag::PlugCall(x) => Self::PlugCall(x),
			Tag::Content(x) => Self::Content(x),
			Tag::Doctype(x) => Self::Doctype(x),
			Tag::If(x) => Self::If(x),
			Tag::For(x) => Self::For(x),
		}
	}
}

impl From<BodyTags> for BodyNodes {
	fn from(value: BodyTags) -> Self {
		match value {
			BodyTags::HtmlTag(x) => Self::HtmlTag(x),
			BodyTags::MacroCall(x) => Self::MacroCall(x),
			BodyTags::PlugCall(x) => Self::PlugCall(x),
			BodyTags::Content(x) => Self::Content(x),
			BodyTags::If(x) => Self::If(x),
			BodyTags::For(x) => Self::For(x),
			BodyTags::Raw(x) => Self::Raw(x),
		}
	}
}

impl From<BodyTags> for HtmlNodes {
	fn from(value: BodyTags) -> Self {
		match value {
			BodyTags::HtmlTag(x) => Self::HtmlTag(x),
			BodyTags::MacroCall(x) => Self::MacroCall(x),
			BodyTags::PlugCall(x) => Self::PlugCall(x),
			BodyTags::Content(x) => Self::Content(x),
			BodyTags::If(x) => Self::If(x),
			BodyTags::For(x) => Self::For(x),
			BodyTags::Raw(x) => Self::Raw(x),
		}
	}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum BinFunc {
	And,
	Or,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum UniFunc {
	Not,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Expression {
	Array(Vec<Ranged<Expression>>),
	BinFunc(BinFunc, Box<Ranged<Expression>>, Box<Ranged<Expression>>),
	Literal(Vec<StringParts>),
	None,
	UniFunc(UniFunc, Box<Ranged<Expression>>),
	Variable(String),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Variable {
	pub name: Ranged<String>,
	pub value: Ranged<Expression>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Lambda {
	pub name: Ranged<String>,
	pub value: Option<Ranged<Expression>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Ranged<T> {
	pub value: T,
	pub range: MultilineRange,
}

impl Ranged<&str> {
	#[must_use]
	pub fn to_own(&self) -> Ranged<String> {
		Ranged {
			value: self.value.to_owned(),
			range: self.range.clone(),
		}
	}
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum MultilineRange {
	Multi(Box<[MultilineRange]>),
	Range(TextPos, TextPos),
	Single(TextPos),
}

impl MultilineRange {
	#[must_use]
	pub fn get_start_line(&self) -> Option<usize> {
		match self {
			Self::Single(x) => Some(x.get_line()),
			Self::Range(start, _) => Some(start.get_line()),
			Self::Multi(x) => x.first().and_then(Self::get_start_line),
		}
	}

	#[must_use]
	pub fn get_end_line(&self) -> Option<usize> {
		match self {
			Self::Single(x) => Some(x.get_line()),
			Self::Range(start, _) => Some(start.get_line()),
			Self::Multi(x) => x.last().and_then(Self::get_end_line),
		}
	}

	#[must_use]
	pub fn is_one_line(&self) -> bool {
		self.get_end_line() == self.get_start_line()
	}
}

impl Macro {
	#[must_use]
	pub fn get_argument_scope(&self, scope: KisTokenId) -> HashMap<String, ScopedExpression> {
		let mut output = HashMap::new();

		output.extend(self.arguments.iter().map(|x| {
			x.value.as_ref().map_or_else(
				|| (x.name.value.clone(), ((None, x.name.range.clone()), scope)),
				|value| {
					(
						x.name.value.clone(),
						((Some(value), x.name.range.clone()), scope),
					)
				},
			)
		}));

		output
	}
}

impl HtmlTag {
	/// Turn all subtags into regular HTML tags
	#[must_use]
	pub fn merge_subtags(mut self) -> Self {
		let mut subtag_stack = self.subtags;
		let Some(top) = subtag_stack.last_mut() else {
			self.subtags = subtag_stack;
			return self;
		};
		top.body = self.body;
		loop {
			let Some(top) = subtag_stack.pop() else { break };
			let Some(top_2) = subtag_stack.last_mut() else {
				subtag_stack.push(top);
				break;
			};
			top_2.body.push(HtmlNodes::HtmlTag(top));
		}

		self.body = subtag_stack.into_iter().map(HtmlNodes::HtmlTag).collect();

		self.subtags = Vec::new();

		self
	}
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct IfTag {
	pub condition: Ranged<Expression>,
	pub body: Vec<HtmlNodes>,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ForTag {
	pub variable: Ranged<String>,
	pub iterator: Ranged<Expression>,
	pub body: Vec<HtmlNodes>,
}

pub trait FillContent {
	fn fill_content(&mut self, number: usize);
}

impl FillContent for TopNodes {
	fn fill_content(&mut self, number: usize) {
		match self {
			Self::HtmlTag(x) => x.fill_content(number),
			Self::MacroCall(x) => x.fill_content(number),
			Self::PlugCall(x) => x.fill_content(number),
			Self::Content(Content {
				position: _,
				content: x @ None,
			}) => *x = Some(number),
			Self::If(x) => x.fill_content(number),
			Self::For(x) => x.fill_content(number),
			Self::Paragraph(x) => x.fill_content(number),
			Self::Raw(_) | Self::Doctype(_) | Self::Content(_) => (),
		}
	}
}

impl FillContent for HtmlNodes {
	fn fill_content(&mut self, number: usize) {
		match self {
			Self::HtmlTag(x) => x.fill_content(number),
			Self::MacroCall(x) => x.fill_content(number),
			Self::PlugCall(x) => x.fill_content(number),
			Self::Content(Content {
				position: _,
				content: x @ None,
			}) => *x = Some(number),
			Self::For(x) => x.fill_content(number),
			Self::Paragraph(x) => x.fill_content(number),
			Self::If(x) => x.fill_content(number),
			Self::Raw(_) | Self::String(_) | Self::Content(_) => (),
		}
	}
}

impl FillContent for HtmlTag {
	fn fill_content(&mut self, number: usize) {
		for node in &mut self.body {
			node.fill_content(number);
		}
	}
}

impl FillContent for Macro {
	fn fill_content(&mut self, number: usize) {
		for node in &mut self.body {
			node.fill_content(number);
		}
	}
}

impl FillContent for PlugCall {
	fn fill_content(&mut self, number: usize) {
		for node in &mut self.body {
			node.fill_content(number);
		}
	}
}

impl FillContent for IfTag {
	fn fill_content(&mut self, number: usize) {
		for node in &mut self.body {
			node.fill_content(number);
		}
	}
}

impl FillContent for ForTag {
	fn fill_content(&mut self, number: usize) {
		for node in &mut self.body {
			node.fill_content(number);
		}
	}
}

impl FillContent for Paragraph {
	fn fill_content(&mut self, number: usize) {
		for node in &mut self.0 {
			node.fill_content(number);
		}
	}
}

impl<T> FillContent for Vec<T>
where
	T: FillContent,
{
	fn fill_content(&mut self, number: usize) {
		self.iter_mut().for_each(|x| x.fill_content(number));
	}
}

impl FillContent for ParsedFile {
	fn fill_content(&mut self, number: usize) {
		self.body.fill_content(number);
	}
}