Skip to content

Commit

Permalink
Factor out function for standalone citations
Browse files Browse the repository at this point in the history
  • Loading branch information
reknih committed Oct 20, 2023
1 parent a25d8da commit 7b68f3b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 64 deletions.
119 changes: 59 additions & 60 deletions src/csl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,66 +69,6 @@ impl<'a> BibliographyDriver<'a> {
Self::default()
}

/// Create a new citation with the given items. Bibliography-wide
/// disambiguation will not be applied.
pub fn instant_citation(&self, mut req: CitationRequest<'_>) -> ElemChildren {
let style = req.style();
style.sort(&mut req.items, style.csl.citation.sort.as_ref());
let mut res = vec![];
for item in req.items {
res.push(style.citation(
item.entry,
CiteProperties::for_sorting(item.locator, 0),
req.locale.as_ref(),
item.kind,
));
}

let non_empty: Vec<_> = res.into_iter().filter(|c| c.has_content()).collect();

let formatting =
Formatting::default().apply(style.csl.citation.layout.to_formatting());

if !non_empty.is_empty() {
let mut res = if let Some(prefix) = style.csl.citation.layout.prefix.as_ref()
{
ElemChildren(vec![Formatted { text: prefix.clone(), formatting }.into()])
} else {
ElemChildren::new()
};

for (i, elem_children) in non_empty.into_iter().enumerate() {
let first = i == 0;
if !first {
res.0.push(
Formatted {
text: style
.csl
.citation
.layout
.delimiter
.as_deref()
.unwrap_or(Citation::DEFAULT_CITE_GROUP_DELIMITER)
.to_string(),
formatting,
}
.into(),
);
}

res.0.extend(elem_children.0)
}

if let Some(suffix) = style.csl.citation.layout.suffix.as_ref() {
res.0.push(Formatted { text: suffix.clone(), formatting }.into());
}

simplify_children(res)
} else {
ElemChildren::new()
}
}

/// Create a new citation with the given items.
pub fn citation(&mut self, mut req: CitationRequest<'a>) {
let style = req.style();
Expand Down Expand Up @@ -547,6 +487,65 @@ impl<'a> BibliographyDriver<'a> {
}
}

/// Create a new citation with the given items. Bibliography-wide disambiguation
/// and some other features will not be applied.
pub fn standalone_citation(mut req: CitationRequest<'_>) -> ElemChildren {
let style = req.style();
style.sort(&mut req.items, style.csl.citation.sort.as_ref());
let mut res = vec![];
for item in req.items {
res.push(style.citation(
item.entry,
CiteProperties::for_sorting(item.locator, 0),
req.locale.as_ref(),
item.kind,
));
}

let non_empty: Vec<_> = res.into_iter().filter(|c| c.has_content()).collect();

let formatting =
Formatting::default().apply(style.csl.citation.layout.to_formatting());

if !non_empty.is_empty() {
let mut res = if let Some(prefix) = style.csl.citation.layout.prefix.as_ref() {
ElemChildren(vec![Formatted { text: prefix.clone(), formatting }.into()])
} else {
ElemChildren::new()
};

for (i, elem_children) in non_empty.into_iter().enumerate() {
let first = i == 0;
if !first {
res.0.push(
Formatted {
text: style
.csl
.citation
.layout
.delimiter
.as_deref()
.unwrap_or(Citation::DEFAULT_CITE_GROUP_DELIMITER)
.to_string(),
formatting,
}
.into(),
);
}

res.0.extend(elem_children.0)
}

if let Some(suffix) = style.csl.citation.layout.suffix.as_ref() {
res.0.push(Formatted { text: suffix.clone(), formatting }.into());
}

simplify_children(res)
} else {
ElemChildren::new()
}
}

type AmbiguousGroup = Vec<(usize, usize)>;

/// Progressively transform names to disambiguate them.
Expand Down
1 change: 0 additions & 1 deletion src/csl/taxonomy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ impl<'a> InstanceContext<'a> {
.map(|f| f.select(form))
.map(Cow::Borrowed),
StandardVariable::PartTitle => None,
// TODO: Accomodate more serial numbers
StandardVariable::PMCID => {
entry.pmcid().map(|d| Cow::Owned(StringChunk::verbatim(d).into()))
}
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,10 @@ use std::collections::BTreeMap;

pub use citationberg::{IndependentStyle, LocaleFile};
pub use csl::{
BibliographyDriver, BibliographyRequest, Brackets, BufWriteFormat, CitationItem,
CitationRequest, Elem, ElemChild, ElemChildren, ElemMeta, Formatted, Formatting,
Rendered, RenderedBibliography, RenderedCitation, SpecialForm, SpecificLocator,
standalone_citation, BibliographyDriver, BibliographyRequest, Brackets,
BufWriteFormat, CitationItem, CitationRequest, Elem, ElemChild, ElemChildren,
ElemMeta, Formatted, Formatting, Rendered, RenderedBibliography, RenderedCitation,
SpecialForm, SpecificLocator,
};
pub use selectors::{Selector, SelectorError};

Expand Down Expand Up @@ -523,6 +524,7 @@ entry! {
/// Any serial number or version describing the item that is not appropriate
/// for the fields doi, edition, isbn or issn (may be assigned by the author
/// of the item; especially useful for preprint archives).
#[serde(alias = "serial")]
"serial-number" => serial_number: SerialNumber,
/// The language of the item.
"language" => language: LanguageIdentifier,
Expand Down

0 comments on commit 7b68f3b

Please sign in to comment.