Add reversed numbering (#5563)

This commit is contained in:
+merlan #flirora 2024-12-16 08:45:57 -05:00 committed by GitHub
parent 7f139517b9
commit d3620df4c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 42 additions and 6 deletions

View File

@ -74,6 +74,7 @@ pub fn layout_enum(
regions: Regions, regions: Regions,
) -> SourceResult<Fragment> { ) -> SourceResult<Fragment> {
let numbering = elem.numbering(styles); let numbering = elem.numbering(styles);
let reversed = elem.reversed(styles);
let indent = elem.indent(styles); let indent = elem.indent(styles);
let body_indent = elem.body_indent(styles); let body_indent = elem.body_indent(styles);
let gutter = elem.spacing(styles).unwrap_or_else(|| { let gutter = elem.spacing(styles).unwrap_or_else(|| {
@ -86,7 +87,9 @@ pub fn layout_enum(
let mut cells = vec![]; let mut cells = vec![];
let mut locator = locator.split(); let mut locator = locator.split();
let mut number = elem.start(styles); let mut number =
elem.start(styles)
.unwrap_or_else(|| if reversed { elem.children.len() } else { 1 });
let mut parents = EnumElem::parents_in(styles); let mut parents = EnumElem::parents_in(styles);
let full = elem.full(styles); let full = elem.full(styles);
@ -127,7 +130,8 @@ pub fn layout_enum(
item.body.clone().styled(EnumElem::set_parents(smallvec![number])), item.body.clone().styled(EnumElem::set_parents(smallvec![number])),
locator.next(&item.body.span()), locator.next(&item.body.span()),
)); ));
number = number.saturating_add(1); number =
if reversed { number.saturating_sub(1) } else { number.saturating_add(1) };
} }
let grid = CellGrid::new( let grid = CellGrid::new(

View File

@ -9,7 +9,7 @@ use crate::foundations::{
cast, elem, scope, Array, Content, NativeElement, Packed, Show, Smart, StyleChain, cast, elem, scope, Array, Content, NativeElement, Packed, Show, Smart, StyleChain,
Styles, TargetElem, Styles, TargetElem,
}; };
use crate::html::{attr, tag, HtmlElem}; use crate::html::{attr, tag, HtmlAttr, HtmlElem};
use crate::layout::{Alignment, BlockElem, Em, HAlignment, Length, VAlignment, VElem}; use crate::layout::{Alignment, BlockElem, Em, HAlignment, Length, VAlignment, VElem};
use crate::model::{ListItemLike, ListLike, Numbering, NumberingPattern, ParElem}; use crate::model::{ListItemLike, ListLike, Numbering, NumberingPattern, ParElem};
@ -127,8 +127,7 @@ pub struct EnumElem {
/// [Ahead], /// [Ahead],
/// ) /// )
/// ``` /// ```
#[default(1)] pub start: Smart<usize>,
pub start: usize,
/// Whether to display the full numbering, including the numbers of /// Whether to display the full numbering, including the numbers of
/// all parent enumerations. /// all parent enumerations.
@ -144,6 +143,17 @@ pub struct EnumElem {
#[default(false)] #[default(false)]
pub full: bool, pub full: bool,
/// Whether to reverse the numbering for this enumeration.
///
/// ```example
/// #set enum(reversed: true)
/// + Coffee
/// + Tea
/// + Milk
/// ```
#[default(false)]
pub reversed: bool,
/// The indentation of each item. /// The indentation of each item.
#[resolve] #[resolve]
pub indent: Length, pub indent: Length,
@ -217,7 +227,12 @@ impl EnumElem {
impl Show for Packed<EnumElem> { impl Show for Packed<EnumElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> { fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
if TargetElem::target_in(styles).is_html() { if TargetElem::target_in(styles).is_html() {
return Ok(HtmlElem::new(tag::ol) let mut elem = HtmlElem::new(tag::ol);
if self.reversed(styles) {
elem =
elem.with_attr(const { HtmlAttr::constant("reversed") }, "reversed");
}
return Ok(elem
.with_body(Some(Content::sequence(self.children.iter().map(|item| { .with_body(Some(Content::sequence(self.children.iter().map(|item| {
let mut li = HtmlElem::new(tag::li); let mut li = HtmlElem::new(tag::li);
if let Some(nr) = item.number(styles) { if let Some(nr) = item.number(styles) {

Binary file not shown.

After

Width:  |  Height:  |  Size: 666 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 B

View File

@ -71,6 +71,23 @@ a + 0.
+ First + First
+ Nested + Nested
--- enum-numbering-reversed ---
// Test reverse numbering.
#set enum(reversed: true)
+ Coffee
+ Tea
+ Milk
--- enum-numbering-reversed-overriden ---
// Test reverse numbering with overriden numbers.
#set enum(reversed: true)
+ A
+ B
+ C
9. D
+ E
+ F
--- enum-numbering-closure --- --- enum-numbering-closure ---
// Test numbering with closure. // Test numbering with closure.
#enum( #enum(