about summary refs log tree commit diff
path: root/markup/src/legacy/mod.rs
blob: 562b8774ff7c0b8fac5c0f5e18b223cdf8cef4e1 (plain)
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
mod intra;
mod state;

use std::ops::Deref;

use bempline::{Document, Options};

use state::State;

pub fn process(raw: &str) -> Markup {
	let mut state = State::default();

	for line in raw.lines() {
		state.process_line(line)
	}

	state.done()
}

pub struct Markup {
	content: String,
	fill: Vec<Fill>,
}

impl Markup {
	pub fn fill(&self) -> &[Fill] {
		&self.fill
	}

	pub fn content(&self) -> &str {
		&self.content
	}

	pub fn into_content(self) -> String {
		self.content
	}

	pub fn into_document(self) -> Result<Document, String> {
		let Ok(mut doc) = Document::from_str(&self.content, Options::default()) else {
			return Err(self.content);
		};

		self.fill_document(&mut doc);

		Ok(doc)
	}

	pub fn fill_document(&self, doc: &mut Document) {
		for fill in &self.fill {
			doc.set(&fill.key, &fill.content);
		}
	}
}

impl Deref for Markup {
	type Target = String;

	fn deref(&self) -> &Self::Target {
		&self.content
	}
}

#[derive(Clone, Debug, PartialEq)]
pub struct Fill {
	pub key: String,
	pub content: String,
}

#[cfg(test)]
mod document_test {
	use crate::legacy::process;

	const BASE: &str = "test/markup";
	fn test_files(test: &str) {
		let input_path = format!("{BASE}/{test}/input.html");
		let output_path = format!("{BASE}/{test}/output.html");

		let input = std::fs::read_to_string(input_path).unwrap();
		let output = std::fs::read_to_string(output_path).unwrap();

		let actual = process(&input).into_document().unwrap().compile();

		assert_eq!(actual, output)
	}

	#[test]
	fn inserts_fill() {
		let input = "[@fill main]\nwords!\n[@fill-end]\n{main}";
		let expected = "<p>\nwords!\n</p>";
		let markup = process(input);
		let doc = markup.into_document().unwrap().compile();
		assert_eq!(doc, expected)
	}

	#[test]
	fn fill_restores_paragraphs() {
		test_files("fill paragraphs");
	}

	#[test]
	fn parses_complicated_fill() {
		test_files("complicated fill");
	}
}