diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-04-23 21:55:58 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-04-23 21:55:58 +0200 |
| commit | 04fb8b288aa7c80607da79db7d085a4820b95a9d (patch) | |
| tree | 7ca96d09d511274ebac298c329d5eef53a290d9c /src/library | |
| parent | 7a2cc3e7d29d16c5cf9b5a93a688e14da93c8662 (diff) | |
Show rules with type ascribed object
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/layout/grid.rs | 6 | ||||
| -rw-r--r-- | src/library/math/mod.rs | 20 | ||||
| -rw-r--r-- | src/library/structure/heading.rs | 23 | ||||
| -rw-r--r-- | src/library/structure/list.rs | 49 | ||||
| -rw-r--r-- | src/library/structure/table.rs | 31 | ||||
| -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 |
9 files changed, 168 insertions, 90 deletions
diff --git a/src/library/layout/grid.rs b/src/library/layout/grid.rs index 4908d4d8..afd08aee 100644 --- a/src/library/layout/grid.rs +++ b/src/library/layout/grid.rs @@ -8,7 +8,7 @@ pub struct GridNode { /// Defines sizing of gutter rows and columns between content. pub gutter: Spec<Vec<TrackSizing>>, /// The nodes to be arranged in a grid. - pub children: Vec<LayoutNode>, + pub cells: Vec<LayoutNode>, } #[node] @@ -25,7 +25,7 @@ impl GridNode { column_gutter.unwrap_or_else(|| base_gutter.clone()), row_gutter.unwrap_or(base_gutter), ), - children: args.all()?, + cells: args.all()?, })) } } @@ -41,7 +41,7 @@ impl Layout for GridNode { let layouter = GridLayouter::new( self.tracks.as_deref(), self.gutter.as_deref(), - &self.children, + &self.cells, regions, styles, ); diff --git a/src/library/math/mod.rs b/src/library/math/mod.rs index e6548438..587949d7 100644 --- a/src/library/math/mod.rs +++ b/src/library/math/mod.rs @@ -28,11 +28,21 @@ impl MathNode { } impl Show for MathNode { - fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { - let args = [Value::Str(self.formula.clone()), Value::Bool(self.display)]; - let mut content = styles - .show::<Self, _>(ctx, args)? - .unwrap_or_else(|| Content::Text(self.formula.trim().into())); + fn encode(&self) -> Dict { + dict! { + "formula" => Value::Str(self.formula.clone()), + "display" => Value::Bool(self.display) + } + } + + fn show( + &self, + _: &mut Context, + styles: StyleChain, + realized: Option<Content>, + ) -> TypResult<Content> { + let mut content = + realized.unwrap_or_else(|| Content::Text(self.formula.trim().into())); let mut map = StyleMap::new(); if let Smart::Custom(family) = styles.get(Self::FAMILY) { diff --git a/src/library/structure/heading.rs b/src/library/structure/heading.rs index 07e5e662..a352cc92 100644 --- a/src/library/structure/heading.rs +++ b/src/library/structure/heading.rs @@ -56,21 +56,26 @@ impl HeadingNode { } impl Show for HeadingNode { - fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { + fn encode(&self) -> Dict { + dict! { + "level" => Value::Int(self.level as i64), + "body" => Value::Content(self.body.clone()), + } + } + + fn show( + &self, + ctx: &mut Context, + styles: StyleChain, + realized: Option<Content>, + ) -> TypResult<Content> { macro_rules! resolve { ($key:expr) => { styles.get($key).resolve(ctx, self.level)? }; } - let args = [ - Value::Int(self.level as i64), - Value::Content(self.body.clone()), - ]; - - let mut body = styles - .show::<Self, _>(ctx, args)? - .unwrap_or_else(|| self.body.clone()); + let mut body = realized.unwrap_or_else(|| self.body.clone()); let mut map = StyleMap::new(); map.set(TextNode::SIZE, resolve!(Self::SIZE)); diff --git a/src/library/structure/list.rs b/src/library/structure/list.rs index 10dcfb7b..c59b443d 100644 --- a/src/library/structure/list.rs +++ b/src/library/structure/list.rs @@ -10,9 +10,9 @@ use crate::library::utility::Numbering; pub struct ListNode<const L: ListKind = UNORDERED> { /// Where the list starts. pub start: usize, - /// If true, there is paragraph spacing between the items, if false + /// If false, there is paragraph spacing between the items, if true /// there is list spacing between the items. - pub wide: bool, + pub tight: bool, /// The individual bulleted or numbered items. pub items: Vec<ListItem>, } @@ -55,7 +55,7 @@ impl<const L: ListKind> ListNode<L> { fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> { Ok(Content::show(Self { start: args.named("start")?.unwrap_or(1), - wide: args.named("wide")?.unwrap_or(false), + tight: args.named("tight")?.unwrap_or(true), items: args .all()? .into_iter() @@ -66,30 +66,47 @@ impl<const L: ListKind> ListNode<L> { } impl<const L: ListKind> Show for ListNode<L> { - fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { - let args = self.items.iter().map(|item| Value::Content((*item.body).clone())); - let content = if let Some(content) = styles.show::<Self, _>(ctx, args)? { + fn encode(&self) -> Dict { + dict! { + "start" => Value::Int(self.start as i64), + "tight" => Value::Bool(self.tight), + "items" => Value::Array( + self.items + .iter() + .map(|item| Value::Content((*item.body).clone())) + .collect() + ), + } + } + + fn show( + &self, + ctx: &mut Context, + styles: StyleChain, + realized: Option<Content>, + ) -> TypResult<Content> { + let content = if let Some(content) = realized { content } else { - let mut children = vec![]; + let mut cells = vec![]; let mut number = self.start; let label = styles.get(Self::LABEL); for item in &self.items { number = item.number.unwrap_or(number); - children.push(LayoutNode::default()); - children.push(label.resolve(ctx, L, number)?.pack()); - children.push(LayoutNode::default()); - children.push((*item.body).clone().pack()); + cells.push(LayoutNode::default()); + cells.push(label.resolve(ctx, L, number)?.pack()); + cells.push(LayoutNode::default()); + cells.push((*item.body).clone().pack()); number += 1; } let leading = styles.get(ParNode::LEADING); - let spacing = if self.wide { - styles.get(ParNode::SPACING) - } else { + let spacing = if self.tight { styles.get(Self::SPACING) + } else { + styles.get(ParNode::SPACING) }; let gutter = leading + spacing; @@ -104,7 +121,7 @@ impl<const L: ListKind> Show for ListNode<L> { TrackSizing::Auto, ]), gutter: Spec::with_y(vec![TrackSizing::Relative(gutter.into())]), - children, + cells, }) }; @@ -127,7 +144,7 @@ impl<const L: ListKind> Show for ListNode<L> { impl<const L: ListKind> From<ListItem> for ListNode<L> { fn from(item: ListItem) -> Self { - Self { items: vec![item], wide: false, start: 1 } + Self { items: vec![item], tight: true, start: 1 } } } diff --git a/src/library/structure/table.rs b/src/library/structure/table.rs index 96d3bd5b..aefd01b5 100644 --- a/src/library/structure/table.rs +++ b/src/library/structure/table.rs @@ -9,7 +9,7 @@ pub struct TableNode { /// Defines sizing of gutter rows and columns between content. pub gutter: Spec<Vec<TrackSizing>>, /// The nodes to be arranged in the table. - pub children: Vec<Content>, + pub cells: Vec<Content>, } #[node(showable)] @@ -37,7 +37,7 @@ impl TableNode { column_gutter.unwrap_or_else(|| base_gutter.clone()), row_gutter.unwrap_or(base_gutter), ), - children: args.all()?, + cells: args.all()?, })) } @@ -53,9 +53,24 @@ impl TableNode { } impl Show for TableNode { - fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { - let args = self.children.iter().map(|child| Value::Content(child.clone())); - if let Some(content) = styles.show::<Self, _>(ctx, args)? { + fn encode(&self) -> Dict { + dict! { + "cells" => Value::Array( + self.cells + .iter() + .map(|cell| Value::Content(cell.clone())) + .collect() + ), + } + } + + fn show( + &self, + _: &mut Context, + styles: StyleChain, + realized: Option<Content>, + ) -> TypResult<Content> { + if let Some(content) = realized { return Ok(content); } @@ -65,8 +80,8 @@ impl Show for TableNode { let padding = styles.get(Self::PADDING); let cols = self.tracks.x.len().max(1); - let children = self - .children + let cells = self + .cells .iter() .cloned() .enumerate() @@ -90,7 +105,7 @@ impl Show for TableNode { Ok(Content::block(GridNode { tracks: self.tracks.clone(), gutter: self.gutter.clone(), - children, + cells, })) } } 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(), |
