Skip to main content

mdbook_angular/codeblock/
flags.rs

1#[derive(PartialEq, Eq, Debug)]
2pub(super) enum CodeBlockFlags {
3	/// Do not show the source code
4	Hide,
5	/// Show the source code collapsed
6	Collapsed,
7	/// Show the source code uncollapsed
8	Uncollapsed,
9
10	/// Show a playground even if configuration disables them
11	Playground,
12	/// Do not show a playground even if configuration allows them
13	NoPlayground,
14
15	/// Do not insert the Angular root element into the page
16	NoInsert,
17}
18
19fn to_flag(value: &str) -> Option<CodeBlockFlags> {
20	match value {
21		"hide" => Some(CodeBlockFlags::Hide),
22		"playground" => Some(CodeBlockFlags::Playground),
23		"noplayground" | "no-playground" => Some(CodeBlockFlags::NoPlayground),
24		"uncollapsed" | "no-collapse" => Some(CodeBlockFlags::Uncollapsed),
25		"collapsed" | "collapse" => Some(CodeBlockFlags::Collapsed),
26		"no-insert" => Some(CodeBlockFlags::NoInsert),
27		_ => None,
28	}
29}
30
31fn is_flag_separator(c: char) -> bool {
32	c == ',' || c == ' '
33}
34
35/// Extract flags from the given string
36///
37/// The text should contain flags separated by space or comma.
38/// Unknown flags are ignored.
39pub(super) fn get_flags(string: &str) -> Vec<CodeBlockFlags> {
40	string
41		.split(is_flag_separator)
42		.filter_map(to_flag)
43		.collect()
44}