Populate render context for footnote references.

This commit is contained in:
Tom Alexander 2023-10-29 12:53:28 -04:00
parent 795945f0da
commit cd27869122
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 42 additions and 6 deletions

View File

@ -9,15 +9,23 @@ use crate::intermediate::IFootnoteReference;
#[derive(Debug, Serialize)]
#[serde(tag = "type")]
#[serde(rename = "footnote_reference")]
pub(crate) struct RenderFootnoteReference {}
pub(crate) struct RenderFootnoteReference {
reference_id: String,
definition_link: String,
label: String,
}
impl RenderFootnoteReference {
pub(crate) fn new(
config: &Config,
output_directory: &Path,
output_file: &Path,
comment: &IFootnoteReference,
original: &IFootnoteReference,
) -> Result<RenderFootnoteReference, CustomError> {
Ok(RenderFootnoteReference {})
Ok(RenderFootnoteReference {
reference_id: original.get_reference_id(),
definition_link: format!("#{}", original.get_definition_id()),
label: original.get_display_label(),
})
}
}

View File

@ -5,6 +5,7 @@ use super::registry::Registry;
#[derive(Debug)]
pub(crate) struct IFootnoteReference {
footnote_id: usize,
duplicate_offset: usize,
}
impl IFootnoteReference {
@ -13,6 +14,33 @@ impl IFootnoteReference {
original: &'intermediate organic::types::FootnoteReference<'parse>,
) -> Result<IFootnoteReference, CustomError> {
let footnote_id = registry.get_footnote_reference_id(original.label, &original.definition);
Ok(IFootnoteReference { footnote_id })
Ok(IFootnoteReference {
footnote_id,
duplicate_offset: 0, // TODO
})
}
pub(crate) fn get_display_label(&self) -> String {
format!("{}", self.footnote_id + 1)
}
/// Get an ID to refer to this footnote reference.
///
/// This ID could, for example, be used for the id attribute in HTML for the reference anchor tag.
pub(crate) fn get_reference_id(&self) -> String {
if self.duplicate_offset == 0 {
format!("fnr.{}", self.get_display_label())
} else {
// 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;
format!("fnr.{}.{}", self.get_display_label(), append)
}
}
/// Get an ID to refer to the footnote definition this footnote reference references.
///
/// This ID could, for example, be used for the id attribute in HTML for the definition anchor tag.
pub(crate) fn get_definition_id(&self) -> String {
format!("fn.{}", self.get_display_label())
}
}

View File

@ -42,7 +42,7 @@ impl<'intermediate, 'parse> Registry<'intermediate, 'parse> {
// If it has no label then it must always get a new ID.
self.footnote_ids
.push((None, FootnoteDefinitionContents::FromReference(definition)));
return self.footnote_ids.len();
return self.footnote_ids.len() - 1;
}
if let Some(existing_id) = self
@ -61,7 +61,7 @@ impl<'intermediate, 'parse> Registry<'intermediate, 'parse> {
} else {
self.footnote_ids
.push((label, FootnoteDefinitionContents::FromReference(definition)));
self.footnote_ids.len()
self.footnote_ids.len() - 1
}
}