diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-02-18 15:02:02 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-02-18 16:57:53 +0100 |
| commit | e01970b20a058ab1b4ebea916f229c9b706c84e4 (patch) | |
| tree | 5c5efc75abd6e607bd45a0602603231edf520503 /src/library/link.rs | |
| parent | 05ec0f993b4a1b8481e494ee16285d23f000872f (diff) | |
Basic show rules
Diffstat (limited to 'src/library/link.rs')
| -rw-r--r-- | src/library/link.rs | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/src/library/link.rs b/src/library/link.rs index 95fff089..41209549 100644 --- a/src/library/link.rs +++ b/src/library/link.rs @@ -10,7 +10,7 @@ pub struct LinkNode { /// The url the link points to. pub url: EcoString, /// How the link is represented. - pub body: Template, + pub body: Option<Template>, } #[class] @@ -22,22 +22,31 @@ impl LinkNode { pub const UNDERLINE: bool = true; fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { - let url = args.expect::<EcoString>("url")?; - let body = args.find()?.unwrap_or_else(|| { - 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() }) - }); - - Ok(Template::show(Self { url, body })) + Ok(Template::show(Self { + url: args.expect::<EcoString>("url")?, + body: args.find()?, + })) } } impl Show for LinkNode { - fn show(&self, _: &mut Vm, styles: StyleChain) -> TypResult<Template> { + fn show(&self, vm: &mut Vm, styles: StyleChain) -> TypResult<Template> { + let mut body = styles + .show(self, vm, [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())); @@ -45,7 +54,6 @@ impl Show for LinkNode { map.set(TextNode::FILL, fill); } - let mut body = self.body.clone(); if styles.get(Self::UNDERLINE) { body = body.underlined(); } |
