natter/src/intermediate/footnote_reference.rs
2023-12-21 15:09:13 -05:00

63 lines
2.2 KiB
Rust

use super::macros::intermediate;
use super::registry::get_footnote_reference_id;
use crate::error::CustomError;
use organic::types::StandardProperties;
#[derive(Debug, Clone)]
pub(crate) struct IFootnoteReference {
footnote_id: usize,
duplicate_offset: usize,
pub(crate) post_blank: organic::types::PostBlank,
}
intermediate!(
IFootnoteReference,
&'orig organic::types::FootnoteReference<'parse>,
original,
intermediate_context,
{
let (footnote_id, reference_count) =
get_footnote_reference_id(intermediate_context, original.label, &original.definition)
.await?;
Ok(IFootnoteReference {
footnote_id,
duplicate_offset: reference_count,
post_blank: original.get_post_blank(),
})
}
);
impl IFootnoteReference {
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, id_addition: Option<&str>) -> String {
let id_addition = id_addition
.map(|id_addition| format!("sec{}.", id_addition))
.unwrap_or(String::default());
if self.duplicate_offset == 0 {
format!("{}fnr.{}", id_addition, 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 - 1;
format!("{}fnr.{}.{}", id_addition, 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, id_addition: Option<&str>) -> String {
let id_addition = id_addition
.map(|id_addition| format!("sec{}.", id_addition))
.unwrap_or(String::default());
format!("{}fn.{}", id_addition, self.get_display_label())
}
}