summaryrefslogtreecommitdiff
path: root/library/src/layout
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-02-13 16:51:30 +0100
committerLaurenz <laurmaedje@gmail.com>2023-02-13 16:54:05 +0100
commit05c8c6045cd831a98776b418a7b176af8c6ec546 (patch)
treef0c59c273f98d83210c7ed2a01e6c1d81023287e /library/src/layout
parent8f68bc7a8ea619af12d95bec7025bd9f3a86625f (diff)
Configurable numbering for nested enums
Diffstat (limited to 'library/src/layout')
-rw-r--r--library/src/layout/enum.rs64
1 files changed, 61 insertions, 3 deletions
diff --git a/library/src/layout/enum.rs b/library/src/layout/enum.rs
index 585b20e5..9b7ea199 100644
--- a/library/src/layout/enum.rs
+++ b/library/src/layout/enum.rs
@@ -3,6 +3,7 @@ use std::str::FromStr;
use crate::compute::{Numbering, NumberingPattern};
use crate::layout::{BlockNode, GridNode, ParNode, Sizing, Spacing};
use crate::prelude::*;
+use crate::text::TextNode;
/// # Numbered List
/// A numbered list.
@@ -105,10 +106,16 @@ impl EnumNode {
/// How to number the enumeration. Accepts a
/// [numbering pattern or function]($func/numbering).
///
+ /// If the numbering pattern contains multiple counting symbols, they apply
+ /// to nested enums. If given a function, the function receives one argument
+ /// if `full` is `{false}` and multiple arguments if `full` is `{true}`.
+ ///
/// ```example
- /// #set enum(numbering: "(a)")
+ /// #set enum(numbering: "1.a)")
/// + Different
/// + Numbering
+ /// + Nested
+ /// + Items
/// + Style
///
/// #set enum(numbering: n => super[#n])
@@ -119,6 +126,20 @@ impl EnumNode {
pub const NUMBERING: Numbering =
Numbering::Pattern(NumberingPattern::from_str("1.").unwrap());
+ /// Whether to display the full numbering, including the numbers of
+ /// all parent enumerations.
+ ///
+ /// Defaults to `{false}`.
+ ///
+ /// ```example
+ /// #set enum(numbering: "1.a)", full: true)
+ /// + Cook
+ /// + Heat water
+ /// + Add integredients
+ /// + Eat
+ /// ```
+ pub const FULL: bool = false;
+
/// The indentation of each item's label.
#[property(resolve)]
pub const INDENT: Length = Length::zero();
@@ -132,6 +153,10 @@ impl EnumNode {
/// If set to `{auto}` uses the spacing [below blocks]($func/block.below).
pub const SPACING: Smart<Spacing> = Smart::Auto;
+ /// The numbers of parent items.
+ #[property(skip, fold)]
+ const PARENTS: Parent = vec![];
+
fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> {
let mut number: NonZeroUsize =
args.named("start")?.unwrap_or(NonZeroUsize::new(1).unwrap());
@@ -193,13 +218,34 @@ impl Layout for EnumNode {
let mut cells = vec![];
let mut number = NonZeroUsize::new(1).unwrap();
+ let mut parents = styles.get(Self::PARENTS);
+ let full = styles.get(Self::FULL);
+
for ((n, item), map) in self.items.iter() {
number = n.unwrap_or(number);
- let resolved = numbering.apply(vt.world(), &[number])?.display();
+
+ let resolved = if full {
+ parents.push(number);
+ let content = numbering.apply(vt.world(), &parents)?.display();
+ parents.pop();
+ content
+ } else {
+ match numbering {
+ Numbering::Pattern(pattern) => {
+ TextNode::packed(pattern.apply_kth(parents.len(), number))
+ }
+ other => other.apply(vt.world(), &[number])?.display(),
+ }
+ };
+
cells.push(Content::empty());
cells.push(resolved.styled_with_map(map.clone()));
cells.push(Content::empty());
- cells.push(item.clone().styled_with_map(map.clone()));
+ cells.push(
+ item.clone()
+ .styled_with_map(map.clone())
+ .styled(Self::PARENTS, Parent(number)),
+ );
number = number.saturating_add(1);
}
@@ -216,3 +262,15 @@ impl Layout for EnumNode {
.layout(vt, styles, regions)
}
}
+
+#[derive(Debug, Clone, Hash)]
+struct Parent(NonZeroUsize);
+
+impl Fold for Parent {
+ type Output = Vec<NonZeroUsize>;
+
+ fn fold(self, mut outer: Self::Output) -> Self::Output {
+ outer.push(self.0);
+ outer
+ }
+}