use super::prelude::*;
use crate::tree::Bibliography;
pub fn render_bibcite(ctx: &mut HtmlContext, label: &str, brackets: bool) {
debug!("Rendering bibliography citation (label {label}, brackets {brackets})");
match ctx.get_bibliography_ref(label) {
Some((index, contents)) => {
let reference_string = ctx
.handle()
.get_message(ctx.language(), "bibliography-reference");
let label = format!("{reference_string} {index}.");
ctx.html()
.span()
.attr(attr!("class" => "wj-bibliography-ref"))
.inner(|ctx| {
let id = str!(index);
if brackets {
ctx.push_raw('[');
}
ctx.html()
.element("wj-bibliography-ref-marker")
.attr(attr!(
"class" => "wj-bibliography-ref-marker",
"role" => "link",
"aria-label" => &label,
"data-id" => &id,
))
.contents(&id);
if brackets {
ctx.push_raw(']');
}
ctx.html()
.span()
.attr(attr!(
"class" => "wj-bibliography-ref-tooltip",
"aria-hidden" => "true",
))
.inner(|ctx| {
ctx.html()
.span()
.attr(
attr!("class" => "wj-bibliography-ref-tooltip-label"),
)
.contents(&label);
ctx.html()
.span()
.attr(attr!("class" => "wj-bibliography-ref-contents"))
.contents(contents);
});
});
}
None => {
let message = ctx
.handle()
.get_message(ctx.language(), "bibliography-cite-not-found");
ctx.html()
.span()
.attr(attr!("class" => "wj-error-inline"))
.contents(message);
}
}
}
pub fn render_bibliography(
ctx: &mut HtmlContext,
title: Option<&str>,
bibliography_index: usize,
bibliography: &Bibliography,
) {
debug!(
"Rendering bibliography block (title {}, items {})",
title.unwrap_or("<default>"),
bibliography.slice().len(),
);
let title_default;
let title: &str = match title {
Some(title) => title,
None => {
title_default = ctx
.handle()
.get_message(ctx.language(), "bibliography-block-title");
title_default
}
};
ctx.html()
.div()
.attr(attr!("class" => "wj-bibliography bibitems"))
.inner(|ctx| {
ctx.html()
.div()
.attr(attr!("class" => "wj-bibliography-title title"))
.contents(title);
let mut id = String::new();
for (entry_index, (_, elements)) in bibliography.slice().iter().enumerate() {
let bibliography_index = bibliography_index + 1;
let entry_index = entry_index + 1;
id.clear();
str_write!(
id,
"wj-bibliography-item-{}-{} bibitem-{}-{}",
bibliography_index,
entry_index,
bibliography_index,
entry_index,
);
ctx.html()
.div()
.attr(attr!("class" => "wj-bibliography-item bibitem", "id" => &id))
.inner(|ctx| {
ctx.html()
.element("wj-bibliography-item-marker")
.attr(attr!(
"class" => "wj-bibliography-item-marker",
"type" => "button",
"role" => "link",
))
.inner(|ctx| {
str_write!(ctx, "{entry_index}");
ctx.html()
.span()
.attr(attr!("class" => "wj-bibliography-sep"))
.contents(".");
});
render_elements(ctx, elements);
});
}
});
}