summaryrefslogtreecommitdiff
path: root/library/src/text
diff options
context:
space:
mode:
Diffstat (limited to 'library/src/text')
-rw-r--r--library/src/text/deco.rs27
-rw-r--r--library/src/text/link.rs52
-rw-r--r--library/src/text/mod.rs44
-rw-r--r--library/src/text/raw.rs26
-rw-r--r--library/src/text/shift.rs16
5 files changed, 83 insertions, 82 deletions
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<const L: DecoLine> DecoNode<L> {
fn construct(_: &mut Vm, args: &mut Args) -> SourceResult<Content> {
Ok(Self(args.expect("body")?).pack())
}
-}
-
-impl<const L: DecoLine> Show for DecoNode<L> {
- fn unguard_parts(&self, sel: Selector) -> Content {
- Self(self.0.unguard(sel)).pack()
- }
fn field(&self, name: &str) -> Option<Value> {
match name {
@@ -50,12 +44,14 @@ impl<const L: DecoLine> Show for DecoNode<L> {
_ => None,
}
}
+}
+
+impl<const L: DecoLine> Show for DecoNode<L> {
+ fn unguard_parts(&self, sel: Selector) -> Content {
+ Self(self.0.unguard(sel)).pack()
+ }
- fn realize(
- &self,
- _: Tracked<dyn World>,
- styles: StyleChain,
- ) -> SourceResult<Content> {
+ fn show(&self, _: Tracked<dyn World>, styles: StyleChain) -> SourceResult<Content> {
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<Self>;
+
+ 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<Value> {
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<dyn World>, _: StyleChain) -> SourceResult<Content> {
- 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<dyn World>, _: StyleChain) -> SourceResult<Content> {
+ 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<dyn World>,
@@ -83,6 +81,8 @@ impl Show for LinkNode {
mut realized: Content,
) -> SourceResult<Content> {
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<EcoString>) -> 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<Content> {
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<Value> {
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<dyn World>, _: StyleChain) -> SourceResult<Content> {
+ fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> {
Ok(self.0.clone().styled(TextNode::BOLD, Toggle))
}
}
@@ -514,12 +521,6 @@ impl EmphNode {
fn construct(_: &mut Vm, args: &mut Args) -> SourceResult<Content> {
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<Value> {
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<dyn World>, _: StyleChain) -> SourceResult<Content> {
+ fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> {
Ok(self.0.clone().styled(TextNode::ITALIC, Toggle))
}
}
@@ -544,12 +551,3 @@ impl Fold for Toggle {
!outer
}
}
-
-impl Fold for Decoration {
- type Output = Vec<Self>;
-
- 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<Value> {
match name {
@@ -55,12 +49,14 @@ impl Show for RawNode {
_ => None,
}
}
+}
- fn realize(
- &self,
- _: Tracked<dyn World>,
- styles: StyleChain,
- ) -> SourceResult<Content> {
+impl Show for RawNode {
+ fn unguard_parts(&self, _: Selector) -> Content {
+ Self { text: self.text.clone(), ..*self }.pack()
+ }
+
+ fn show(&self, _: Tracked<dyn World>, styles: StyleChain) -> SourceResult<Content> {
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<dyn World>,
@@ -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<const S: ShiftKind> ShiftNode<S> {
fn construct(_: &mut Vm, args: &mut Args) -> SourceResult<Content> {
Ok(Self(args.expect("body")?).pack())
}
-}
-
-impl<const S: ShiftKind> Show for ShiftNode<S> {
- fn unguard_parts(&self, _: Selector) -> Content {
- Self(self.0.clone()).pack()
- }
fn field(&self, name: &str) -> Option<Value> {
match name {
@@ -46,8 +40,14 @@ impl<const S: ShiftKind> Show for ShiftNode<S> {
_ => None,
}
}
+}
+
+impl<const S: ShiftKind> Show for ShiftNode<S> {
+ fn unguard_parts(&self, _: Selector) -> Content {
+ Self(self.0.clone()).pack()
+ }
- fn realize(
+ fn show(
&self,
world: Tracked<dyn World>,
styles: StyleChain,
@@ -56,7 +56,7 @@ impl<const S: ShiftKind> Show for ShiftNode<S> {
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));
}
}
};