use super::prelude::*;
use crate::parsing::{ParseWarning, ParseWarningKind};
pub const BLOCK_COLLAPSIBLE: BlockRule = BlockRule {
name: "block-collapsible",
accepts_names: &["collapsible"],
accepts_special: false,
accepts_newlines: true,
parse_fn,
};
fn parse_fn<'r, 't>(
log: &slog::Logger,
parser: &mut Parser<'r, 't>,
name: &'t str,
special: bool,
in_head: bool,
) -> ParseResult<'r, 't, Elements<'t>> {
debug!(
log,
"Parsing collapsible block";
"in-head" => in_head,
);
assert_eq!(special, false, "Collapsible doesn't allow special variant");
assert_block_name(&BLOCK_COLLAPSIBLE, name);
let mut arguments = parser.get_head_map(&BLOCK_COLLAPSIBLE, in_head)?;
let show_text = arguments.get("show");
let hide_text = arguments.get("hide");
let start_open = !arguments.get_bool(parser, "folded")?.unwrap_or(true);
let (show_top, show_bottom) = match arguments.get("hideLocation") {
Some(value) => parse_hide_location(&value, parser)?,
None => (true, false),
};
let (elements, exceptions) =
parser.get_body_elements(&BLOCK_COLLAPSIBLE, true)?.into();
let element = Element::Collapsible {
elements,
attributes: arguments.to_hash_map(),
start_open,
show_text,
hide_text,
show_top,
show_bottom,
};
ok!(element, exceptions)
}
fn parse_hide_location(s: &str, parser: &Parser) -> Result<(bool, bool), ParseWarning> {
const NAMES: [(&str, (bool, bool)); 5] = [
("top", (true, false)),
("bottom", (false, true)),
("both", (true, true)),
("neither", (false, false)),
("none", (false, false)),
];
let s = s.trim();
for &(name, value) in &NAMES {
if name.eq_ignore_ascii_case(s) {
return Ok(value);
}
}
debug!(&parser.log(), "Unknown hideLocation argument"; "value" => s);
Err(parser.make_warn(ParseWarningKind::BlockMalformedArguments))
}