You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

416 lines
15KB

  1. use pest::iterators::Pair;
  2. use pest::Parser;
  3. use regex::Regex;
  4. use tera::{to_value, Context, Map, Value};
  5. use context::RenderContext;
  6. use errors::{Result, Error};
  7. // This include forces recompiling this source file if the grammar file changes.
  8. // Uncomment it when doing changes to the .pest file
  9. const _GRAMMAR: &str = include_str!("content.pest");
  10. #[derive(Parser)]
  11. #[grammar = "content.pest"]
  12. pub struct ContentParser;
  13. lazy_static! {
  14. static ref MULTIPLE_NEWLINE_RE: Regex = Regex::new(r"\n\s*\n").unwrap();
  15. }
  16. fn replace_string_markers(input: &str) -> String {
  17. match input.chars().next().unwrap() {
  18. '"' => input.replace('"', ""),
  19. '\'' => input.replace('\'', ""),
  20. '`' => input.replace('`', ""),
  21. _ => unreachable!("How did you even get there"),
  22. }
  23. }
  24. fn parse_literal(pair: Pair<Rule>) -> Value {
  25. let mut val = None;
  26. for p in pair.into_inner() {
  27. match p.as_rule() {
  28. Rule::boolean => match p.as_str() {
  29. "true" => val = Some(Value::Bool(true)),
  30. "false" => val = Some(Value::Bool(false)),
  31. _ => unreachable!(),
  32. },
  33. Rule::string => val = Some(Value::String(replace_string_markers(p.as_str()))),
  34. Rule::float => {
  35. val = Some(to_value(p.as_str().parse::<f64>().unwrap()).unwrap());
  36. }
  37. Rule::int => {
  38. val = Some(to_value(p.as_str().parse::<i64>().unwrap()).unwrap());
  39. }
  40. _ => unreachable!("Unknown literal: {:?}", p),
  41. };
  42. }
  43. val.unwrap()
  44. }
  45. /// Returns (shortcode_name, kwargs)
  46. fn parse_shortcode_call(pair: Pair<Rule>) -> (String, Map<String, Value>) {
  47. let mut name = None;
  48. let mut args = Map::new();
  49. for p in pair.into_inner() {
  50. match p.as_rule() {
  51. Rule::ident => {
  52. name = Some(p.into_span().as_str().to_string());
  53. }
  54. Rule::kwarg => {
  55. let mut arg_name = None;
  56. let mut arg_val = None;
  57. for p2 in p.into_inner() {
  58. match p2.as_rule() {
  59. Rule::ident => {
  60. arg_name = Some(p2.into_span().as_str().to_string());
  61. }
  62. Rule::literal => {
  63. arg_val = Some(parse_literal(p2));
  64. }
  65. Rule::array => {
  66. let mut vals = vec![];
  67. for p3 in p2.into_inner() {
  68. match p3.as_rule() {
  69. Rule::literal => vals.push(parse_literal(p3)),
  70. _ => unreachable!(
  71. "Got something other than literal in an array: {:?}",
  72. p3
  73. ),
  74. }
  75. }
  76. arg_val = Some(Value::Array(vals));
  77. }
  78. _ => unreachable!("Got something unexpected in a kwarg: {:?}", p2),
  79. }
  80. }
  81. args.insert(arg_name.unwrap(), arg_val.unwrap());
  82. }
  83. _ => unreachable!("Got something unexpected in a shortcode: {:?}", p),
  84. }
  85. }
  86. (name.unwrap(), args)
  87. }
  88. fn render_shortcode(
  89. name: &str,
  90. args: &Map<String, Value>,
  91. context: &RenderContext,
  92. body: Option<&str>,
  93. ) -> Result<String> {
  94. let mut tera_context = Context::new();
  95. for (key, value) in args.iter() {
  96. tera_context.insert(key, value);
  97. }
  98. if let Some(ref b) = body {
  99. // Trimming right to avoid most shortcodes with bodies ending up with a HTML new line
  100. tera_context.insert("body", b.trim_right());
  101. }
  102. tera_context.extend(context.tera_context.clone());
  103. let tpl_name = format!("shortcodes/{}.html", name);
  104. let res = context
  105. .tera
  106. .render(&tpl_name, &tera_context)
  107. .map_err(|e| Error::chain(format!("Failed to render {} shortcode", name), e))?;
  108. // Small hack to avoid having multiple blank lines because of Tera tags for example
  109. // A blank like will cause the markdown parser to think we're out of HTML and start looking
  110. // at indentation, making the output a code block.
  111. let res = MULTIPLE_NEWLINE_RE.replace_all(&res, "\n");
  112. Ok(res.to_string())
  113. }
  114. pub fn render_shortcodes(content: &str, context: &RenderContext) -> Result<String> {
  115. let mut res = String::with_capacity(content.len());
  116. let mut pairs = match ContentParser::parse(Rule::page, content) {
  117. Ok(p) => p,
  118. Err(e) => {
  119. let fancy_e = e.renamed_rules(|rule| match *rule {
  120. Rule::int => "an integer".to_string(),
  121. Rule::float => "a float".to_string(),
  122. Rule::string => "a string".to_string(),
  123. Rule::literal => "a literal (int, float, string, bool)".to_string(),
  124. Rule::array => "an array".to_string(),
  125. Rule::kwarg => "a keyword argument".to_string(),
  126. Rule::ident => "an identifier".to_string(),
  127. Rule::inline_shortcode => "an inline shortcode".to_string(),
  128. Rule::ignored_inline_shortcode => "an ignored inline shortcode".to_string(),
  129. Rule::sc_body_start => "the start of a shortcode".to_string(),
  130. Rule::ignored_sc_body_start => "the start of an ignored shortcode".to_string(),
  131. Rule::text => "some text".to_string(),
  132. Rule::EOI => "end of input".to_string(),
  133. Rule::double_quoted_string => "double quoted string".to_string(),
  134. Rule::single_quoted_string => "single quoted string".to_string(),
  135. Rule::backquoted_quoted_string => "backquoted quoted string".to_string(),
  136. Rule::boolean => "a boolean (true, false)".to_string(),
  137. Rule::all_chars => "a alphanumerical character".to_string(),
  138. Rule::kwargs => "a list of keyword arguments".to_string(),
  139. Rule::sc_def => "a shortcode definition".to_string(),
  140. Rule::shortcode_with_body => "a shortcode with body".to_string(),
  141. Rule::ignored_shortcode_with_body => "an ignored shortcode with body".to_string(),
  142. Rule::sc_body_end => "{% end %}".to_string(),
  143. Rule::ignored_sc_body_end => "{%/* end */%}".to_string(),
  144. Rule::text_in_body_sc => "text in a shortcode body".to_string(),
  145. Rule::text_in_ignored_body_sc => "text in an ignored shortcode body".to_string(),
  146. Rule::content => "some content".to_string(),
  147. Rule::page => "a page".to_string(),
  148. Rule::WHITESPACE => "whitespace".to_string(),
  149. });
  150. bail!("{}", fancy_e);
  151. }
  152. };
  153. // We have at least a `page` pair
  154. for p in pairs.next().unwrap().into_inner() {
  155. match p.as_rule() {
  156. Rule::text => res.push_str(p.into_span().as_str()),
  157. Rule::inline_shortcode => {
  158. let (name, args) = parse_shortcode_call(p);
  159. res.push_str(&render_shortcode(&name, &args, context, None)?);
  160. }
  161. Rule::shortcode_with_body => {
  162. let mut inner = p.into_inner();
  163. // 3 items in inner: call, body, end
  164. // we don't care about the closing tag
  165. let (name, args) = parse_shortcode_call(inner.next().unwrap());
  166. let body = inner.next().unwrap().into_span().as_str();
  167. res.push_str(&render_shortcode(&name, &args, context, Some(body))?);
  168. }
  169. Rule::ignored_inline_shortcode => {
  170. res.push_str(
  171. &p.into_span().as_str().replacen("{{/*", "{{", 1).replacen("*/}}", "}}", 1),
  172. );
  173. }
  174. Rule::ignored_shortcode_with_body => {
  175. for p2 in p.into_inner() {
  176. match p2.as_rule() {
  177. Rule::ignored_sc_body_start | Rule::ignored_sc_body_end => {
  178. res.push_str(
  179. &p2.into_span()
  180. .as_str()
  181. .replacen("{%/*", "{%", 1)
  182. .replacen("*/%}", "%}", 1),
  183. );
  184. }
  185. Rule::text_in_ignored_body_sc => res.push_str(p2.into_span().as_str()),
  186. _ => unreachable!("Got something weird in an ignored shortcode: {:?}", p2),
  187. }
  188. }
  189. }
  190. Rule::EOI => (),
  191. _ => unreachable!("unexpected page rule: {:?}", p.as_rule()),
  192. }
  193. }
  194. Ok(res)
  195. }
  196. #[cfg(test)]
  197. mod tests {
  198. use std::collections::HashMap;
  199. use super::*;
  200. use config::Config;
  201. use front_matter::InsertAnchor;
  202. use tera::Tera;
  203. macro_rules! assert_lex_rule {
  204. ($rule: expr, $input: expr) => {
  205. let res = ContentParser::parse($rule, $input);
  206. println!("{:?}", $input);
  207. println!("{:#?}", res);
  208. if res.is_err() {
  209. println!("{}", res.unwrap_err());
  210. panic!();
  211. }
  212. assert!(res.is_ok());
  213. assert_eq!(res.unwrap().last().unwrap().into_span().end(), $input.len());
  214. };
  215. }
  216. fn render_shortcodes(code: &str, tera: &Tera) -> String {
  217. let config = Config::default();
  218. let permalinks = HashMap::new();
  219. let context = RenderContext::new(&tera, &config, "", &permalinks, InsertAnchor::None);
  220. super::render_shortcodes(code, &context).unwrap()
  221. }
  222. #[test]
  223. fn lex_text() {
  224. let inputs = vec!["Hello world", "HEllo \n world", "Hello 1 2 true false 'hey'"];
  225. for i in inputs {
  226. assert_lex_rule!(Rule::text, i);
  227. }
  228. }
  229. #[test]
  230. fn lex_inline_shortcode() {
  231. let inputs = vec![
  232. "{{ youtube() }}",
  233. "{{ youtube(id=1, autoplay=true, url='hey') }}",
  234. "{{ youtube(id=1, \nautoplay=true, url='hey') }}",
  235. ];
  236. for i in inputs {
  237. assert_lex_rule!(Rule::inline_shortcode, i);
  238. }
  239. }
  240. #[test]
  241. fn lex_inline_ignored_shortcode() {
  242. let inputs = vec![
  243. "{{/* youtube() */}}",
  244. "{{/* youtube(id=1, autoplay=true, url='hey') */}}",
  245. "{{/* youtube(id=1, \nautoplay=true, \nurl='hey') */}}",
  246. ];
  247. for i in inputs {
  248. assert_lex_rule!(Rule::ignored_inline_shortcode, i);
  249. }
  250. }
  251. #[test]
  252. fn lex_shortcode_with_body() {
  253. let inputs = vec![
  254. r#"{% youtube() %}
  255. Some text
  256. {% end %}"#,
  257. r#"{% youtube(id=1,
  258. autoplay=true, url='hey') %}
  259. Some text
  260. {% end %}"#,
  261. ];
  262. for i in inputs {
  263. assert_lex_rule!(Rule::shortcode_with_body, i);
  264. }
  265. }
  266. #[test]
  267. fn lex_ignored_shortcode_with_body() {
  268. let inputs = vec![
  269. r#"{%/* youtube() */%}
  270. Some text
  271. {%/* end */%}"#,
  272. r#"{%/* youtube(id=1,
  273. autoplay=true, url='hey') */%}
  274. Some text
  275. {%/* end */%}"#,
  276. ];
  277. for i in inputs {
  278. assert_lex_rule!(Rule::ignored_shortcode_with_body, i);
  279. }
  280. }
  281. #[test]
  282. fn lex_page() {
  283. let inputs = vec![
  284. "Some text and a shortcode `{{/* youtube() */}}`",
  285. "{{ youtube(id=1, autoplay=true, url='hey') }}",
  286. "{{ youtube(id=1, \nautoplay=true, url='hey') }} that's it",
  287. r#"
  288. This is a test
  289. {% hello() %}
  290. Body {{ var }}
  291. {% end %}
  292. "#,
  293. ];
  294. for i in inputs {
  295. assert_lex_rule!(Rule::page, i);
  296. }
  297. }
  298. #[test]
  299. fn does_nothing_with_no_shortcodes() {
  300. let res = render_shortcodes("Hello World", &Tera::default());
  301. assert_eq!(res, "Hello World");
  302. }
  303. #[test]
  304. fn can_unignore_inline_shortcode() {
  305. let res = render_shortcodes("Hello World {{/* youtube() */}}", &Tera::default());
  306. assert_eq!(res, "Hello World {{ youtube() }}");
  307. }
  308. #[test]
  309. fn can_unignore_shortcode_with_body() {
  310. let res = render_shortcodes(
  311. r#"
  312. Hello World
  313. {%/* youtube() */%}Some body {{ hello() }}{%/* end */%}"#,
  314. &Tera::default(),
  315. );
  316. assert_eq!(res, "\nHello World\n{% youtube() %}Some body {{ hello() }}{% end %}");
  317. }
  318. // https://github.com/Keats/gutenberg/issues/383
  319. #[test]
  320. fn unignore_shortcode_with_body_does_not_swallow_initial_whitespace() {
  321. let res = render_shortcodes(
  322. r#"
  323. Hello World
  324. {%/* youtube() */%}
  325. Some body {{ hello() }}{%/* end */%}"#,
  326. &Tera::default(),
  327. );
  328. assert_eq!(res, "\nHello World\n{% youtube() %}\nSome body {{ hello() }}{% end %}");
  329. }
  330. #[test]
  331. fn can_parse_shortcode_arguments() {
  332. let inputs = vec![
  333. ("{{ youtube() }}", "youtube", Map::new()),
  334. ("{{ youtube(id=1, autoplay=true, hello='salut', float=1.2) }}", "youtube", {
  335. let mut m = Map::new();
  336. m.insert("id".to_string(), to_value(1).unwrap());
  337. m.insert("autoplay".to_string(), to_value(true).unwrap());
  338. m.insert("hello".to_string(), to_value("salut").unwrap());
  339. m.insert("float".to_string(), to_value(1.2).unwrap());
  340. m
  341. }),
  342. ("{{ gallery(photos=['something', 'else'], fullscreen=true) }}", "gallery", {
  343. let mut m = Map::new();
  344. m.insert("photos".to_string(), to_value(["something", "else"]).unwrap());
  345. m.insert("fullscreen".to_string(), to_value(true).unwrap());
  346. m
  347. }),
  348. ];
  349. for (i, n, a) in inputs {
  350. let mut res = ContentParser::parse(Rule::inline_shortcode, i).unwrap();
  351. let (name, args) = parse_shortcode_call(res.next().unwrap());
  352. assert_eq!(name, n);
  353. assert_eq!(args, a);
  354. }
  355. }
  356. #[test]
  357. fn can_render_inline_shortcodes() {
  358. let mut tera = Tera::default();
  359. tera.add_raw_template("shortcodes/youtube.html", "Hello {{id}}").unwrap();
  360. let res = render_shortcodes("Inline {{ youtube(id=1) }}.", &tera);
  361. assert_eq!(res, "Inline Hello 1.");
  362. }
  363. #[test]
  364. fn can_render_shortcodes_with_body() {
  365. let mut tera = Tera::default();
  366. tera.add_raw_template("shortcodes/youtube.html", "{{body}}").unwrap();
  367. let res = render_shortcodes("Body\n {% youtube() %}Hey!{% end %}", &tera);
  368. assert_eq!(res, "Body\n Hey!");
  369. }
  370. // https://github.com/Keats/gutenberg/issues/462
  371. #[test]
  372. fn shortcodes_with_body_do_not_eat_newlines() {
  373. let mut tera = Tera::default();
  374. tera.add_raw_template("shortcodes/youtube.html", "{{body | safe}}").unwrap();
  375. let res = render_shortcodes("Body\n {% youtube() %}\nHello \n World{% end %}", &tera);
  376. assert_eq!(res, "Body\n Hello \n World");
  377. }
  378. }