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 smallvec::{smallvec, SmallVec};
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::engine::Engine;
@ -29,7 +29,7 @@ use crate::foundations::{
use crate::introspection::{Introspector, Locatable, Location};
use crate::layout::{
BlockBody, BlockElem, Em, GridCell, GridChild, GridElem, GridItem, HElem, PadElem,
Sizing, TrackSizings, VElem,
Sides, Sizing, TrackSizings,
};
use crate::loading::{DataSource, Load};
use crate::model::{
@ -206,19 +206,20 @@ impl Show for Packed<BibliographyElem> {
const COLUMN_GUTTER: Em = Em::new(0.65);
const INDENT: Em = Em::new(1.5);
let span = self.span();
let mut seq = vec![];
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(
HeadingElem::new(title)
.with_depth(NonZeroUsize::ONE)
.pack()
.spanned(self.span()),
.spanned(span),
);
}
let span = self.span();
let works = Works::generate(engine).at(span)?;
let references = works
.references
@ -226,10 +227,9 @@ impl Show for Packed<BibliographyElem> {
.ok_or("CSL style is not suitable for bibliographies")
.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()) {
let row_gutter = ParElem::spacing_in(styles);
let mut cells = vec![];
for (prefix, reference) in references {
cells.push(GridChild::Item(GridItem::Cell(
@ -246,23 +246,27 @@ impl Show for Packed<BibliographyElem> {
.with_column_gutter(TrackSizings(smallvec![COLUMN_GUTTER.into()]))
.with_row_gutter(TrackSizings(smallvec![row_gutter.into()]))
.pack()
.spanned(self.span()),
.spanned(span),
);
} else {
for (i, (_, reference)) in references.iter().enumerate() {
if i > 0 {
seq.push(row_gutter_elem.clone());
}
seq.push(reference.clone());
for (_, reference) in references {
let realized = reference.clone();
let block = if works.hanging_indent {
let body = HElem::new((-INDENT).into()).pack() + realized;
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);
if works.hanging_indent {
content = content.styled(ParElem::set_hanging_indent(INDENT.into()));
}
Ok(content)
Ok(Content::sequence(seq))
}
}

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
#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 ---
// 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