Register footnote definitions.
This commit is contained in:
		
							parent
							
								
									cb7c28c1ae
								
							
						
					
					
						commit
						795945f0da
					
				| @ -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<IFootnoteDefinition, CustomError> { | ||||
|         registry.register_footnote_definition(original.label, &original.children); | ||||
|         Ok(IFootnoteDefinition {}) | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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<Object<'parse>>)>, | ||||
|     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<Element<'parse>>, | ||||
|     ) { | ||||
|         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<Object<'parse>>), | ||||
|     FromDefinition(&'intermediate Vec<Element<'parse>>), | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Tom Alexander
						Tom Alexander