Beginning to hand out footnote ids.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use organic::types::Object;
|
||||
|
||||
type IdCounter = u16;
|
||||
|
||||
pub(crate) struct Registry<'parse> {
|
||||
pub(crate) struct Registry<'intermediate, 'parse> {
|
||||
id_counter: IdCounter,
|
||||
targets: HashMap<&'parse str, String>,
|
||||
footnote_ids: Vec<(Option<&'parse str>, &Vec<Object<'parse>>)>,
|
||||
}
|
||||
|
||||
impl<'parse> Registry<'parse> {
|
||||
@@ -12,6 +15,7 @@ impl<'parse> Registry<'parse> {
|
||||
Registry {
|
||||
id_counter: 0,
|
||||
targets: HashMap::new(),
|
||||
footnote_ids: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +25,37 @@ impl<'parse> Registry<'parse> {
|
||||
format!("target_{}", self.id_counter)
|
||||
})
|
||||
}
|
||||
|
||||
/// Get a 0-indexed ID for a footnote.
|
||||
///
|
||||
/// This needs to be incremented to be 1-indexed for render.
|
||||
pub(crate) fn get_footnote_reference_id<'b>(
|
||||
&'b mut self,
|
||||
label: Option<&'parse str>,
|
||||
definition: &'parse Vec<Object<'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));
|
||||
return self.footnote_ids.len();
|
||||
}
|
||||
|
||||
if let Some(existing_id) = self
|
||||
.footnote_ids
|
||||
.iter()
|
||||
.position(|(id, _definition)| *id == label)
|
||||
{
|
||||
if !definition.is_empty() {
|
||||
let entry = self
|
||||
.footnote_ids
|
||||
.get_mut(existing_id)
|
||||
.expect("If-statement proves this to be Some.");
|
||||
entry.1 = definition;
|
||||
}
|
||||
existing_id
|
||||
} else {
|
||||
self.footnote_ids.push((label, definition));
|
||||
self.footnote_ids.len()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user