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
|
use std::{borrow::Cow, rc::Rc};
use crate::lineup::InterlinkSolver;
pub struct Document<'raw> {
pub(crate) nodes: Vec<TreeNode<'raw>>,
pub(crate) solver: Rc<dyn InterlinkSolver>,
}
impl<'raw> Document<'raw> {
pub fn set_interlink_solver(&mut self, solver: Rc<dyn InterlinkSolver>) {
self.solver = solver;
}
pub fn into_html(self) -> String {
TreeNode::html_nodes(&self.nodes, Rc::clone(&self.solver))
}
pub fn as_text(&self) -> String {
TreeNode::text_nodes(&self.nodes, Rc::clone(&self.solver))
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum TreeNode<'raw> {
Text(&'raw str),
Interlink {
children: Vec<TreeNode<'raw>>,
},
Link {
kind: LinkKind,
name: Option<Vec<TreeNode<'raw>>>,
children: Vec<TreeNode<'raw>>,
},
StyledText {
style: NodeStyle,
children: Vec<TreeNode<'raw>>,
},
ColoredText {
color: &'raw str,
children: Vec<TreeNode<'raw>>,
},
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum LinkKind {
External,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum NodeStyle {
Bold,
Code,
Italic,
}
impl<'raw> TreeNode<'raw> {
pub fn html(&self, solver: Rc<dyn InterlinkSolver>) -> Cow<'raw, str> {
match self {
TreeNode::Text(str) => Cow::Borrowed(str),
TreeNode::Interlink { children } => {
let txt = Self::text_nodes(&children, Rc::clone(&solver));
let title = solver.link_title(&txt);
let location = solver.link_location(&txt);
Cow::Owned(format!("<a href=\"{location}\">{title}</a>"))
}
TreeNode::Link {
kind: _,
name,
children,
} => {
let txt = Self::text_nodes(&children, Rc::clone(&solver));
let title = match name.as_deref() {
None => Cow::Borrowed(txt.as_str()),
Some(name) => {
let txt = Self::text_nodes(name, Rc::clone(&solver));
Cow::Owned(txt)
}
};
let location = txt.as_str();
Cow::Owned(format!("<a href=\"{location}\">{title}</a>"))
}
TreeNode::StyledText { style, children } => {
let (open, close) = match style {
NodeStyle::Bold => ("<b>", "</b>"),
NodeStyle::Code => ("<code>", "</code>"),
NodeStyle::Italic => ("<i>", "</i>"),
};
let inner = Self::html_nodes(children.as_slice(), solver);
Cow::Owned(format!("{open}{inner}{close}"))
}
TreeNode::ColoredText { color, children } => {
let inner = Self::html_nodes(children.as_slice(), solver);
Cow::Owned(format!("<span style='color: {color}'>{inner}</span>"))
}
}
}
pub fn text(&self, solver: Rc<dyn InterlinkSolver>) -> Cow<'raw, str> {
match self {
TreeNode::Text(str) => Cow::Borrowed(str),
TreeNode::Interlink { children } => {
let text = Self::text_nodes(children, Rc::clone(&solver));
Cow::Owned(solver.link_title(&text).into_owned())
}
TreeNode::Link {
kind: _,
name,
children,
} => match name.as_deref() {
None => {
let text = Self::text_nodes(children, Rc::clone(&solver));
Cow::Owned(solver.link_title(&text).into_owned())
}
Some(name) => {
let text = Self::text_nodes(name, Rc::clone(&solver));
Cow::Owned(solver.link_title(&text).into_owned())
}
},
TreeNode::StyledText { style: _, children } => {
Cow::Owned(Self::text_nodes(children, solver))
}
TreeNode::ColoredText { color: _, children } => {
Cow::Owned(Self::text_nodes(children, solver))
}
}
}
fn text_nodes(nodes: &[TreeNode<'raw>], solver: Rc<dyn InterlinkSolver>) -> String {
let mut ret = String::new();
for node in nodes {
let node_txt = &node.text(Rc::clone(&solver));
if !node_txt.is_empty() {
ret.push_str(node_txt);
}
}
ret
}
fn html_nodes(nodes: &[TreeNode<'raw>], solver: Rc<dyn InterlinkSolver>) -> String {
let mut ret = String::new();
for node in nodes {
ret.push_str(&node.html(Rc::clone(&solver)));
}
ret
}
}
|