diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-03-09 14:17:24 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-03-09 14:42:14 +0100 |
| commit | c38d72383d2068361d635d6c1c78ba97aa917801 (patch) | |
| tree | e758418a2d704d69dee88faf4a9a9c69b25b47ca /library/src/meta | |
| parent | d7a65fa26d131179d9d82226e5ee1b562084e48a (diff) | |
Make all optional fields settable
Diffstat (limited to 'library/src/meta')
| -rw-r--r-- | library/src/meta/document.rs | 17 | ||||
| -rw-r--r-- | library/src/meta/heading.rs | 24 | ||||
| -rw-r--r-- | library/src/meta/link.rs | 33 | ||||
| -rw-r--r-- | library/src/meta/outline.rs | 25 |
4 files changed, 43 insertions, 56 deletions
diff --git a/library/src/meta/document.rs b/library/src/meta/document.rs index 1325d85b..8da3c731 100644 --- a/library/src/meta/document.rs +++ b/library/src/meta/document.rs @@ -16,20 +16,17 @@ use crate::prelude::*; /// Category: meta #[node(LayoutRoot)] pub struct DocumentNode { - /// The page runs. - #[variadic] - pub children: Vec<Content>, - /// The document's title. This is often rendered as the title of the /// PDF viewer window. - #[settable] - #[default] pub title: Option<EcoString>, /// The document's authors. - #[settable] - #[default] pub author: Author, + + /// The page runs. + #[internal] + #[variadic] + pub children: Vec<Content>, } impl LayoutRoot for DocumentNode { @@ -58,8 +55,8 @@ impl LayoutRoot for DocumentNode { Ok(Document { pages, - title: Self::title_in(styles), - author: Self::author_in(styles).0, + title: self.title(styles), + author: self.author(styles).0, }) } } diff --git a/library/src/meta/heading.rs b/library/src/meta/heading.rs index f0107a6a..09cbc8b1 100644 --- a/library/src/meta/heading.rs +++ b/library/src/meta/heading.rs @@ -42,13 +42,7 @@ use crate::text::{TextNode, TextSize}; /// Category: meta #[node(Prepare, Show, Finalize)] pub struct HeadingNode { - /// The heading's title. - #[positional] - #[required] - pub body: Content, - /// The logical nesting depth of the heading, starting from one. - #[named] #[default(NonZeroUsize::new(1).unwrap())] pub level: NonZeroUsize, @@ -62,8 +56,6 @@ pub struct HeadingNode { /// == A subsection /// === A sub-subsection /// ``` - #[settable] - #[default] pub numbering: Option<Numbering>, /// Whether the heading should appear in the outline. @@ -78,9 +70,13 @@ pub struct HeadingNode { /// This heading does not appear /// in the outline. /// ``` - #[settable] #[default(true)] pub outlined: bool, + + /// The heading's title. + #[positional] + #[required] + pub body: Content, } impl Prepare for HeadingNode { @@ -106,11 +102,11 @@ impl Prepare for HeadingNode { } let mut numbers = Value::None; - if let Some(numbering) = Self::numbering_in(styles) { + if let Some(numbering) = self.numbering(styles) { numbers = numbering.apply(vt.world(), counter.advance(self))?; } - this.push_field("outlined", Value::Bool(Self::outlined_in(styles))); + this.push_field("outlined", Value::Bool(self.outlined(styles))); this.push_field("numbers", numbers); let meta = Meta::Node(my_id, this.clone()); @@ -132,8 +128,8 @@ impl Show for HeadingNode { } impl Finalize for HeadingNode { - fn finalize(&self, realized: Content) -> Content { - let level = self.level().get(); + fn finalize(&self, realized: Content, styles: StyleChain) -> Content { + let level = self.level(styles).get(); let scale = match level { 1 => 1.4, 2 => 1.2, @@ -165,7 +161,7 @@ impl HeadingCounter { /// Advance the counter and return the numbers for the given heading. pub fn advance(&mut self, heading: &HeadingNode) -> &[NonZeroUsize] { - let level = heading.level().get(); + let level = heading.level(StyleChain::default()).get(); if self.0.len() >= level { self.0[level - 1] = self.0[level - 1].saturating_add(1); diff --git a/library/src/meta/link.rs b/library/src/meta/link.rs index 32b70153..d4d4d8ca 100644 --- a/library/src/meta/link.rs +++ b/library/src/meta/link.rs @@ -25,7 +25,7 @@ use crate::text::{Hyphenate, TextNode}; /// /// Display: Link /// Category: meta -#[node(Construct, Show, Finalize)] +#[node(Show, Finalize)] pub struct LinkNode { /// The destination the link points to. /// @@ -48,6 +48,10 @@ pub struct LinkNode { /// #[positional] #[required] + #[parse( + let dest = args.expect::<Destination>("destination")?; + dest.clone() + )] pub dest: Destination, /// How the link is represented. @@ -56,7 +60,14 @@ pub struct LinkNode { /// parameter can be omitted. In this case, the URL will be shown as the /// link. #[positional] - #[default] + #[required] + #[parse(match &dest { + Destination::Url(url) => match args.eat()? { + Some(body) => body, + None => body_from_url(url), + }, + Destination::Internal(_) => args.expect("body")?, + })] pub body: Content, } @@ -64,21 +75,7 @@ impl LinkNode { /// Create a link node from a URL with its bare text. pub fn from_url(url: EcoString) -> Self { let body = body_from_url(&url); - Self::new(Destination::Url(url)).with_body(body) - } -} - -impl Construct for LinkNode { - fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> { - let dest = args.expect::<Destination>("destination")?; - let body = match &dest { - Destination::Url(url) => match args.eat()? { - Some(body) => body, - None => body_from_url(url), - }, - Destination::Internal(_) => args.expect("body")?, - }; - Ok(Self::new(dest).with_body(body).pack()) + Self::new(Destination::Url(url), body) } } @@ -89,7 +86,7 @@ impl Show for LinkNode { } impl Finalize for LinkNode { - fn finalize(&self, realized: Content) -> Content { + fn finalize(&self, realized: Content, _: StyleChain) -> Content { realized .styled(MetaNode::set_data(vec![Meta::Link(self.dest())])) .styled(TextNode::set_hyphenate(Hyphenate(Smart::Custom(false)))) diff --git a/library/src/meta/outline.rs b/library/src/meta/outline.rs index 3ccc991e..9e5614aa 100644 --- a/library/src/meta/outline.rs +++ b/library/src/meta/outline.rs @@ -30,14 +30,11 @@ pub struct OutlineNode { /// language]($func/text.lang) will be used. This is the default. /// - When set to `{none}`, the outline will not have a title. /// - A custom title can be set by passing content. - #[settable] #[default(Some(Smart::Auto))] pub title: Option<Smart<Content>>, /// The maximum depth up to which headings are included in the outline. When /// this argument is `{none}`, all headings are included. - #[settable] - #[default] pub depth: Option<NonZeroUsize>, /// Whether to indent the subheadings to align the start of their numbering @@ -57,7 +54,6 @@ pub struct OutlineNode { /// == Products /// #lorem(10) /// ``` - #[settable] #[default(false)] pub indent: bool, @@ -69,7 +65,6 @@ pub struct OutlineNode { /// /// = A New Beginning /// ``` - #[settable] #[default(Some(RepeatNode::new(TextNode::packed(".")).pack()))] pub fill: Option<Content>, } @@ -102,7 +97,7 @@ impl Show for OutlineNode { styles: StyleChain, ) -> SourceResult<Content> { let mut seq = vec![ParbreakNode::new().pack()]; - if let Some(title) = Self::title_in(styles) { + if let Some(title) = self.title(styles) { let title = title.clone().unwrap_or_else(|| { TextNode::packed(match TextNode::lang_in(styles) { Lang::GERMAN => "Inhaltsverzeichnis", @@ -112,14 +107,15 @@ impl Show for OutlineNode { seq.push( HeadingNode::new(title) - .pack() - .styled(HeadingNode::set_numbering(None)) - .styled(HeadingNode::set_outlined(false)), + .with_level(NonZeroUsize::new(1).unwrap()) + .with_numbering(None) + .with_outlined(false) + .pack(), ); } - let indent = Self::indent_in(styles); - let depth = Self::depth_in(styles); + let indent = self.indent(styles); + let depth = self.depth(styles); let mut ancestors: Vec<&Content> = vec![]; for (_, node) in vt.locate(Selector::node::<HeadingNode>()) { @@ -129,13 +125,14 @@ impl Show for OutlineNode { let heading = node.to::<HeadingNode>().unwrap(); if let Some(depth) = depth { - if depth < heading.level() { + if depth < heading.level(StyleChain::default()) { continue; } } while ancestors.last().map_or(false, |last| { - last.to::<HeadingNode>().unwrap().level() >= heading.level() + last.to::<HeadingNode>().unwrap().level(StyleChain::default()) + >= heading.level(StyleChain::default()) }) { ancestors.pop(); } @@ -171,7 +168,7 @@ impl Show for OutlineNode { seq.push(start.linked(Destination::Internal(loc))); // Add filler symbols between the section name and page number. - if let Some(filler) = Self::fill_in(styles) { + if let Some(filler) = self.fill(styles) { seq.push(SpaceNode::new().pack()); seq.push( BoxNode::new() |
