Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ biblatex = { git = "https://github.com/typst/biblatex.git", features = [
ciborium = { version = "0.2.1", optional = true }
clap = { version = "4", optional = true, features = ["cargo"] }
strum = { version = "0.26", features = ["derive"], optional = true }
icu_collator = "2.0.0"
icu_locale = "2.0.0"

[dev-dependencies]
heck = "0.5"
Expand Down
40 changes: 38 additions & 2 deletions src/csl/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,49 @@ use citationberg::{
DemoteNonDroppingParticle, InheritableNameOptions, LocaleCode, LongShortForm, Sort,
SortDirection, SortKey,
};
use icu_collator::options::CollatorOptions;
use icu_collator::Collator;
use icu_locale::Locale as IcuLocale;

use crate::csl::rendering::RenderCsl;
use crate::csl::BufWriteFormat;

use super::taxonomy::EntryLike;
use super::{CitationItem, InstanceContext, StyleContext};

trait CollationOrd: Ord {
fn collation_cmp(&self, other: &Self, locale: LocaleCode) -> Ordering;
}

impl CollationOrd for str {
fn collation_cmp(&self, other: &Self, locale: LocaleCode) -> Ordering {
if let Ok(locale) = locale.0.parse::<IcuLocale>() {
let options = CollatorOptions::default();
let collator = Collator::try_new(locale.into(), options).unwrap();
collator.compare(self, other)
} else {
self.cmp(other)
}
}
}

impl<T: CollationOrd> CollationOrd for Option<T> {
fn collation_cmp(&self, other: &Self, locale: LocaleCode) -> Ordering {
match (self, other) {
(Some(a), Some(b)) => a.collation_cmp(b, locale),
(Some(_), None) => Ordering::Greater,
(None, Some(_)) => Ordering::Less,
(None, None) => Ordering::Equal,
}
}
}

impl CollationOrd for String {
fn collation_cmp(&self, other: &Self, locale: LocaleCode) -> Ordering {
CollationOrd::collation_cmp(self as &str, other, locale)
}
}

impl StyleContext<'_> {
/// Retrieve the ordering of two entries according to the given sort key.
fn cmp_entries<T: EntryLike>(
Expand All @@ -32,7 +68,7 @@ impl StyleContext<'_> {
.resolve_standard_variable(LongShortForm::default(), *s)
.map(|s| s.to_string().to_lowercase());

a.cmp(&b)
a.collation_cmp(&b, self.locale())
}
SortKey::Variable { variable: Variable::Date(d), .. } => {
let a = a.entry.resolve_date_variable(*d);
Expand Down Expand Up @@ -133,7 +169,7 @@ impl StyleContext<'_> {
let a_rendered = render(a, a_idx);
let b_rendered = render(b, b_idx);

a_rendered.cmp(&b_rendered)
a_rendered.collation_cmp(&b_rendered, self.locale())
}
};

Expand Down
1 change: 1 addition & 0 deletions tests/citeproc-pass.txt
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ sort_CitationNumberPrimaryAscendingViaMacroBibliography
sort_CitationNumberPrimaryAscendingViaVariableBibliography
sort_CitationSecondaryKey
sort_CiteGroupDelimiter
sort_ConditionalMacroDates
sort_DaleDalebout
sort_DateMacroSortWithSecondFieldAlign
sort_DateVariable
Expand Down
120 changes: 120 additions & 0 deletions tests/local/sort_Unicode.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
>>==== MODE ====>>
citation
<<==== MODE ====<<

>>==== RESULT ====>>
Ambroise-Rendu; Ère; Łakomy; Roux; Žižek
<<==== RESULT ====<<

>>==== CITATION-ITEMS ====>>
[
[
{
"id": "ITEM-1"
},
{
"id": "ITEM-2"
},
{
"id": "ITEM-3"
},
{
"id": "ITEM-4"
},
{
"id": "ITEM-5"
}
]
]
<<==== CITATION-ITEMS ====<<

>>==== CSL ====>>
<style
xmlns="http://purl.org/net/xbiblio/csl"
class="note"
version="1.0">
<info>
<title>Test fixture</title>
<id>http://citationstyles.org/tests/fixture</id>
<link href="http://citationstyles.org/tests/fixture" rel="self"/>
<link href="http://citationstyles.org/documentation/text" rel="documentation"/>
<category citation-format="author-date"/>
<updated>2014-04-30T13:19:38+00:00</updated>
<rights license="http://creativecommons.org/licenses/by-sa/3.0/">This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 License</rights>
</info>
<macro name="author">
<names variable="author">
<name form="short" />
</names>
</macro>
<citation>
<sort>
<key macro="author" />
</sort>
<layout delimiter="; ">
<text macro="author" />
</layout>
</citation>
</style>
<<==== CSL ====<<

>>==== INPUT ====>>
[
{
"author": [
{
"family": "Ambroise-Rendu",
"given": "Marc"
}
],
"id": "ITEM-1",
"type": "book"
},
{
"author": [
{
"family": "Roux",
"given": "Nicolas"
}
],
"id": "ITEM-2",
"type": "book"
},
{
"author": [
{
"family": "Łakomy",
"given": "Henryk"
}
],
"id": "ITEM-3",
"type": "book"
},
{
"author": [
{
"family": "Ère",
"given": "Informatique"
}
],
"id": "ITEM-4",
"type": "book"
},
{
"author": [
{
"family": "Žižek",
"given": "Slavoj"
}
],
"id": "ITEM-5",
"type": "book"
}
]
<<==== INPUT ====<<



>>===== VERSION =====>>
1.0
<<===== VERSION =====<<