Prefix-less bibliography items are blocks

This commit is contained in:
Laurenz 2025-01-23 17:54:28 +01:00
parent 2492ba42ed
commit 0e575632de
4 changed files with 42 additions and 20 deletions

View File

@ -17,7 +17,7 @@ use hayagriva::{
use indexmap::IndexMap; use indexmap::IndexMap;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use typst_syntax::{Span, Spanned}; use typst_syntax::{Span, Spanned};
use typst_utils::{ManuallyHash, NonZeroExt, PicoStr}; use typst_utils::{Get, ManuallyHash, NonZeroExt, PicoStr};
use crate::diag::{bail, error, At, FileError, HintedStrResult, SourceResult, StrResult}; use crate::diag::{bail, error, At, FileError, HintedStrResult, SourceResult, StrResult};
use crate::engine::Engine; use crate::engine::Engine;
@ -29,7 +29,7 @@ use crate::foundations::{
use crate::introspection::{Introspector, Locatable, Location}; use crate::introspection::{Introspector, Locatable, Location};
use crate::layout::{ use crate::layout::{
BlockBody, BlockElem, Em, GridCell, GridChild, GridElem, GridItem, HElem, PadElem, BlockBody, BlockElem, Em, GridCell, GridChild, GridElem, GridItem, HElem, PadElem,
Sizing, TrackSizings, VElem, Sides, Sizing, TrackSizings,
}; };
use crate::loading::{DataSource, Load}; use crate::loading::{DataSource, Load};
use crate::model::{ use crate::model::{
@ -206,19 +206,20 @@ impl Show for Packed<BibliographyElem> {
const COLUMN_GUTTER: Em = Em::new(0.65); const COLUMN_GUTTER: Em = Em::new(0.65);
const INDENT: Em = Em::new(1.5); const INDENT: Em = Em::new(1.5);
let span = self.span();
let mut seq = vec![]; let mut seq = vec![];
if let Some(title) = self.title(styles).unwrap_or_else(|| { if let Some(title) = self.title(styles).unwrap_or_else(|| {
Some(TextElem::packed(Self::local_name_in(styles)).spanned(self.span())) Some(TextElem::packed(Self::local_name_in(styles)).spanned(span))
}) { }) {
seq.push( seq.push(
HeadingElem::new(title) HeadingElem::new(title)
.with_depth(NonZeroUsize::ONE) .with_depth(NonZeroUsize::ONE)
.pack() .pack()
.spanned(self.span()), .spanned(span),
); );
} }
let span = self.span();
let works = Works::generate(engine).at(span)?; let works = Works::generate(engine).at(span)?;
let references = works let references = works
.references .references
@ -226,10 +227,9 @@ impl Show for Packed<BibliographyElem> {
.ok_or("CSL style is not suitable for bibliographies") .ok_or("CSL style is not suitable for bibliographies")
.at(span)?; .at(span)?;
let row_gutter = ParElem::spacing_in(styles);
let row_gutter_elem = VElem::new(row_gutter.into()).with_weak(true).pack();
if references.iter().any(|(prefix, _)| prefix.is_some()) { if references.iter().any(|(prefix, _)| prefix.is_some()) {
let row_gutter = ParElem::spacing_in(styles);
let mut cells = vec![]; let mut cells = vec![];
for (prefix, reference) in references { for (prefix, reference) in references {
cells.push(GridChild::Item(GridItem::Cell( cells.push(GridChild::Item(GridItem::Cell(
@ -246,23 +246,27 @@ impl Show for Packed<BibliographyElem> {
.with_column_gutter(TrackSizings(smallvec![COLUMN_GUTTER.into()])) .with_column_gutter(TrackSizings(smallvec![COLUMN_GUTTER.into()]))
.with_row_gutter(TrackSizings(smallvec![row_gutter.into()])) .with_row_gutter(TrackSizings(smallvec![row_gutter.into()]))
.pack() .pack()
.spanned(self.span()), .spanned(span),
); );
} else { } else {
for (i, (_, reference)) in references.iter().enumerate() { for (_, reference) in references {
if i > 0 { let realized = reference.clone();
seq.push(row_gutter_elem.clone()); let block = if works.hanging_indent {
} let body = HElem::new((-INDENT).into()).pack() + realized;
seq.push(reference.clone()); let inset = Sides::default()
.with(TextElem::dir_in(styles).start(), Some(INDENT.into()));
BlockElem::new()
.with_body(Some(BlockBody::Content(body)))
.with_inset(inset)
} else {
BlockElem::new().with_body(Some(BlockBody::Content(realized)))
};
seq.push(block.pack().spanned(span));
} }
} }
let mut content = Content::sequence(seq); Ok(Content::sequence(seq))
if works.hanging_indent {
content = content.styled(ParElem::set_hanging_indent(INDENT.into()));
}
Ok(content)
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

View File

@ -53,6 +53,24 @@ Now we have multiple bibliographies containing @glacier-melt @keshav2007read
@Zee04 @Zee04
#bibliography("/assets/bib/works_too.bib", style: "mla") #bibliography("/assets/bib/works_too.bib", style: "mla")
--- bibliography-grid-par ---
// Ensure that a grid-based bibliography does not produce paragraphs.
#show par: highlight
@Zee04
@keshav2007read
#bibliography("/assets/bib/works_too.bib")
--- bibliography-indent-par ---
// Ensure that an indent-based bibliography does not produce paragraphs.
#show par: highlight
@Zee04
@keshav2007read
#bibliography("/assets/bib/works_too.bib", style: "mla")
--- issue-4618-bibliography-set-heading-level --- --- issue-4618-bibliography-set-heading-level ---
// Test that the bibliography block's heading is set to 2 by the show rule, // Test that the bibliography block's heading is set to 2 by the show rule,
// and therefore should be rendered like a level-2 heading. Notably, this // and therefore should be rendered like a level-2 heading. Notably, this