From acae6e2a54f11b27bae343a15d9eff952323fe28 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 17 Feb 2022 23:54:00 +0100 Subject: [PATCH] List label styling --- src/eval/template.rs | 23 +++---- src/library/heading.rs | 12 ++-- src/library/list.rs | 126 ++++++++++++++++++++++++++++------ src/library/mod.rs | 3 + src/library/numbering.rs | 108 +++++++++++++++++++++++++++++ src/library/utility.rs | 65 ------------------ src/parse/scanner.rs | 6 ++ src/parse/tokens.rs | 2 +- src/util/eco_string.rs | 10 +++ tests/ref/markup/enums.png | Bin 7427 -> 26118 bytes tests/typ/markup/enums.typ | 37 ++++++++++ tests/typ/utility/strings.typ | 6 +- 12 files changed, 289 insertions(+), 109 deletions(-) create mode 100644 src/library/numbering.rs diff --git a/src/eval/template.rs b/src/eval/template.rs index 0db63535f..689744524 100644 --- a/src/eval/template.rs +++ b/src/eval/template.rs @@ -13,7 +13,7 @@ use crate::diag::StrResult; use crate::layout::{Layout, LayoutNode}; use crate::library::prelude::*; use crate::library::{ - DecoNode, FlowChild, FlowNode, Labelling, ListItem, ListNode, PageNode, ParChild, + DecoNode, FlowChild, FlowNode, ListItem, ListKind, ListNode, PageNode, ParChild, ParNode, PlaceNode, SpacingKind, TextNode, ORDERED, UNDERLINE, UNORDERED, }; use crate::util::EcoString; @@ -307,14 +307,14 @@ impl<'a> Builder<'a> { builder.staged.push((template, styles)); return Ok(()); } - Template::List(item) if builder.labelling == UNORDERED => { + Template::List(item) if builder.kind == UNORDERED => { builder.wide |= builder.staged.iter().any(|&(t, _)| *t == Template::Parbreak); builder.staged.clear(); builder.items.push(item.clone()); return Ok(()); } - Template::Enum(item) if builder.labelling == ORDERED => { + Template::Enum(item) if builder.kind == ORDERED => { builder.wide |= builder.staged.iter().any(|&(t, _)| *t == Template::Parbreak); builder.staged.clear(); @@ -376,7 +376,7 @@ impl<'a> Builder<'a> { Template::List(item) => { self.list = Some(ListBuilder { styles, - labelling: UNORDERED, + kind: UNORDERED, items: vec![item.clone()], wide: false, staged: vec![], @@ -385,7 +385,7 @@ impl<'a> Builder<'a> { Template::Enum(item) => { self.list = Some(ListBuilder { styles, - labelling: ORDERED, + kind: ORDERED, items: vec![item.clone()], wide: false, staged: vec![], @@ -447,13 +447,12 @@ impl<'a> Builder<'a> { /// Finish the currently built list. fn finish_list(&mut self, vm: &mut Vm) -> TypResult<()> { - let ListBuilder { styles, labelling, items, wide, staged } = - match self.list.take() { - Some(list) => list, - None => return Ok(()), - }; + let ListBuilder { styles, kind, items, wide, staged } = match self.list.take() { + Some(list) => list, + None => return Ok(()), + }; - let template = match labelling { + let template = match kind { UNORDERED => Template::show(ListNode:: { items, wide, start: 1 }), ORDERED | _ => Template::show(ListNode:: { items, wide, start: 1 }), }; @@ -492,7 +491,7 @@ impl<'a> Builder<'a> { /// Builds an unordered or ordered list from items. struct ListBuilder<'a> { styles: StyleChain<'a>, - labelling: Labelling, + kind: ListKind, items: Vec, wide: bool, staged: Vec<(&'a Template, StyleChain<'a>)>, diff --git a/src/library/heading.rs b/src/library/heading.rs index dd78b1471..3438c7b78 100644 --- a/src/library/heading.rs +++ b/src/library/heading.rs @@ -113,9 +113,9 @@ pub enum Leveled { impl Leveled { /// Resolve the value based on the level. pub fn resolve(self, vm: &mut Vm, level: usize) -> TypResult { - match self { - Self::Value(value) => Ok(value), - Self::Mapping(mapping) => Ok(mapping(level)), + Ok(match self { + Self::Value(value) => value, + Self::Mapping(mapping) => mapping(level), Self::Func(func, span) => { let args = Args { span, @@ -125,9 +125,9 @@ impl Leveled { value: Spanned::new(Value::Int(level as i64), span), }], }; - func.call(vm, args)?.cast().at(span) + func.call(vm, args)?.cast().at(span)? } - } + }) } } @@ -138,7 +138,7 @@ impl Cast> for Leveled { fn cast(value: Spanned) -> StrResult { match value.v { - Value::Func(f) => Ok(Self::Func(f, value.span)), + Value::Func(v) => Ok(Self::Func(v, value.span)), v => T::cast(v) .map(Self::Value) .map_err(|msg| with_alternative(msg, "function")), diff --git a/src/library/list.rs b/src/library/list.rs index 1ec89da2f..fe499cb19 100644 --- a/src/library/list.rs +++ b/src/library/list.rs @@ -1,11 +1,13 @@ //! Unordered (bulleted) and ordered (numbered) lists. use super::prelude::*; -use super::{GridNode, ParNode, TextNode, TrackSizing}; +use super::{GridNode, Numbering, ParNode, TextNode, TrackSizing}; + +use crate::parse::Scanner; /// An unordered or ordered list. #[derive(Debug, Hash)] -pub struct ListNode { +pub struct ListNode { /// The individual bulleted or numbered items. pub items: Vec, /// If true, there is paragraph spacing between the items, if false @@ -25,13 +27,15 @@ pub struct ListItem { } #[class] -impl ListNode { +impl ListNode { + /// How the list is labelled. + pub const LABEL: Label = Label::Default; + /// The spacing between the list items of a non-wide list. + pub const SPACING: Linear = Linear::zero(); /// The indentation of each item's label. pub const LABEL_INDENT: Linear = Relative::new(0.0).into(); /// The space between the label and the body of each item. pub const BODY_INDENT: Linear = Relative::new(0.5).into(); - /// The spacing between the list items of a non-wide list. - pub const SPACING: Linear = Linear::zero(); fn construct(_: &mut Vm, args: &mut Args) -> TypResult