diff --git a/src/intermediate/footnote_definition.rs b/src/intermediate/footnote_definition.rs index 601472b..f6da6c3 100644 --- a/src/intermediate/footnote_definition.rs +++ b/src/intermediate/footnote_definition.rs @@ -8,8 +8,9 @@ pub(crate) struct IFootnoteDefinition {} impl IFootnoteDefinition { pub(crate) async fn new<'intermediate, 'parse>( registry: &mut Registry<'intermediate, 'parse>, - original: &organic::types::FootnoteDefinition<'parse>, + original: &'intermediate organic::types::FootnoteDefinition<'parse>, ) -> Result { + registry.register_footnote_definition(original.label, &original.children); Ok(IFootnoteDefinition {}) } } diff --git a/src/intermediate/registry.rs b/src/intermediate/registry.rs index 9996893..70bb158 100644 --- a/src/intermediate/registry.rs +++ b/src/intermediate/registry.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +use organic::types::Element; use organic::types::Object; type IdCounter = u16; @@ -7,7 +8,10 @@ type IdCounter = u16; pub(crate) struct Registry<'intermediate, 'parse> { id_counter: IdCounter, targets: HashMap<&'parse str, String>, - footnote_ids: Vec<(Option<&'parse str>, &'intermediate Vec>)>, + footnote_ids: Vec<( + Option<&'parse str>, + FootnoteDefinitionContents<'intermediate, 'parse>, + )>, } impl<'intermediate, 'parse> Registry<'intermediate, 'parse> { @@ -36,7 +40,8 @@ impl<'intermediate, 'parse> Registry<'intermediate, 'parse> { ) -> usize { if let None = label { // If it has no label then it must always get a new ID. - self.footnote_ids.push((None, definition)); + self.footnote_ids + .push((None, FootnoteDefinitionContents::FromReference(definition))); return self.footnote_ids.len(); } @@ -50,12 +55,34 @@ impl<'intermediate, 'parse> Registry<'intermediate, 'parse> { .footnote_ids .get_mut(existing_id) .expect("If-statement proves this to be Some."); - entry.1 = definition; + entry.1 = FootnoteDefinitionContents::FromReference(definition); } existing_id } else { - self.footnote_ids.push((label, definition)); + self.footnote_ids + .push((label, FootnoteDefinitionContents::FromReference(definition))); self.footnote_ids.len() } } + + /// Update the definition to a footnote but do not mark it as referenced. + pub(crate) fn register_footnote_definition<'b>( + &'b mut self, + label: &'parse str, + definition: &'intermediate Vec>, + ) { + if let Some((existing_id, existing_definition)) = self + .footnote_ids + .iter_mut() + .find(|(id, _definition)| *id == Some(label)) + { + *existing_definition = FootnoteDefinitionContents::FromDefinition(definition); + } + } +} + +#[derive(Debug)] +pub(crate) enum FootnoteDefinitionContents<'intermediate, 'parse> { + FromReference(&'intermediate Vec>), + FromDefinition(&'intermediate Vec>), }