Skip to content

Commit

Permalink
Pass formatting for transparent
Browse files Browse the repository at this point in the history
  • Loading branch information
reknih committed Oct 30, 2023
1 parent 111acd8 commit a217517
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 51 deletions.
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ unic-langid = { version = "0.9.0", features = ["serde"] }
unicode-segmentation = "1.6.0"
unscanny = "0.1.0"
url = { version = "2.4", features = ["serde"] }
biblatex = { version = "0.8", optional = true }
biblatex = { version = "0.8.1", optional = true }
ciborium = { version = "0.2.1", optional = true }
clap = { version = "3.1", optional = true, features = ["cargo"] }
rkyv = { version = "0.7.42", optional = true }
Expand All @@ -45,6 +45,3 @@ required-features = ["cli"]
name = "citeproc"
path = "tests/citeproc.rs"
required-features = ["csl-json"]

[patch.crates-io]
biblatex = { git = "https://github.com/typst/biblatex" }
49 changes: 13 additions & 36 deletions src/csl/elem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Elem {
ElemChild::Elem(e) => e.str_len(),
ElemChild::Markup(m) => m.len(),
ElemChild::Link { text, .. } => text.text.len(),
ElemChild::Transparent(_) => 0,
ElemChild::Transparent { .. } => 0,
})
.sum()
}
Expand Down Expand Up @@ -181,24 +181,6 @@ impl ElemChildren {
None
}

/// Remove the first child with a matching meta by DFS.
pub(super) fn remove_meta(&mut self, meta: ElemMeta) -> bool {
for i in 0..self.0.len() {
if let ElemChild::Elem(e) = &mut self.0[i] {
if e.meta == Some(meta) {
self.0.remove(i);
return true;
}

if e.children.remove_meta(meta) {
return true;
}
}
}

false
}

/// Remove the first child with any meta by DFS.
pub(super) fn remove_any_meta(&mut self) -> Option<ElemChild> {
for i in 0..self.0.len() {
Expand Down Expand Up @@ -237,16 +219,6 @@ impl ElemChildren {
pub(super) fn last_text(&self) -> Option<&Formatted> {
last_text_child(&self.0)
}

/// Get the last character of the last text leaf.
pub(super) fn last_char(&self) -> Option<char> {
self.last_text().and_then(|t| t.text.chars().last())
}

/// Remove the last character of the last text leaf.
pub(super) fn pop_char(&mut self) -> Option<char> {
self.last_text_mut().and_then(|t| t.text.pop())
}
}

pub(crate) fn last_text_mut_child(children: &mut [ElemChild]) -> Option<&mut Formatted> {
Expand Down Expand Up @@ -291,9 +263,14 @@ pub enum ElemChild {
/// The URL.
url: String,
},
/// A transparent element that must be replaced by the consumer. It contains
/// the original index of the cite request.
Transparent(usize),
/// A transparent element that must be replaced by the consumer.
Transparent {
/// The original index of the cite request so the consumer can look up
/// the right citation item.
cite_idx: usize,
/// The formatting of the element.
format: Formatting,
},
}

impl ElemChild {
Expand Down Expand Up @@ -326,7 +303,7 @@ impl ElemChild {
w.write_str(&text.text)?;
text.formatting.write_end(w, format)
}
ElemChild::Transparent(_) => Ok(()),
ElemChild::Transparent { .. } => Ok(()),
}
}

Expand All @@ -336,7 +313,7 @@ impl ElemChild {
ElemChild::Elem(e) => e.str_len(),
ElemChild::Markup(m) => m.len(),
ElemChild::Link { text, .. } => text.text.len(),
ElemChild::Transparent(_) => 0,
ElemChild::Transparent { .. } => 0,
}
}

Expand All @@ -347,7 +324,7 @@ impl ElemChild {
}
ElemChild::Elem(e) => e.has_content(),
ElemChild::Link { .. } => true,
ElemChild::Transparent(_) => true,
ElemChild::Transparent { .. } => true,
}
}

Expand All @@ -358,7 +335,7 @@ impl ElemChild {
}
ElemChild::Elem(e) => e.is_empty(),
ElemChild::Link { text, .. } => text.text.is_empty(),
ElemChild::Transparent(_) => false,
ElemChild::Transparent { .. } => false,
}
}
}
Expand Down
34 changes: 24 additions & 10 deletions src/csl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl<'a, T: EntryLike + Hash + PartialEq + Eq + Debug> BibliographyDriver<'a, T>
//
// If we have set the disambiguation state for an item, we need to set
// the same state for all entries referencing that item.
for _ in 0..5 {
for _ in 0..6 {
let ambiguous = find_ambiguous_sets(&res);
if ambiguous.is_empty() {
break;
Expand Down Expand Up @@ -269,9 +269,12 @@ impl<'a, T: EntryLike + Hash + PartialEq + Eq + Debug> BibliographyDriver<'a, T>
cite.request
.style
.citation
.collapse
.is_some()
.then_some(Citation::DEFAULT_CITE_GROUP_DELIMITER)
.layout
.delimiter
.as_deref()
.filter(|_| {
cite.request.style.citation.collapse.is_some()
})
},
)
else {
Expand Down Expand Up @@ -939,6 +942,7 @@ enum CollapseVerdict {
/// Only the first date should be printed.
First,
/// Only the year suffix should be printed.
#[allow(dead_code)]
YearSuffix,
}

Expand Down Expand Up @@ -1078,7 +1082,7 @@ impl<'a> StyleContext<'a> {
None,
),
style: self,
writing: WritingContext::new(),
writing: WritingContext::new(self.csl.settings.options.clone()),
}
}

Expand All @@ -1099,7 +1103,7 @@ impl<'a> StyleContext<'a> {
None,
),
style: self,
writing: WritingContext::new(),
writing: WritingContext::new(self.csl.settings.options.clone()),
}
}

Expand Down Expand Up @@ -1614,8 +1618,11 @@ impl Default for WritingContext {
}

impl WritingContext {
fn new() -> Self {
Self::default()
fn new(options: InheritableNameOptions) -> Self {
Self {
name_options: NonEmptyStack::new(options),
..Self::default()
}
}

/// Retrieve the current formatting.
Expand Down Expand Up @@ -1710,7 +1717,9 @@ impl WritingContext {
Some(ElemChild::Text(_)) => false,
Some(ElemChild::Elem(e)) => e.has_content(),
Some(
ElemChild::Markup(_) | ElemChild::Link { .. } | ElemChild::Transparent(_),
ElemChild::Markup(_)
| ElemChild::Link { .. }
| ElemChild::Transparent { .. },
) => true,
None => false,
};
Expand Down Expand Up @@ -2337,8 +2346,13 @@ impl<'a, T: EntryLike> Context<'a, T> {

/// Push a transparent element child into the buffer.
pub fn push_transparent(&mut self, idx: usize) {
let format = *self.writing.formatting();
self.writing.save_to_block();
self.writing.elem_stack.last_mut().0.push(ElemChild::Transparent(idx))
self.writing
.elem_stack
.last_mut()
.0
.push(ElemChild::Transparent { cite_idx: idx, format })
}

/// Folds all remaining elements into the first element and returns it.
Expand Down
2 changes: 1 addition & 1 deletion tests/citeproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ fn test_parse_tests() {
#[ignore]
fn test_single_file() {
let locales = locales();
let name = "name_CiteGroupDelimiterWithYearSuffixCollapse.txt";
let name = "nameattr_DelimiterPrecedesLastOnCitationInCitation.txt";
let test_path = PathBuf::from(CACHE_PATH)
.join(TEST_REPO_NAME)
.join("processor-tests/humans/");
Expand Down

0 comments on commit a217517

Please sign in to comment.