diff options
Diffstat (limited to 'src/library/text')
| -rw-r--r-- | src/library/text/deco.rs | 31 | ||||
| -rw-r--r-- | src/library/text/link.rs | 43 | ||||
| -rw-r--r-- | src/library/text/mod.rs | 30 | ||||
| -rw-r--r-- | src/library/text/raw.rs | 25 |
4 files changed, 80 insertions, 49 deletions
diff --git a/src/library/text/deco.rs b/src/library/text/deco.rs index 7481b836..9fe4e65a 100644 --- a/src/library/text/deco.rs +++ b/src/library/text/deco.rs @@ -43,18 +43,25 @@ impl<const L: DecoLine> DecoNode<L> { } impl<const L: DecoLine> Show for DecoNode<L> { - fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { - Ok(styles - .show::<Self, _>(ctx, [Value::Content(self.0.clone())])? - .unwrap_or_else(|| { - self.0.clone().styled(TextNode::DECO, Decoration { - line: L, - stroke: styles.get(Self::STROKE).unwrap_or_default(), - offset: styles.get(Self::OFFSET), - extent: styles.get(Self::EXTENT), - evade: styles.get(Self::EVADE), - }) - })) + fn encode(&self) -> Dict { + dict! { "body" => Value::Content(self.0.clone()) } + } + + fn show( + &self, + _: &mut Context, + styles: StyleChain, + realized: Option<Content>, + ) -> TypResult<Content> { + Ok(realized.unwrap_or_else(|| { + self.0.clone().styled(TextNode::DECO, Decoration { + line: L, + stroke: styles.get(Self::STROKE).unwrap_or_default(), + offset: styles.get(Self::OFFSET), + extent: styles.get(Self::EXTENT), + evade: styles.get(Self::EVADE), + }) + })) } } diff --git a/src/library/text/link.rs b/src/library/text/link.rs index 3ef7011d..d1e5eb8e 100644 --- a/src/library/text/link.rs +++ b/src/library/text/link.rs @@ -28,24 +28,31 @@ impl LinkNode { } impl Show for LinkNode { - fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { - let args = [Value::Str(self.url.clone()), match &self.body { - Some(body) => Value::Content(body.clone()), - None => Value::None, - }]; - - let mut body = styles - .show::<Self, _>(ctx, args)? - .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(); - Content::Text(if shorter { text.into() } else { url.clone() }) - }); + fn encode(&self) -> Dict { + dict! { + "url" => Value::Str(self.url.clone()), + "body" => match &self.body { + Some(body) => Value::Content(body.clone()), + None => Value::None, + }, + } + } + + fn show( + &self, + _: &mut Context, + styles: StyleChain, + realized: Option<Content>, + ) -> TypResult<Content> { + let mut body = realized.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(); + Content::Text(if shorter { text.into() } else { url.clone() }) + }); let mut map = StyleMap::new(); map.set(TextNode::LINK, Some(self.url.clone())); diff --git a/src/library/text/mod.rs b/src/library/text/mod.rs index bde553e2..e477e76d 100644 --- a/src/library/text/mod.rs +++ b/src/library/text/mod.rs @@ -471,10 +471,17 @@ impl StrongNode { } impl Show for StrongNode { - fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { - Ok(styles - .show::<Self, _>(ctx, [Value::Content(self.0.clone())])? - .unwrap_or_else(|| self.0.clone().styled(TextNode::STRONG, Toggle))) + fn encode(&self) -> Dict { + dict! { "body" => Value::Content(self.0.clone()) } + } + + fn show( + &self, + _: &mut Context, + _: StyleChain, + realized: Option<Content>, + ) -> TypResult<Content> { + Ok(realized.unwrap_or_else(|| self.0.clone().styled(TextNode::STRONG, Toggle))) } } @@ -490,9 +497,16 @@ impl EmphNode { } impl Show for EmphNode { - fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { - Ok(styles - .show::<Self, _>(ctx, [Value::Content(self.0.clone())])? - .unwrap_or_else(|| self.0.clone().styled(TextNode::EMPH, Toggle))) + fn encode(&self) -> Dict { + dict! { "body" => Value::Content(self.0.clone()) } + } + + fn show( + &self, + _: &mut Context, + _: StyleChain, + realized: Option<Content>, + ) -> TypResult<Content> { + Ok(realized.unwrap_or_else(|| self.0.clone().styled(TextNode::EMPH, Toggle))) } } diff --git a/src/library/text/raw.rs b/src/library/text/raw.rs index db97da07..cc225bed 100644 --- a/src/library/text/raw.rs +++ b/src/library/text/raw.rs @@ -43,7 +43,19 @@ impl RawNode { } impl Show for RawNode { - fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { + fn encode(&self) -> Dict { + dict! { + "text" => Value::Str(self.text.clone()), + "block" => Value::Bool(self.block) + } + } + + fn show( + &self, + _: &mut Context, + styles: StyleChain, + realized: Option<Content>, + ) -> TypResult<Content> { let lang = styles.get(Self::LANG).as_ref(); let foreground = THEME .settings @@ -52,16 +64,7 @@ impl Show for RawNode { .unwrap_or(Color::BLACK) .into(); - let args = [ - Value::Str(self.text.clone()), - match lang { - Some(lang) => Value::Str(lang.clone()), - None => Value::None, - }, - Value::Bool(self.block), - ]; - - let mut content = if let Some(content) = styles.show::<Self, _>(ctx, args)? { + let mut content = if let Some(content) = realized { content } else if matches!( lang.map(|s| s.to_lowercase()).as_deref(), |
