summaryrefslogtreecommitdiff
path: root/src/library/structure
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-04-23 21:55:58 +0200
committerLaurenz <laurmaedje@gmail.com>2022-04-23 21:55:58 +0200
commit04fb8b288aa7c80607da79db7d085a4820b95a9d (patch)
tree7ca96d09d511274ebac298c329d5eef53a290d9c /src/library/structure
parent7a2cc3e7d29d16c5cf9b5a93a688e14da93c8662 (diff)
Show rules with type ascribed object
Diffstat (limited to 'src/library/structure')
-rw-r--r--src/library/structure/heading.rs23
-rw-r--r--src/library/structure/list.rs49
-rw-r--r--src/library/structure/table.rs31
3 files changed, 70 insertions, 33 deletions
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,
}))
}
}