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
|
//! Hyperlinking.
use super::prelude::*;
use super::TextNode;
use crate::util::EcoString;
/// Link text and other elements to an URL.
#[derive(Debug, Hash)]
pub struct LinkNode {
/// The url the link points to.
pub url: EcoString,
/// How the link is represented.
pub body: Option<Template>,
}
#[class]
impl LinkNode {
/// The fill color of text in the link. Just the surrounding text color
/// if `auto`.
pub const FILL: Smart<Paint> = Smart::Auto;
/// Whether to underline link.
pub const UNDERLINE: bool = true;
fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> {
Ok(Template::show(Self {
url: args.expect::<EcoString>("url")?,
body: args.find()?,
}))
}
}
impl Show for LinkNode {
fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> {
let mut body = styles
.show(self, ctx, [
Value::Str(self.url.clone()),
match &self.body {
Some(body) => Value::Template(body.clone()),
None => Value::None,
},
])?
.or_else(|| self.body.clone())
.unwrap_or_else(|| {
let url = &self.url;
let mut text = url.as_str();
for prefix in ["mailto:", "tel:"] {
text = text.trim_start_matches(prefix);
}
let shorter = text.len() < url.len();
Template::Text(if shorter { text.into() } else { url.clone() })
});
let mut map = StyleMap::new();
map.set(TextNode::LINK, Some(self.url.clone()));
if let Smart::Custom(fill) = styles.get(Self::FILL) {
map.set(TextNode::FILL, fill);
}
if styles.get(Self::UNDERLINE) {
body = body.underlined();
}
Ok(body.styled_with_map(map))
}
}
|