mod common; use common::ShortCode; use std::path::PathBuf; use templates::ZOLA_TERA; macro_rules! test_scenario { ($in_str:literal, $out_str:literal, [$($shortcodes:ident),*]) => { let config = config::Config::default_for_test(); #[allow(unused_mut)] let mut tera = tera::Tera::default(); tera.extend(&ZOLA_TERA).unwrap(); $( let ShortCode { name, is_md, output } = $shortcodes; tera.add_raw_template( &format!("shortcodes/{}.{}", name, if is_md { "md" } else { "html" }), &output, ).unwrap(); )* let mut permalinks = std::collections::HashMap::new(); permalinks.insert("".to_string(), "".to_string()); tera.register_filter( "markdown", templates::filters::MarkdownFilter::new( PathBuf::new(), config.clone(), permalinks.clone(), ).unwrap() ); let mut context = rendering::RenderContext::new( &tera, &config, &config.default_language, "", &permalinks, front_matter::InsertAnchor::None, ); let shortcode_def = utils::templates::get_shortcodes(&tera); context.set_shortcode_definitions(&shortcode_def); let rendered = rendering::render_content($in_str, &context); println!("{:?}", rendered); assert!(rendered.is_ok()); let rendered = rendered.unwrap(); assert_eq!(rendered.body, $out_str.to_string()); } } #[test] fn plain_text() { // Test basic formation of text and paragraphs tags // - Plain sentences (Long and broken up) // - Multiple paragraphs test_scenario!("Hello World!", "
Hello World!
\n", []); test_scenario!("Hello\nWorld!", "Hello\nWorld!
\n", []); test_scenario!("Hello\n\nWorld!", "Hello
\nWorld!
\n", []); test_scenario!("Hello\n\nWorld\n\n!", "Hello
\nWorld
\n!
\n", []); } #[test] fn header() { // Test basic header ids // - Plain headers // - Headers with text test_scenario!("# Header1", "Hello World!
\n", [] ); test_scenario!( "# Header1\n\nHello World!", "Hello World!
\n", [] ); } #[test] fn code_block() { test_scenario!("```\nWow Code!\n```", "Wow Code!\n
\n", []);
test_scenario!(" Wow Code!", "Wow Code!
\n", []);
}
const MD_SIMPLE: ShortCode = ShortCode::new("simple", "Hello World!", true);
const HTML_SIMPLE: ShortCode = ShortCode::new("simple", "Hello World!", false);
#[test]
fn simple_shortcodes() {
// Test both MD & HTML plain text shortcodes
test_scenario!("{{ simple() }}", "Hello World!
\n", [MD_SIMPLE]); test_scenario!("hey {{ simple() }}", "hey Hello World!
\n", [HTML_SIMPLE]); } const MD_LINK: ShortCode = ShortCode::new("link", "[Link...](/)", true); const HTML_LINK: ShortCode = ShortCode::new("link", "Link...", false); #[test] fn md_inline_shortcodes() { // Test both MD & HTML inline shortcodes test_scenario!( "A read more link: {{ link() }}", "A read more link: Link...
\n", [MD_LINK] ); test_scenario!( "A read more link: {{ link() }}", "A read more link: Link...
\n", [HTML_LINK] ); } const HTML_DIV: ShortCode = ShortCode::new("dived", "Hello World!
\n", [MD_OUTER, MD_INNER]); // test_scenario!("{{ outer() }}", "Hello World!
\n", [MD_INNER, MD_OUTER]); // } // // #[test] // fn html_recursive_shortcodes() { // // Test recursive shortcodes in a HTML context. // // One can add HTML shortcodes within html shortcodes, unless a shortcode is reused // // test_scenario!("{{ outer() }}", "Hello {{ inner() }}!
", [HTML_OUTER, HTML_INNER]); // test_scenario!("{{ outer() }}", "Hello {{ inner() }}!
", [HTML_INNER, HTML_OUTER]); // } // // #[test] // fn shortcodes_recursion_stop() { // // Test whether recursion stops if a shortcode is reused. // // test_scenario_fail!("{{ reuser() }}", [MD_REUSER]); // test_scenario_fail!("{{ leveledreuser() }}", [MD_LEVELED_REUSER, MD_REFBACK]); // // test_scenario_fail!("{{ reuser() }}", [HTML_REUSER]); // test_scenario_fail!("{{ leveledreuser() }}", [HTML_LEVELED_REUSER, HTML_REFBACK]); // // test_scenario_fail!("{{ leveledreuser() }}", [HTML_LEVELED_REUSER, MD_REFBACK]); // test_scenario_fail!("{{ leveledreuser() }}", [MD_LEVELED_REUSER, HTML_REFBACK]); // } // // #[test] // fn html_in_md_recursive_shortcodes() { // // Test whether we can properly add HTML shortcodes in MD shortcodes // // test_scenario!("{{ outer() }}", "Hello World!
\n", [HTML_INNER, MD_OUTER]); // test_scenario!("{{ outer() }}", "Hello World!
\n", [MD_OUTER, HTML_INNER]); // } // // #[test] // fn md_in_html_recursive_shortcodes() { // // Test whether we can not add MD shortcodes in HTML shortcodes // // test_scenario!("{{ outer() }}", "Hello {{ inner() }}!
", [HTML_OUTER, MD_INNER]); // test_scenario!("{{ outer() }}", "Hello {{ inner() }}!
", [MD_INNER, HTML_OUTER]); // } const MD_BODY_SHORTCODE: ShortCode = ShortCode::new("bdy", "*{{ body }}*", true); const HTML_BODY_SHORTCODE: ShortCode = ShortCode::new("bdy", "{{ body }}", false); #[test] fn md_body_shortcodes() { // Test basic MD body shortcodes test_scenario!("abc {% bdy() %}def{% end %}", "abc def
\n", [MD_BODY_SHORTCODE]); test_scenario!( "abc\n\n{% bdy() %}def{% end %}", "abc
\ndef
\n", [MD_BODY_SHORTCODE] ); } #[test] fn html_body_shortcodes() { // Test basic HTML body shortcodes test_scenario!( "abc {% bdy() %}def{% end %}", "abc def
\n", [HTML_BODY_SHORTCODE] ); // Should it wrap the shortcode in a ``? test_scenario!( "abc\n\n{% bdy() %}def{% end %}", "
abc
\ndef", [HTML_BODY_SHORTCODE] ); } // Related to issue #515 // #[test] // fn shortcode_in_md_body() { // // Test whether we can properly insert a shortcode in a MD shortcode body // // test_scenario!( // "{% bdy() %}Wow! {{ simple() }}{% end %}", // "Wow! Hello World!
\n", // [MD_BODY_SHORTCODE, MD_SIMPLE] // ); // // test_scenario!( // "{% bdy() %}Wow! {{ simple() }}{% end %}", // "Wow! Hello World!
\n", // [MD_SIMPLE, MD_BODY_SHORTCODE] // ); // // test_scenario!( // "{% bdy() %}Wow! {{ simple() }}{% end %}", // "Wow! Hello World!
\n", // [MD_BODY_SHORTCODE, HTML_SIMPLE] // ); // // test_scenario!( // "{% bdy() %}Wow! {{ simple() }}{% end %}", // "Wow! Hello World!
\n", // [HTML_SIMPLE, MD_BODY_SHORTCODE] // ); // } // // #[test] // fn shortcode_in_html_body() { // // Test whether we can properly insert a shortcode in a HTML shortcode body // // test_scenario!( // "{% bdy() %}Wow! {{ simple() }}{% end %}", // "Wow! Hello World!
\n", // [HTML_BODY_SHORTCODE, MD_SIMPLE] // ); // // test_scenario!( // "{% bdy() %}Wow! {{ simple() }}{% end %}", // "Wow! Hello World!
\n", // [MD_SIMPLE, HTML_BODY_SHORTCODE] // ); // // test_scenario!( // "{% bdy() %}Wow! {{ simple() }}{% end %}", // "Wow! Hello World!
\n", // [HTML_BODY_SHORTCODE, HTML_SIMPLE] // ); // // test_scenario!( // "{% bdy() %}Wow! {{ simple() }}{% end %}", // "Wow! Hello World!
\n", // [HTML_SIMPLE, HTML_BODY_SHORTCODE] // ); // } const MD_ARG_SHORTCODE: ShortCode = ShortCode::new( "argeater", "{{ s }}\n{{ b }}\n{{ f }}\n{{ i }}\n{{ array | join(sep=' // ') | safe }}", true, ); const HTML_ARG_SHORTCODE: ShortCode = ShortCode::new( "argeater", "{{ s }}\n{{ b }}\n{{ f }}\n{{ i }}\n{{ array | join(sep=' // ') | safe }}", false, ); #[test] fn shortcode_arguments() { // Test for properly inserting all shortcodes test_scenario!( "{{ argeater(s='Hello World!', b=true, f=3.1415, i=42, array=[1, 3, 3, 7]) }}", "Hello World!\ntrue\n3.1415\n42\n1 // 3 // 3 // 7
\n", [MD_ARG_SHORTCODE] ); test_scenario!( "{{ argeater(s='Hello World!', b=true, f=3.1415, i=42, array=[1, 3, 3, 7]) }}", "Hello World!\ntrue\n3.1415\n42\n1 // 3 // 3 // 7", [HTML_ARG_SHORTCODE] ); } // const MD_BDY_OUTER: ShortCode = ShortCode::new("outer", "*{{ body }}*", true); // const MD_BDY_INNER: ShortCode = ShortCode::new("inner", "**{{ body }}**", true); // Originally from PR #1475 // #[test] // fn body_in_body() { // test_scenario!( // "{% outer() %}\n\tTest text\n\t{% inner() %}\n\t\tHello World!\n\t{% end %}\n{% end %}", // "Test text Hello World!
\n", // [MD_BDY_OUTER, MD_BDY_INNER] // ); // } // https://github.com/getzola/zola/issues/1355 #[test] fn list_with_shortcode() { test_scenario!( "* a\n* b\n\t{{ multiline() }}\n*c\n\t{{ multiline() }}\n", "{{ body | safe}}
some code;\nmore code;\n\nother code here;
\nsome code\n
\n\n\n\n", [] ); } const GOOGLE_SHORTCODE: ShortCode = ShortCode::new( "google", r#""#, false, ); // https://github.com/getzola/zola/issues/1500 #[test] fn can_handle_issue_1500() { test_scenario!( r#"foo {{ google(query="apple") }} bar."#, "test quote\n
\n \n\n\ntest quote\n
foo