summaryrefslogtreecommitdiff
path: root/library/src/meta
diff options
context:
space:
mode:
authorpan93412 <pan93412@gmail.com>2023-04-26 17:30:03 +0800
committerGitHub <noreply@github.com>2023-04-26 11:30:03 +0200
commita6df909a8d8018ef87b66b2128c6acfa9fb0599c (patch)
treeec1717715dac6944543e13df8b54fef3a3eae038 /library/src/meta
parent1d42d6674c5bbeca2be1ca9a35dccd71b9346228 (diff)
Allow passing `region` to LocalName (#926)
Diffstat (limited to 'library/src/meta')
-rw-r--r--library/src/meta/bibliography.rs12
-rw-r--r--library/src/meta/figure.rs13
-rw-r--r--library/src/meta/heading.rs12
-rw-r--r--library/src/meta/mod.rs5
-rw-r--r--library/src/meta/outline.rs18
-rw-r--r--library/src/meta/reference.rs17
6 files changed, 56 insertions, 21 deletions
diff --git a/library/src/meta/bibliography.rs b/library/src/meta/bibliography.rs
index 368a7c39..99f2111f 100644
--- a/library/src/meta/bibliography.rs
+++ b/library/src/meta/bibliography.rs
@@ -152,10 +152,14 @@ impl Show for BibliographyElem {
let mut seq = vec![];
if let Some(title) = self.title(styles) {
- let title = title.unwrap_or_else(|| {
- TextElem::packed(self.local_name(TextElem::lang_in(styles)))
+ let title =
+ title.unwrap_or_else(|| {
+ TextElem::packed(self.local_name(
+ TextElem::lang_in(styles),
+ TextElem::region_in(styles),
+ ))
.spanned(self.span())
- });
+ });
seq.push(HeadingElem::new(title).with_level(NonZeroUsize::ONE).pack());
}
@@ -206,7 +210,7 @@ impl Finalize for BibliographyElem {
}
impl LocalName for BibliographyElem {
- fn local_name(&self, lang: Lang) -> &'static str {
+ fn local_name(&self, lang: Lang, _: Option<Region>) -> &'static str {
match lang {
Lang::ARABIC => "المراجع",
Lang::BOKMÅL => "Bibliografi",
diff --git a/library/src/meta/figure.rs b/library/src/meta/figure.rs
index d86625b7..bfbfeb83 100644
--- a/library/src/meta/figure.rs
+++ b/library/src/meta/figure.rs
@@ -193,7 +193,10 @@ impl Synthesize for FigureElem {
Smart::Auto => match &kind {
FigureKind::Elem(func) => {
let elem = Content::new(*func).with::<dyn LocalName>().map(|c| {
- TextElem::packed(c.local_name(TextElem::lang_in(styles)))
+ TextElem::packed(c.local_name(
+ TextElem::lang_in(styles),
+ TextElem::region_in(styles),
+ ))
});
if numbering.is_some() {
@@ -273,6 +276,7 @@ impl Refable for FigureElem {
vt: &mut Vt,
supplement: Option<Content>,
_: Lang,
+ _: Option<Region>,
) -> SourceResult<Content> {
// If the figure is not numbered, we cannot reference it.
// Otherwise we build the supplement and numbering scheme.
@@ -283,7 +287,12 @@ impl Refable for FigureElem {
Ok(desc)
}
- fn outline(&self, vt: &mut Vt, _: Lang) -> SourceResult<Option<Content>> {
+ fn outline(
+ &self,
+ vt: &mut Vt,
+ _: Lang,
+ _: Option<Region>,
+ ) -> SourceResult<Option<Content>> {
// If the figure is not outlined, it is not referenced.
if !self.outlined(StyleChain::default()) {
return Ok(None);
diff --git a/library/src/meta/heading.rs b/library/src/meta/heading.rs
index ad836363..43505448 100644
--- a/library/src/meta/heading.rs
+++ b/library/src/meta/heading.rs
@@ -164,13 +164,14 @@ impl Refable for HeadingElem {
vt: &mut Vt,
supplement: Option<Content>,
lang: Lang,
+ region: Option<Region>,
) -> SourceResult<Content> {
// Create the supplement of the heading.
let mut supplement = if let Some(supplement) = supplement {
supplement
} else {
match self.supplement(StyleChain::default()) {
- Smart::Auto => TextElem::packed(self.local_name(lang)),
+ Smart::Auto => TextElem::packed(self.local_name(lang, region)),
Smart::Custom(None) => Content::empty(),
Smart::Custom(Some(supplement)) => {
supplement.resolve(vt, std::iter::once(Value::from(self.clone())))?
@@ -208,7 +209,12 @@ impl Refable for HeadingElem {
Counter::of(Self::func())
}
- fn outline(&self, vt: &mut Vt, _: Lang) -> SourceResult<Option<Content>> {
+ fn outline(
+ &self,
+ vt: &mut Vt,
+ _: Lang,
+ _: Option<Region>,
+ ) -> SourceResult<Option<Content>> {
// Check whether the heading is outlined.
if !self.outlined(StyleChain::default()) {
return Ok(None);
@@ -228,7 +234,7 @@ impl Refable for HeadingElem {
}
impl LocalName for HeadingElem {
- fn local_name(&self, lang: Lang) -> &'static str {
+ fn local_name(&self, lang: Lang, _: Option<Region>) -> &'static str {
match lang {
Lang::ARABIC => "الفصل",
Lang::BOKMÅL => "Kapittel",
diff --git a/library/src/meta/mod.rs b/library/src/meta/mod.rs
index 5ec40e42..279fe54a 100644
--- a/library/src/meta/mod.rs
+++ b/library/src/meta/mod.rs
@@ -27,9 +27,10 @@ pub use self::reference::*;
pub use self::state::*;
use typst::doc::Lang;
+use typst::doc::Region;
/// The named with which an element is referenced.
pub trait LocalName {
- /// Get the name in the given language.
- fn local_name(&self, lang: Lang) -> &'static str;
+ /// Get the name in the given language and (optionally) region.
+ fn local_name(&self, lang: Lang, region: Option<Region>) -> &'static str;
}
diff --git a/library/src/meta/outline.rs b/library/src/meta/outline.rs
index 25211164..8e1bb723 100644
--- a/library/src/meta/outline.rs
+++ b/library/src/meta/outline.rs
@@ -1,5 +1,7 @@
use std::str::FromStr;
+use typst::util::option_eq;
+
use super::{
Counter, CounterKey, HeadingElem, LocalName, Numbering, NumberingPattern, Refable,
};
@@ -141,10 +143,14 @@ impl Show for OutlineElem {
let mut seq = vec![ParbreakElem::new().pack()];
// Build the outline title.
if let Some(title) = self.title(styles) {
- let title = title.unwrap_or_else(|| {
- TextElem::packed(self.local_name(TextElem::lang_in(styles)))
+ let title =
+ title.unwrap_or_else(|| {
+ TextElem::packed(self.local_name(
+ TextElem::lang_in(styles),
+ TextElem::region_in(styles),
+ ))
.spanned(self.span())
- });
+ });
seq.push(HeadingElem::new(title).with_level(NonZeroUsize::ONE).pack());
}
@@ -152,6 +158,7 @@ impl Show for OutlineElem {
let indent = self.indent(styles);
let depth = self.depth(styles).map_or(usize::MAX, NonZeroUsize::get);
let lang = TextElem::lang_in(styles);
+ let region = TextElem::region_in(styles);
let mut ancestors: Vec<&Content> = vec![];
let elems = vt.introspector.query(&self.target(styles));
@@ -165,7 +172,7 @@ impl Show for OutlineElem {
continue;
}
- let Some(outline) = refable.outline(vt, lang)? else {
+ let Some(outline) = refable.outline(vt, lang, region)? else {
continue;
};
@@ -255,10 +262,11 @@ impl Finalize for OutlineElem {
}
impl LocalName for OutlineElem {
- fn local_name(&self, lang: Lang) -> &'static str {
+ fn local_name(&self, lang: Lang, region: Option<Region>) -> &'static str {
match lang {
Lang::ARABIC => "المحتويات",
Lang::BOKMÅL => "Innhold",
+ Lang::CHINESE if option_eq(region, "TW") => "目錄",
Lang::CHINESE => "目录",
Lang::CZECH => "Obsah",
Lang::FRENCH => "Table des matières",
diff --git a/library/src/meta/reference.rs b/library/src/meta/reference.rs
index adbc9f57..29b28828 100644
--- a/library/src/meta/reference.rs
+++ b/library/src/meta/reference.rs
@@ -184,10 +184,11 @@ impl Show for RefElem {
};
let lang = TextElem::lang_in(styles);
+ let region = TextElem::region_in(styles);
let reference = elem
.with::<dyn Refable>()
.expect("element should be refable")
- .reference(vt, supplement, lang)?;
+ .reference(vt, supplement, lang, region)?;
Ok(reference.linked(Destination::Location(elem.location().unwrap())))
}
@@ -259,21 +260,27 @@ pub trait Refable {
///
/// # Arguments
/// - `vt` - The virtual typesetter.
- /// - `styles` - The styles of the reference.
- /// - `location` - The location where the reference is being created.
/// - `supplement` - The supplement of the reference.
+ /// - `lang`: The language of the reference.
+ /// - `region`: The region of the reference.
fn reference(
&self,
vt: &mut Vt,
supplement: Option<Content>,
lang: Lang,
+ region: Option<Region>,
) -> SourceResult<Content>;
/// Tries to build an outline element for this element.
/// If this returns `None`, the outline will not include this element.
/// By default this just calls [`Refable::reference`].
- fn outline(&self, vt: &mut Vt, lang: Lang) -> SourceResult<Option<Content>> {
- self.reference(vt, None, lang).map(Some)
+ fn outline(
+ &self,
+ vt: &mut Vt,
+ lang: Lang,
+ region: Option<Region>,
+ ) -> SourceResult<Option<Content>> {
+ self.reference(vt, None, lang, region).map(Some)
}
/// Returns the level of this element.