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.

133 lines
4.1KB

  1. use std::collections::HashMap;
  2. use base64::{encode, decode};
  3. use pulldown_cmark as cmark;
  4. use tera::{Value, to_value, Result as TeraResult};
  5. pub fn markdown(value: Value, args: HashMap<String, Value>) -> TeraResult<Value> {
  6. let s = try_get_value!("markdown", "value", String, value);
  7. let inline = match args.get("inline") {
  8. Some(val) => try_get_value!("markdown", "inline", bool, val),
  9. None => false,
  10. };
  11. let mut opts = cmark::Options::empty();
  12. opts.insert(cmark::OPTION_ENABLE_TABLES);
  13. opts.insert(cmark::OPTION_ENABLE_FOOTNOTES);
  14. let mut html = String::new();
  15. let parser = cmark::Parser::new_ext(&s, opts);
  16. cmark::html::push_html(&mut html, parser);
  17. if inline {
  18. html = html
  19. .trim_left_matches("<p>")
  20. // pulldown_cmark finishes a paragraph with `</p>\n`
  21. .trim_right_matches("</p>\n")
  22. .to_string();
  23. }
  24. Ok(to_value(&html).unwrap())
  25. }
  26. pub fn base64_encode(value: Value, _: HashMap<String, Value>) -> TeraResult<Value> {
  27. let s = try_get_value!("base64_encode", "value", String, value);
  28. Ok(
  29. to_value(&encode(s.as_bytes())).unwrap()
  30. )
  31. }
  32. pub fn base64_decode(value: Value, _: HashMap<String, Value>) -> TeraResult<Value> {
  33. let s = try_get_value!("base64_decode", "value", String, value);
  34. Ok(
  35. to_value(
  36. &String::from_utf8(
  37. decode(s.as_bytes()).unwrap()
  38. ).unwrap()
  39. ).unwrap()
  40. )
  41. }
  42. #[cfg(test)]
  43. mod tests {
  44. use std::collections::HashMap;
  45. use tera::to_value;
  46. use super::{markdown, base64_decode, base64_encode};
  47. #[test]
  48. fn markdown_filter() {
  49. let result = markdown(to_value(&"# Hey").unwrap(), HashMap::new());
  50. assert!(result.is_ok());
  51. assert_eq!(result.unwrap(), to_value(&"<h1>Hey</h1>\n").unwrap());
  52. }
  53. #[test]
  54. fn markdown_filter_inline() {
  55. let mut args = HashMap::new();
  56. args.insert("inline".to_string(), to_value(true).unwrap());
  57. let result = markdown(to_value(&"Using `map`, `filter`, and `fold` instead of `for`").unwrap(), args);
  58. assert!(result.is_ok());
  59. assert_eq!(result.unwrap(), to_value(&"Using <code>map</code>, <code>filter</code>, and <code>fold</code> instead of <code>for</code>").unwrap());
  60. }
  61. // https://github.com/Keats/gutenberg/issues/417
  62. #[test]
  63. fn markdown_filter_inline_tables() {
  64. let mut args = HashMap::new();
  65. args.insert("inline".to_string(), to_value(true).unwrap());
  66. let result = markdown(to_value(&r#"
  67. |id|author_id| timestamp_created|title |content |
  68. |-:|--------:|-----------------------:|:---------------------|:-----------------|
  69. | 1| 1|2018-09-05 08:03:43.141Z|How to train your ORM |Badly written blog|
  70. | 2| 1|2018-08-22 13:11:50.050Z|How to bake a nice pie|Badly written blog|
  71. "#).unwrap(), args);
  72. assert!(result.is_ok());
  73. assert!(result.unwrap().as_str().unwrap().contains("<table>"));
  74. }
  75. #[test]
  76. fn base64_encode_filter() {
  77. // from https://tools.ietf.org/html/rfc4648#section-10
  78. let tests = vec![
  79. ("", ""),
  80. ("f", "Zg=="),
  81. ("fo", "Zm8="),
  82. ("foo", "Zm9v"),
  83. ("foob", "Zm9vYg=="),
  84. ("fooba", "Zm9vYmE="),
  85. ("foobar", "Zm9vYmFy")
  86. ];
  87. for (input, expected) in tests {
  88. let args = HashMap::new();
  89. let result = base64_encode(to_value(input).unwrap(), args);
  90. assert!(result.is_ok());
  91. assert_eq!(result.unwrap(), to_value(expected).unwrap());
  92. }
  93. }
  94. #[test]
  95. fn base64_decode_filter() {
  96. let tests = vec![
  97. ("", ""),
  98. ("Zg==", "f"),
  99. ("Zm8=", "fo"),
  100. ("Zm9v", "foo"),
  101. ("Zm9vYg==", "foob"),
  102. ("Zm9vYmE=", "fooba"),
  103. ("Zm9vYmFy", "foobar")
  104. ];
  105. for (input, expected) in tests {
  106. let args = HashMap::new();
  107. let result = base64_decode(to_value(input).unwrap(), args);
  108. assert!(result.is_ok());
  109. assert_eq!(result.unwrap(), to_value(expected).unwrap());
  110. }
  111. }
  112. }