use super::prelude::*;
use crate::data::PageRef;
use crate::parsing::UnstructuredParseResult;
pub const BLOCK_INCLUDE_ELEMENTS: BlockRule = BlockRule {
name: "block-include-elements",
accepts_names: &["include-elements"],
accepts_star: false,
accepts_score: false,
accepts_newlines: true,
parse_fn,
};
fn parse_fn<'r, 't>(
parser: &mut Parser<'r, 't>,
name: &'t str,
flag_star: bool,
flag_score: bool,
in_head: bool,
) -> ParseResult<'r, 't, Elements<'t>> {
debug!("Found invalid include-elements block");
parser.check_page_syntax()?;
assert!(!flag_star, "Include (elements) doesn't allow star flag");
assert!(!flag_score, "Include (elements) doesn't allow score flag");
assert_block_name(&BLOCK_INCLUDE_ELEMENTS, name);
let (page_name, variables) =
parser.get_head_name_map(&BLOCK_INCLUDE_ELEMENTS, in_head)?;
let page_ref = match PageRef::parse(page_name) {
Ok(page_ref) => page_ref,
Err(_) => return Err(parser.make_err(ParseErrorKind::BlockMalformedArguments)),
};
let UnstructuredParseResult {
result,
mut html_blocks,
mut code_blocks,
mut table_of_contents_depths,
mut footnotes,
has_footnote_block,
mut bibliographies,
} = include_page(parser, &page_ref)?;
if has_footnote_block {
parser.set_footnote_block();
}
let ParseSuccess {
item: elements,
errors,
paragraph_safe,
..
} = result?;
parser.append_shared_items(
&mut html_blocks,
&mut code_blocks,
&mut table_of_contents_depths,
&mut footnotes,
&mut bibliographies,
);
let variables = variables.to_hash_map();
let element = Element::Include {
paragraph_safe,
variables,
location: page_ref,
elements,
};
ok!(element, errors)
}
fn include_page<'r, 't>(
parser: &Parser<'r, 't>,
_page: &PageRef,
) -> Result<UnstructuredParseResult<'r, 't>, ParseError> {
if false {
return Err(parser.make_err(ParseErrorKind::NoSuchPage));
}
Ok(UnstructuredParseResult {
result: Ok(ParseSuccess::new(
vec![text!("<INCLUDED PAGE (ELEMENTS)>")],
vec![],
false,
)),
html_blocks: vec![],
code_blocks: vec![],
table_of_contents_depths: vec![],
footnotes: vec![],
has_footnote_block: false,
bibliographies: Default::default(),
})
}