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
use std::iter::FromIterator;
use std::collections::{HashSet, HashMap};
use std::rc::Rc;
use std::borrow::Cow;
use crate::frontend::data::*;
use crate::frontend::ast::*;
fn match_and_apply_rewrite_rule<'a>(
pattern: Vec<Node<'a>>,
target: Vec<Node<'a>>,
children: Vec<Node<'a>>,
) -> Vec<Node<'a>> {
let mut left: Vec<Node<'a>> = Vec::<Node>::new();
let mut current = children;
while current.len() > 0 && current.len() >= pattern.len() {
let matches = current
.iter()
.zip(pattern.iter())
.all(|(x, y)| x.syntactically_equal(y));
if matches {
left.extend(target.clone());
let _ = current
.drain(..pattern.len())
.collect::<Vec<_>>();
continue;
}
left.push(current.remove(0));
}
left.extend(current);
left
}
fn child_list_passes<'a>(children: Vec<Node<'a>>) -> Vec<Node<'a>> {
fn merge_text_content<'a>(xs: Vec<Node<'a>>) -> Vec<Node<'a>> {
let mut results = Vec::new();
for current in xs.into_iter() {
let left = results
.last_mut()
.and_then(Node::unwrap_string_mut);
if let Some(left) = left {
if let Some(txt) = current.unwrap_string() {
*left = Ann::unannotated(left.data.to_owned() + txt.data.to_owned());
continue;
}
}
results.push(current);
}
results
}
fn block_passes<'a>(xs: Vec<Node<'a>>) -> Vec<Node<'a>> {
merge_text_content(xs)
}
let node = Node::new_fragment(children);
let node = node.transform_children(Rc::new(block_passes));
node.into_fragment()
}
fn node_passes<'a>(node: Node<'a>) -> Node<'a> {
fn apply_rewrite_rules<'a>(tag: Tag<'a>) -> Tag<'a> {
let mut children = tag.children;
for RewriteRule{from, to} in tag.rewrite_rules {
let from = from.unwrap_curly_brace();
let to = to.unwrap_curly_brace();
match (from, to) {
(Some(from), Some(to)) => {
children = match_and_apply_rewrite_rule(
from.clone(),
to.clone(),
children,
);
}
_ => ()
}
}
Tag {
name: tag.name,
parameters: tag.parameters,
children,
rewrite_rules: Vec::new(),
}
}
fn process_tags<'a>(env: NodeEnvironment, mut tag: Tag<'a>) -> Tag<'a> {
let name: &str = &(tag.name.data);
if env.is_default_env() {
tag.children = tag.children
.into_iter()
.flat_map(Node::unblock)
.collect();
}
if name == "note" {
tag.name = Ann::unannotated(Cow::Borrowed("div"));
tag.insert_unannotated_parameter("macro=note");
} else if name == "layout" {
tag.name = Ann::unannotated(Cow::Borrowed("div"));
tag.insert_unannotated_parameter("macro=layout");
}
tag
}
let f = |env: NodeEnvironment, node: Node<'a>| -> Node<'a> {
match node {
Node::Tag(tag) => {
let tag = apply_rewrite_rules(tag);
let tag = process_tags(env, tag);
Node::Tag(tag)
}
node @ Node::Enclosure(_) => node,
node @ Node::String(_) => node,
node @ Node::Ident(_) => node,
node @ Node::InvalidToken(_) => node,
}
};
node.transform(NodeEnvironment::default(), Rc::new(f))
}
pub fn html_canonicalization<'a>(nodes: Vec<Node<'a>>) -> Vec<Node<'a>> {
fn passes<'a>(children: Vec<Node<'a>>) -> Vec<Node<'a>> {
let children = children
.into_iter()
.map(node_passes)
.collect();
child_list_passes(children)
}
let result = passes(nodes);
let result = result
.into_iter()
.map(crate::frontend::pass::math::latex_pass)
.collect::<Vec<_>>();
result
}