use super::prelude::*;
use crate::tree::{Container, ContainerType, HtmlTag};
pub fn render_container(ctx: &mut HtmlContext, container: &Container) {
debug!("Rendering container '{}'", container.ctype().name());
match container.ctype() {
ContainerType::RubyText => {
ctx.html().rp().contents("(");
render_container_internal(ctx, container);
ctx.html().rp().contents(")");
}
_ => render_container_internal(ctx, container),
}
}
pub fn render_container_internal(ctx: &mut HtmlContext, container: &Container) {
let layout = ctx.layout();
let tag_spec = container.ctype().html_tag(layout, ctx);
let random_id = choose_id(ctx, &tag_spec);
let mut tag = ctx.html().tag(tag_spec.tag());
match tag_spec {
HtmlTag::Tag(_) => tag.attr(attr!(;; container.attributes())),
HtmlTag::TagAndClass { class, .. } => tag.attr(attr!(
"class" => class;;
container.attributes(),
)),
HtmlTag::TagAndStyle { style, .. } => tag.attr(attr!(
"style" => style;;
container.attributes(),
)),
HtmlTag::TagAndId { id, .. } => tag.attr(attr!(
"id" => match random_id {
Some(ref id) => id,
None => &id,
};;
container.attributes(),
)),
};
tag.contents(container.elements());
}
pub fn render_color(ctx: &mut HtmlContext, color: &str, elements: &[Element]) {
debug!("Rendering color container (color '{color}')");
ctx.html()
.span()
.attr(attr!(
"style" => "color: " color ";",
))
.contents(elements);
}
fn choose_id(ctx: &mut HtmlContext, tag_spec: &HtmlTag) -> Option<String> {
if matches!(tag_spec, HtmlTag::TagAndId { .. }) && !ctx.settings().use_true_ids {
Some(ctx.random().generate_html_id())
} else {
None
}
}