diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-04-24 14:39:53 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-04-24 14:53:43 +0200 |
| commit | 50e4002a2a65c27f46895103c59cb775ca60d16d (patch) | |
| tree | dbd43c16ae8456536c89d64d2cfff293bb6b1148 /src/library/text | |
| parent | 04fb8b288aa7c80607da79db7d085a4820b95a9d (diff) | |
Split `show` into `realize` and `finalize`
Diffstat (limited to 'src/library/text')
| -rw-r--r-- | src/library/text/deco.rs | 21 | ||||
| -rw-r--r-- | src/library/text/link.rs | 22 | ||||
| -rw-r--r-- | src/library/text/mod.rs | 18 | ||||
| -rw-r--r-- | src/library/text/raw.rs | 32 |
4 files changed, 39 insertions, 54 deletions
diff --git a/src/library/text/deco.rs b/src/library/text/deco.rs index 9fe4e65a..52f8ea80 100644 --- a/src/library/text/deco.rs +++ b/src/library/text/deco.rs @@ -47,20 +47,13 @@ impl<const L: DecoLine> Show for DecoNode<L> { 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), - }) + fn realize(&self, _: &mut Context, styles: StyleChain) -> TypResult<Content> { + Ok(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 d1e5eb8e..710fbc47 100644 --- a/src/library/text/link.rs +++ b/src/library/text/link.rs @@ -38,13 +38,8 @@ impl Show for LinkNode { } } - fn show( - &self, - _: &mut Context, - styles: StyleChain, - realized: Option<Content>, - ) -> TypResult<Content> { - let mut body = realized.or_else(|| self.body.clone()).unwrap_or_else(|| { + fn realize(&self, _: &mut Context, _: StyleChain) -> TypResult<Content> { + Ok(self.body.clone().unwrap_or_else(|| { let url = &self.url; let mut text = url.as_str(); for prefix in ["mailto:", "tel:"] { @@ -52,8 +47,15 @@ impl Show for LinkNode { } let shorter = text.len() < url.len(); Content::Text(if shorter { text.into() } else { url.clone() }) - }); + })) + } + fn finalize( + &self, + _: &mut Context, + styles: StyleChain, + mut realized: Content, + ) -> TypResult<Content> { let mut map = StyleMap::new(); map.set(TextNode::LINK, Some(self.url.clone())); @@ -62,9 +64,9 @@ impl Show for LinkNode { } if styles.get(Self::UNDERLINE) { - body = body.underlined(); + realized = realized.underlined(); } - Ok(body.styled_with_map(map)) + Ok(realized.styled_with_map(map)) } } diff --git a/src/library/text/mod.rs b/src/library/text/mod.rs index e477e76d..3cfbc55d 100644 --- a/src/library/text/mod.rs +++ b/src/library/text/mod.rs @@ -475,13 +475,8 @@ impl Show for StrongNode { 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))) + fn realize(&self, _: &mut Context, _: StyleChain) -> TypResult<Content> { + Ok(self.0.clone().styled(TextNode::STRONG, Toggle)) } } @@ -501,12 +496,7 @@ impl Show for EmphNode { 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))) + fn realize(&self, _: &mut Context, _: StyleChain) -> TypResult<Content> { + Ok(self.0.clone().styled(TextNode::EMPH, Toggle)) } } diff --git a/src/library/text/raw.rs b/src/library/text/raw.rs index cc225bed..13daa1b9 100644 --- a/src/library/text/raw.rs +++ b/src/library/text/raw.rs @@ -50,12 +50,7 @@ impl Show for RawNode { } } - fn show( - &self, - _: &mut Context, - styles: StyleChain, - realized: Option<Content>, - ) -> TypResult<Content> { + fn realize(&self, _: &mut Context, styles: StyleChain) -> TypResult<Content> { let lang = styles.get(Self::LANG).as_ref(); let foreground = THEME .settings @@ -64,9 +59,7 @@ impl Show for RawNode { .unwrap_or(Color::BLACK) .into(); - let mut content = if let Some(content) = realized { - content - } else if matches!( + if matches!( lang.map(|s| s.to_lowercase()).as_deref(), Some("typ" | "typst") ) { @@ -79,7 +72,7 @@ impl Show for RawNode { seq.push(styled(&self.text[range], foreground, style)); }); - Content::sequence(seq) + Ok(Content::sequence(seq)) } else if let Some(syntax) = lang.and_then(|token| SYNTAXES.find_syntax_by_token(&token)) { @@ -95,11 +88,18 @@ impl Show for RawNode { } } - Content::sequence(seq) + Ok(Content::sequence(seq)) } else { - Content::Text(self.text.clone()) - }; + Ok(Content::Text(self.text.clone())) + } + } + fn finalize( + &self, + _: &mut Context, + styles: StyleChain, + mut realized: Content, + ) -> TypResult<Content> { let mut map = StyleMap::new(); map.set(TextNode::OVERHANG, false); map.set(TextNode::HYPHENATE, Smart::Custom(Hyphenate(false))); @@ -109,13 +109,13 @@ impl Show for RawNode { map.set_family(family.clone(), styles); } - content = content.styled_with_map(map); + realized = realized.styled_with_map(map); if self.block { - content = Content::Block(content.pack()); + realized = Content::block(realized); } - Ok(content) + Ok(realized) } } |
