Assign the reference counts.

This commit is contained in:
Tom Alexander 2023-10-29 22:27:47 -04:00
parent ff03140007
commit 0da37b25e3
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 20 additions and 9 deletions

View File

@ -10,11 +10,11 @@ pub(crate) struct IFootnoteReference {
} }
intermediate!(IFootnoteReference, FootnoteReference, original, registry, { intermediate!(IFootnoteReference, FootnoteReference, original, registry, {
let footnote_id = let (footnote_id, reference_count) =
get_footnote_reference_id(registry, original.label, &original.definition).await?; get_footnote_reference_id(registry, original.label, &original.definition).await?;
Ok(IFootnoteReference { Ok(IFootnoteReference {
footnote_id, footnote_id,
duplicate_offset: 0, // TODO duplicate_offset: reference_count,
}) })
}); });
@ -31,7 +31,7 @@ impl IFootnoteReference {
format!("fnr.{}", self.get_display_label()) format!("fnr.{}", self.get_display_label())
} else { } else {
// Org-mode makes all duplicates use "100" but I figure there is no harm in giving each a unique ID. // Org-mode makes all duplicates use "100" but I figure there is no harm in giving each a unique ID.
let append = 100 + self.duplicate_offset; let append = 100 + self.duplicate_offset - 1;
format!("fnr.{}.{}", self.get_display_label(), append) format!("fnr.{}.{}", self.get_display_label(), append)
} }
} }

View File

@ -13,6 +13,7 @@ pub(crate) struct Registry<'orig, 'parse> {
id_counter: IdCounter, id_counter: IdCounter,
targets: HashMap<&'parse str, String>, targets: HashMap<&'parse str, String>,
footnote_ids: Vec<(Option<&'parse str>, Vec<IAstNode>)>, footnote_ids: Vec<(Option<&'parse str>, Vec<IAstNode>)>,
footnote_reference_counts: HashMap<&'parse str, usize>,
on_deck_footnote_ids: HashMap<&'parse str, &'orig Vec<Element<'parse>>>, on_deck_footnote_ids: HashMap<&'parse str, &'orig Vec<Element<'parse>>>,
} }
@ -22,6 +23,7 @@ impl<'orig, 'parse> Registry<'orig, 'parse> {
id_counter: 0, id_counter: 0,
targets: HashMap::new(), targets: HashMap::new(),
footnote_ids: Vec::new(), footnote_ids: Vec::new(),
footnote_reference_counts: HashMap::new(),
on_deck_footnote_ids: HashMap::new(), on_deck_footnote_ids: HashMap::new(),
} }
} }
@ -48,7 +50,7 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
registry: RefRegistry<'orig, 'parse>, registry: RefRegistry<'orig, 'parse>,
label: Option<&'parse str>, label: Option<&'parse str>,
definition: &'orig Vec<Object<'parse>>, definition: &'orig Vec<Object<'parse>>,
) -> Result<usize, CustomError> { ) -> Result<(usize, usize), CustomError> {
if let None = label { if let None = label {
// If it has no label then it must always get a new ID. // If it has no label then it must always get a new ID.
let contents = convert_reference_contents(registry.clone(), definition).await?; let contents = convert_reference_contents(registry.clone(), definition).await?;
@ -57,12 +59,21 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
registry.footnote_ids.push((None, contents)); registry.footnote_ids.push((None, contents));
registry.footnote_ids.len() - 1 registry.footnote_ids.len() - 1
}; };
return Ok(pos); return Ok((pos, 0));
} }
if let Some(label) = label { let reference_count = if let Some(label) = label {
promote_footnote_definition(registry.clone(), label).await?; promote_footnote_definition(registry.clone(), label).await?;
} let mut registry = registry.lock().unwrap();
let reference_count = registry
.footnote_reference_counts
.entry(label)
.and_modify(|count| *count += 1)
.or_insert(0);
*reference_count
} else {
0
};
let existing_index = registry let existing_index = registry
.lock() .lock()
@ -80,7 +91,7 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
.expect("If-statement proves this to be Some."); .expect("If-statement proves this to be Some.");
entry.1 = contents; entry.1 = contents;
} }
Ok(existing_id) Ok((existing_id, reference_count))
} else { } else {
let existing_id = { let existing_id = {
let mut registry = registry.lock().unwrap(); let mut registry = registry.lock().unwrap();
@ -96,7 +107,7 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
.expect("If-statement proves this to be Some."); .expect("If-statement proves this to be Some.");
entry.1 = contents; entry.1 = contents;
} }
Ok(existing_id) Ok((existing_id, reference_count))
} }
} }