Skip to content

Commit 2606034

Browse files
committed
Fix heading supplement
Fixes typst#730
1 parent 9720424 commit 2606034

File tree

5 files changed

+69
-70
lines changed

5 files changed

+69
-70
lines changed

library/src/math/mod.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,21 +298,20 @@ impl Refable for EquationElem {
298298
fn reference(
299299
&self,
300300
vt: &mut Vt,
301-
styles: StyleChain,
302301
supplement: Option<Content>,
302+
lang: Lang,
303303
) -> SourceResult<Content> {
304304
// first we create the supplement of the heading
305-
let mut supplement = supplement.unwrap_or_else(|| {
306-
TextElem::packed(self.local_name(TextElem::lang_in(styles)))
307-
});
305+
let mut supplement =
306+
supplement.unwrap_or_else(|| TextElem::packed(self.local_name(lang)));
308307

309308
// we append a space if the supplement is not empty
310309
if !supplement.is_empty() {
311310
supplement += TextElem::packed('\u{a0}')
312311
};
313312

314313
// we check for a numbering
315-
let Some(numbering) = self.numbering(styles) else {
314+
let Some(numbering) = self.numbering(StyleChain::default()) else {
316315
bail!(self.span(), "only numbered equations can be referenced");
317316
};
318317

@@ -324,11 +323,11 @@ impl Refable for EquationElem {
324323
Ok(supplement + numbers)
325324
}
326325

327-
fn numbering(&self, styles: StyleChain) -> Option<Numbering> {
328-
self.numbering(styles)
326+
fn numbering(&self) -> Option<Numbering> {
327+
self.numbering(StyleChain::default())
329328
}
330329

331-
fn counter(&self, _: StyleChain) -> Counter {
330+
fn counter(&self) -> Counter {
332331
Counter::of(Self::func())
333332
}
334333
}

library/src/meta/figure.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,14 @@ impl Synthesize for FigureElem {
223223
}),
224224
)));
225225

226+
self.push_caption(self.caption(styles));
226227
self.push_kind(Smart::Custom(kind));
227-
self.push_numbering(numbering);
228-
self.push_counter(Some(counter));
229228
self.push_supplement(Smart::Custom(Supplement::Content(
230229
supplement.unwrap_or_default(),
231230
)));
231+
self.push_numbering(numbering);
232+
self.push_outlined(self.outlined(styles));
233+
self.push_counter(Some(counter));
232234

233235
Ok(())
234236
}
@@ -242,7 +244,7 @@ impl Show for FigureElem {
242244
// We build the caption, if any.
243245
if self.caption(styles).is_some() {
244246
realized += VElem::weak(self.gap(styles).into()).pack();
245-
realized += self.show_caption(vt, styles)?;
247+
realized += self.show_caption(vt)?;
246248
}
247249

248250
// We wrap the contents in a block.
@@ -268,32 +270,32 @@ impl Refable for FigureElem {
268270
fn reference(
269271
&self,
270272
vt: &mut Vt,
271-
styles: StyleChain,
272273
supplement: Option<Content>,
274+
_: Lang,
273275
) -> SourceResult<Content> {
274276
// If the figure is not numbered, we cannot reference it.
275277
// Otherwise we build the supplement and numbering scheme.
276-
let Some(desc) = self.show_supplement_and_numbering(vt, styles, supplement)? else {
278+
let Some(desc) = self.show_supplement_and_numbering(vt, supplement)? else {
277279
bail!(self.span(), "cannot reference unnumbered figure")
278280
};
279281

280282
Ok(desc)
281283
}
282284

283-
fn outline(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult<Option<Content>> {
285+
fn outline(&self, vt: &mut Vt, _: Lang) -> SourceResult<Option<Content>> {
284286
// If the figure is not outlined, it is not referenced.
285-
if !self.outlined(styles) {
287+
if !self.outlined(StyleChain::default()) {
286288
return Ok(None);
287289
}
288290

289-
self.show_caption(vt, styles).map(Some)
291+
self.show_caption(vt).map(Some)
290292
}
291293

292-
fn numbering(&self, styles: StyleChain) -> Option<Numbering> {
293-
self.numbering(styles)
294+
fn numbering(&self) -> Option<Numbering> {
295+
self.numbering(StyleChain::default())
294296
}
295297

296-
fn counter(&self, _: StyleChain) -> Counter {
298+
fn counter(&self) -> Counter {
297299
self.counter().unwrap_or_else(|| Counter::of(Self::func()))
298300
}
299301
}
@@ -327,12 +329,13 @@ impl FigureElem {
327329
pub fn show_supplement_and_numbering(
328330
&self,
329331
vt: &mut Vt,
330-
styles: StyleChain,
331332
external_supplement: Option<Content>,
332333
) -> SourceResult<Option<Content>> {
333334
if let (Some(numbering), Some(supplement), Some(counter)) = (
334-
self.numbering(styles),
335-
self.supplement(styles).as_custom().and_then(|s| s.as_content()),
335+
self.numbering(StyleChain::default()),
336+
self.supplement(StyleChain::default())
337+
.as_custom()
338+
.and_then(|s| s.as_content()),
336339
self.counter(),
337340
) {
338341
let mut name = external_supplement.unwrap_or(supplement);
@@ -356,12 +359,12 @@ impl FigureElem {
356359
///
357360
/// # Errors
358361
/// If a numbering is specified but the [`Self::element`] is `None`.
359-
pub fn show_caption(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult<Content> {
360-
let Some(mut caption) = self.caption(styles) else {
362+
pub fn show_caption(&self, vt: &mut Vt) -> SourceResult<Content> {
363+
let Some(mut caption) = self.caption(StyleChain::default()) else {
361364
return Ok(Content::empty());
362365
};
363366

364-
if let Some(sup_and_num) = self.show_supplement_and_numbering(vt, styles, None)? {
367+
if let Some(sup_and_num) = self.show_supplement_and_numbering(vt, None)? {
365368
caption = sup_and_num + TextElem::packed(": ") + caption;
366369
}
367370

library/src/meta/heading.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,6 @@ pub struct HeadingElem {
5959
/// ```
6060
pub numbering: Option<Numbering>,
6161

62-
/// Whether the heading should appear in the outline.
63-
///
64-
/// ```example
65-
/// #outline()
66-
///
67-
/// #heading[Normal]
68-
/// This is a normal heading.
69-
///
70-
/// #heading(outlined: false)[Hidden]
71-
/// This heading does not appear
72-
/// in the outline.
73-
/// ```
74-
#[default(true)]
75-
pub outlined: bool,
76-
7762
/// A supplement for the heading.
7863
///
7964
/// For references to headings, this is added before the
@@ -91,6 +76,21 @@ pub struct HeadingElem {
9176
#[default(Smart::Auto)]
9277
pub supplement: Smart<Option<Supplement>>,
9378

79+
/// Whether the heading should appear in the outline.
80+
///
81+
/// ```example
82+
/// #outline()
83+
///
84+
/// #heading[Normal]
85+
/// This is a normal heading.
86+
///
87+
/// #heading(outlined: false)[Hidden]
88+
/// This heading does not appear
89+
/// in the outline.
90+
/// ```
91+
#[default(true)]
92+
pub outlined: bool,
93+
9494
/// The heading's title.
9595
#[required]
9696
pub body: Content,
@@ -100,6 +100,7 @@ impl Synthesize for HeadingElem {
100100
fn synthesize(&mut self, _vt: &mut Vt, styles: StyleChain) -> SourceResult<()> {
101101
self.push_level(self.level(styles));
102102
self.push_numbering(self.numbering(styles));
103+
self.push_supplement(self.supplement(styles));
103104
self.push_outlined(self.outlined(styles));
104105
Ok(())
105106
}
@@ -159,17 +160,15 @@ impl Refable for HeadingElem {
159160
fn reference(
160161
&self,
161162
vt: &mut Vt,
162-
styles: StyleChain,
163163
supplement: Option<Content>,
164+
lang: Lang,
164165
) -> SourceResult<Content> {
165166
// Create the supplement of the heading.
166167
let mut supplement = if let Some(supplement) = supplement {
167168
supplement
168169
} else {
169-
match self.supplement(styles) {
170-
Smart::Auto => {
171-
TextElem::packed(self.local_name(TextElem::lang_in(styles)))
172-
}
170+
match self.supplement(StyleChain::default()) {
171+
Smart::Auto => TextElem::packed(self.local_name(lang)),
173172
Smart::Custom(None) => Content::empty(),
174173
Smart::Custom(Some(supplement)) => {
175174
supplement.resolve(vt, std::iter::once(Value::from(self.clone())))?
@@ -183,7 +182,7 @@ impl Refable for HeadingElem {
183182
};
184183

185184
// Check for a numbering.
186-
let Some(numbering) = self.numbering(styles) else {
185+
let Some(numbering) = self.numbering(StyleChain::default()) else {
187186
bail!(self.span(), "only numbered headings can be referenced");
188187
};
189188

@@ -195,21 +194,21 @@ impl Refable for HeadingElem {
195194
Ok(supplement + numbers)
196195
}
197196

198-
fn level(&self, styles: StyleChain) -> usize {
199-
self.level(styles).get()
197+
fn level(&self) -> usize {
198+
self.level(StyleChain::default()).get()
200199
}
201200

202-
fn numbering(&self, styles: StyleChain) -> Option<Numbering> {
203-
self.numbering(styles)
201+
fn numbering(&self) -> Option<Numbering> {
202+
self.numbering(StyleChain::default())
204203
}
205204

206-
fn counter(&self, _: StyleChain) -> Counter {
205+
fn counter(&self) -> Counter {
207206
Counter::of(Self::func())
208207
}
209208

210-
fn outline(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult<Option<Content>> {
209+
fn outline(&self, vt: &mut Vt, _: Lang) -> SourceResult<Option<Content>> {
211210
// Check whether the heading is outlined.
212-
if !self.outlined(styles) {
211+
if !self.outlined(StyleChain::default()) {
213212
return Ok(None);
214213
}
215214

library/src/meta/outline.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ impl Show for OutlineElem {
151151

152152
let indent = self.indent(styles);
153153
let depth = self.depth(styles).map_or(usize::MAX, NonZeroUsize::get);
154+
let lang = TextElem::lang_in(styles);
154155

155156
let mut ancestors: Vec<&Content> = vec![];
156157
let elems = vt.introspector.query(self.target(styles));
@@ -161,11 +162,11 @@ impl Show for OutlineElem {
161162
};
162163

163164
let location = elem.location().expect("missing location");
164-
if depth < refable.level(StyleChain::default()) {
165+
if depth < refable.level() {
165166
continue;
166167
}
167168

168-
let Some(outline) = refable.outline(vt, StyleChain::default())? else {
169+
let Some(outline) = refable.outline(vt, lang)? else {
169170
continue;
170171
};
171172

@@ -174,10 +175,7 @@ impl Show for OutlineElem {
174175
while ancestors
175176
.last()
176177
.and_then(|ancestor| ancestor.with::<dyn Refable>())
177-
.map_or(false, |last| {
178-
last.level(StyleChain::default())
179-
>= refable.level(StyleChain::default())
180-
})
178+
.map_or(false, |last| last.level() >= refable.level())
181179
{
182180
ancestors.pop();
183181
}
@@ -188,11 +186,9 @@ impl Show for OutlineElem {
188186
for ancestor in &ancestors {
189187
let ancestor_refable = ancestor.with::<dyn Refable>().unwrap();
190188

191-
if let Some(numbering) =
192-
ancestor_refable.numbering(StyleChain::default())
193-
{
189+
if let Some(numbering) = ancestor_refable.numbering() {
194190
let numbers = ancestor_refable
195-
.counter(StyleChain::default())
191+
.counter()
196192
.at(vt, ancestor.location().unwrap())?
197193
.display(vt, &numbering)?;
198194

library/src/meta/reference.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{BibliographyElem, CiteElem, Counter, Figurable, Numbering};
22
use crate::prelude::*;
3+
use crate::text::TextElem;
34

45
/// A reference to a label or bibliography.
56
///
@@ -131,10 +132,11 @@ impl Show for RefElem {
131132
}
132133
};
133134

135+
let lang = TextElem::lang_in(styles);
134136
let reference = elem
135137
.with::<dyn Refable>()
136138
.expect("element should be refable")
137-
.reference(vt, styles, supplement)?;
139+
.reference(vt, supplement, lang)?;
138140

139141
Ok(reference.linked(Destination::Location(elem.location().unwrap())))
140142
}
@@ -212,27 +214,27 @@ pub trait Refable {
212214
fn reference(
213215
&self,
214216
vt: &mut Vt,
215-
styles: StyleChain,
216217
supplement: Option<Content>,
218+
lang: Lang,
217219
) -> SourceResult<Content>;
218220

219221
/// Tries to build an outline element for this element.
220222
/// If this returns `None`, the outline will not include this element.
221223
/// By default this just calls [`Refable::reference`].
222-
fn outline(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult<Option<Content>> {
223-
self.reference(vt, styles, None).map(Some)
224+
fn outline(&self, vt: &mut Vt, lang: Lang) -> SourceResult<Option<Content>> {
225+
self.reference(vt, None, lang).map(Some)
224226
}
225227

226228
/// Returns the level of this element.
227229
/// This is used to determine the level of the outline.
228230
/// By default this returns `0`.
229-
fn level(&self, _styles: StyleChain) -> usize {
231+
fn level(&self) -> usize {
230232
0
231233
}
232234

233235
/// Returns the numbering of this element.
234-
fn numbering(&self, styles: StyleChain) -> Option<Numbering>;
236+
fn numbering(&self) -> Option<Numbering>;
235237

236238
/// Returns the counter of this element.
237-
fn counter(&self, styles: StyleChain) -> Counter;
239+
fn counter(&self) -> Counter;
238240
}

0 commit comments

Comments
 (0)