From efd1853d069fbd1476e82d015da4d0d04cfaccc0 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 7 Nov 2022 12:21:12 +0100 Subject: Show it! - New show rule syntax - Set if syntax - Removed wrap syntax --- library/src/text/deco.rs | 27 ++++++++++++++---------- library/src/text/link.rs | 52 +++++++++++++++++++++++------------------------ library/src/text/mod.rs | 44 +++++++++++++++++++-------------------- library/src/text/raw.rs | 26 +++++++++++------------- library/src/text/shift.rs | 16 +++++++-------- 5 files changed, 83 insertions(+), 82 deletions(-) (limited to 'library/src/text') diff --git a/library/src/text/deco.rs b/library/src/text/deco.rs index 10f3db38..fa0f05a7 100644 --- a/library/src/text/deco.rs +++ b/library/src/text/deco.rs @@ -37,12 +37,6 @@ impl DecoNode { fn construct(_: &mut Vm, args: &mut Args) -> SourceResult { Ok(Self(args.expect("body")?).pack()) } -} - -impl Show for DecoNode { - fn unguard_parts(&self, sel: Selector) -> Content { - Self(self.0.unguard(sel)).pack() - } fn field(&self, name: &str) -> Option { match name { @@ -50,12 +44,14 @@ impl Show for DecoNode { _ => None, } } +} + +impl Show for DecoNode { + fn unguard_parts(&self, sel: Selector) -> Content { + Self(self.0.unguard(sel)).pack() + } - fn realize( - &self, - _: Tracked, - styles: StyleChain, - ) -> SourceResult { + fn show(&self, _: Tracked, styles: StyleChain) -> SourceResult { Ok(self.0.clone().styled( TextNode::DECO, Decoration { @@ -81,6 +77,15 @@ pub(super) struct Decoration { pub evade: bool, } +impl Fold for Decoration { + type Output = Vec; + + fn fold(self, mut outer: Self::Output) -> Self::Output { + outer.insert(0, self); + outer + } +} + /// A kind of decorative line. pub type DecoLine = usize; diff --git a/library/src/text/link.rs b/library/src/text/link.rs index 82abe5cd..4312559e 100644 --- a/library/src/text/link.rs +++ b/library/src/text/link.rs @@ -17,7 +17,7 @@ impl LinkNode { } } -#[node(Show)] +#[node(Show, Finalize)] impl LinkNode { /// The fill color of text in the link. Just the surrounding text color /// if `auto`. @@ -33,16 +33,6 @@ impl LinkNode { }; Ok(Self { dest, body }.pack()) } -} - -impl Show for LinkNode { - fn unguard_parts(&self, sel: Selector) -> Content { - Self { - dest: self.dest.clone(), - body: self.body.as_ref().map(|body| body.unguard(sel)), - } - .pack() - } fn field(&self, name: &str) -> Option { match name { @@ -57,25 +47,33 @@ impl Show for LinkNode { _ => None, } } +} + +impl Show for LinkNode { + fn unguard_parts(&self, sel: Selector) -> Content { + Self { + dest: self.dest.clone(), + body: self.body.as_ref().map(|body| body.unguard(sel)), + } + .pack() + } - fn realize(&self, _: Tracked, _: StyleChain) -> SourceResult { - Ok(self - .body - .clone() - .unwrap_or_else(|| match &self.dest { - Destination::Url(url) => { - let mut text = url.as_str(); - for prefix in ["mailto:", "tel:"] { - text = text.trim_start_matches(prefix); - } - let shorter = text.len() < url.len(); - TextNode(if shorter { text.into() } else { url.clone() }).pack() + fn show(&self, _: Tracked, _: StyleChain) -> SourceResult { + Ok(self.body.clone().unwrap_or_else(|| match &self.dest { + Destination::Url(url) => { + let mut text = url.as_str(); + for prefix in ["mailto:", "tel:"] { + text = text.trim_start_matches(prefix); } - Destination::Internal(_) => Content::empty(), - }) - .styled(TextNode::LINK, Some(self.dest.clone()))) + let shorter = text.len() < url.len(); + TextNode::packed(if shorter { text.into() } else { url.clone() }) + } + Destination::Internal(_) => Content::empty(), + })) } +} +impl Finalize for LinkNode { fn finalize( &self, _: Tracked, @@ -83,6 +81,8 @@ impl Show for LinkNode { mut realized: Content, ) -> SourceResult { let mut map = StyleMap::new(); + map.set(TextNode::LINK, Some(self.dest.clone())); + if let Smart::Custom(fill) = styles.get(Self::FILL) { map.set(TextNode::FILL, fill); } diff --git a/library/src/text/mod.rs b/library/src/text/mod.rs index 6643f821..86c6884a 100644 --- a/library/src/text/mod.rs +++ b/library/src/text/mod.rs @@ -28,6 +28,13 @@ use crate::prelude::*; #[derive(Debug, Clone, Hash)] pub struct TextNode(pub EcoString); +impl TextNode { + /// Create a new packed text node. + pub fn packed(text: impl Into) -> Content { + Self(text.into()).pack() + } +} + #[node] impl TextNode { /// A prioritized sequence of font families. @@ -486,12 +493,6 @@ impl StrongNode { fn construct(_: &mut Vm, args: &mut Args) -> SourceResult { Ok(Self(args.expect("body")?).pack()) } -} - -impl Show for StrongNode { - fn unguard_parts(&self, sel: Selector) -> Content { - Self(self.0.unguard(sel)).pack() - } fn field(&self, name: &str) -> Option { match name { @@ -499,8 +500,14 @@ impl Show for StrongNode { _ => None, } } +} + +impl Show for StrongNode { + fn unguard_parts(&self, sel: Selector) -> Content { + Self(self.0.unguard(sel)).pack() + } - fn realize(&self, _: Tracked, _: StyleChain) -> SourceResult { + fn show(&self, _: Tracked, _: StyleChain) -> SourceResult { Ok(self.0.clone().styled(TextNode::BOLD, Toggle)) } } @@ -514,12 +521,6 @@ impl EmphNode { fn construct(_: &mut Vm, args: &mut Args) -> SourceResult { Ok(Self(args.expect("body")?).pack()) } -} - -impl Show for EmphNode { - fn unguard_parts(&self, sel: Selector) -> Content { - Self(self.0.unguard(sel)).pack() - } fn field(&self, name: &str) -> Option { match name { @@ -527,8 +528,14 @@ impl Show for EmphNode { _ => None, } } +} + +impl Show for EmphNode { + fn unguard_parts(&self, sel: Selector) -> Content { + Self(self.0.unguard(sel)).pack() + } - fn realize(&self, _: Tracked, _: StyleChain) -> SourceResult { + fn show(&self, _: Tracked, _: StyleChain) -> SourceResult { Ok(self.0.clone().styled(TextNode::ITALIC, Toggle)) } } @@ -544,12 +551,3 @@ impl Fold for Toggle { !outer } } - -impl Fold for Decoration { - type Output = Vec; - - fn fold(self, mut outer: Self::Output) -> Self::Output { - outer.insert(0, self); - outer - } -} diff --git a/library/src/text/raw.rs b/library/src/text/raw.rs index 31f1517e..5a98cf3b 100644 --- a/library/src/text/raw.rs +++ b/library/src/text/raw.rs @@ -19,7 +19,7 @@ pub struct RawNode { pub block: bool, } -#[node(Show)] +#[node(Show, Finalize)] impl RawNode { /// The language to syntax-highlight in. #[property(referenced)] @@ -41,12 +41,6 @@ impl RawNode { } .pack()) } -} - -impl Show for RawNode { - fn unguard_parts(&self, _: Selector) -> Content { - Self { text: self.text.clone(), ..*self }.pack() - } fn field(&self, name: &str) -> Option { match name { @@ -55,12 +49,14 @@ impl Show for RawNode { _ => None, } } +} - fn realize( - &self, - _: Tracked, - styles: StyleChain, - ) -> SourceResult { +impl Show for RawNode { + fn unguard_parts(&self, _: Selector) -> Content { + Self { text: self.text.clone(), ..*self }.pack() + } + + fn show(&self, _: Tracked, styles: StyleChain) -> SourceResult { let lang = styles.get(Self::LANG).as_ref().map(|s| s.to_lowercase()); let foreground = THEME .settings @@ -100,7 +96,7 @@ impl Show for RawNode { Content::sequence(seq) } else { - TextNode(self.text.clone()).pack() + TextNode::packed(self.text.clone()) }; if self.block { @@ -114,7 +110,9 @@ impl Show for RawNode { Ok(realized.styled_with_map(map)) } +} +impl Finalize for RawNode { fn finalize( &self, _: Tracked, @@ -134,7 +132,7 @@ impl Show for RawNode { /// Style a piece of text with a syntect style. fn styled(piece: &str, foreground: Paint, style: Style) -> Content { - let mut body = TextNode(piece.into()).pack(); + let mut body = TextNode::packed(piece); let paint = style.foreground.into(); if paint != foreground { diff --git a/library/src/text/shift.rs b/library/src/text/shift.rs index 1117cc00..0f654b5a 100644 --- a/library/src/text/shift.rs +++ b/library/src/text/shift.rs @@ -33,12 +33,6 @@ impl ShiftNode { fn construct(_: &mut Vm, args: &mut Args) -> SourceResult { Ok(Self(args.expect("body")?).pack()) } -} - -impl Show for ShiftNode { - fn unguard_parts(&self, _: Selector) -> Content { - Self(self.0.clone()).pack() - } fn field(&self, name: &str) -> Option { match name { @@ -46,8 +40,14 @@ impl Show for ShiftNode { _ => None, } } +} + +impl Show for ShiftNode { + fn unguard_parts(&self, _: Selector) -> Content { + Self(self.0.clone()).pack() + } - fn realize( + fn show( &self, world: Tracked, styles: StyleChain, @@ -56,7 +56,7 @@ impl Show for ShiftNode { if styles.get(Self::TYPOGRAPHIC) { if let Some(text) = search_text(&self.0, S) { if is_shapable(world, &text, styles) { - transformed = Some(TextNode(text).pack()); + transformed = Some(TextNode::packed(text)); } } }; -- cgit v1.2.3