diff --git a/ox-hugo.el b/ox-hugo.el index 6bd9421..8457bbb 100644 --- a/ox-hugo.el +++ b/ox-hugo.el @@ -2368,34 +2368,47 @@ Optional argument FORMAT can be \"toml\" or \"yaml\"." ((and prefer-no-quotes (string-match-p "\\`[a-zA-Z0-9]+\\'" val)) val) - ((and (org-string-nw-p val) - (string-match-p "\n" val)) - ;; The indentation of the multi-line string is needed only for the - ;; YAML format. But the same is done for TOML too just for better - ;; presentation. - (setq val (replace-regexp-in-string "^" " " val)) - - (if (and (stringp format) - (string= format "yaml")) - (progn - ;; https://yaml-multiline.info/ - ;; - ;; | |foo : > - ;; |abc | abc - ;; | >>> | - ;; |def | - ;; | | def - ;; - ;; In Org, a single blank line is used to start a new - ;; paragraph. In the YAML multi-line string, that needs to - ;; be 2 blank lines. - (setq val (replace-regexp-in-string "\n[[:blank:]]*\n" "\n\n\n" val)) - (format ">\n%s" val)) - ;; Remove indentation/space from blank lines if any. - (setq val (replace-regexp-in-string "\n[[:blank:]]*\n" "\n\n" val)) - (format "\"\"\"\n%s\n \"\"\"" val))) ;Triple-quote + ((org-string-nw-p val) ;If `val' is any other non-empty string + (cond + ((string-match-p "\n" val) ;Multi-line string + ;; The indentation of the multi-line string is needed only for the + ;; YAML format. But the same is done for TOML too just for better + ;; presentation. + (setq val (replace-regexp-in-string "^" " " val)) + + (if (and (stringp format) + (string= format "yaml")) + (progn + ;; https://yaml-multiline.info/ + ;; + ;; | |foo : > + ;; |abc | abc + ;; | >>> | + ;; |def | + ;; | | def + ;; + ;; In Org, a single blank line is used to start a new + ;; paragraph. In the YAML multi-line string, that needs to + ;; be 2 blank lines. + (setq val (replace-regexp-in-string "\n[[:blank:]]*\n" "\n\n\n" val)) + (format ">\n%s" val)) + ;; Escape the backslashes (only for multi-line TOML). + (setq val (replace-regexp-in-string "\\\\" "\\\\\\\\" val)) + + ;; Remove indentation/space from blank lines if any. + (setq val (replace-regexp-in-string "\n[[:blank:]]*\n" "\n\n" val)) + (format "\"\"\"\n%s\n \"\"\"" val))) ;Triple-quote + (t ;Single-line string + ;; Below 2 replacements are order-dependent.. first escape the + ;; backslashes, then escape the quotes with backslashes. + + ;; Escape the backslashes (for both TOML and YAML). + (setq val (replace-regexp-in-string "\\\\" "\\\\\\\\" val)) + ;; Escape the double-quotes. + (setq val (replace-regexp-in-string "\"" "\\\\\"" val)) + (concat "\"" val "\"")))) (t - (concat "\"" (replace-regexp-in-string "\"" "\\\\\"" val) "\"")))) + ""))) (defun org-hugo--parse-property-arguments (str) "Return an alist converted from a string STR of Hugo property value. diff --git a/test/site/content-org/all-posts.org b/test/site/content-org/all-posts.org index c627960..6782eb5 100644 --- a/test/site/content-org/all-posts.org +++ b/test/site/content-org/all-posts.org @@ -4552,6 +4552,23 @@ If the description is set via the subtree property #+begin_description Description set in special block #+end_description +***** Single-line description with backslashes (TOML) :escaping:backslashes:front_matter:single_line: +:PROPERTIES: +:EXPORT_FILE_NAME: description-single-line-escaping-backslashes-org-special-block-toml +:END: +#+begin_description +Test to check that backslashes in =\|= and =\\= are correctly escaped. +#+end_description +Post content. +***** Multi-line description with backslashes (TOML) :escaping:backslashes:front_matter:multi_line: +:PROPERTIES: +:EXPORT_FILE_NAME: description-multi-line-escaping-backslashes-org-special-block-toml +:END: +#+begin_description +Test to check that backslashes in =\|= and =\\= are correctly escaped +in the front-matter. +#+end_description +Post content. **** YAML :yaml: :PROPERTIES: :EXPORT_HUGO_FRONT_MATTER_FORMAT: yaml @@ -4575,6 +4592,23 @@ of this post *bold* -- /italics/ --- +strikethrough+ =monospace= #+end_description Post content. +***** Single-line description with backslashes (YAML) :escaping:backslashes:front_matter:single_line: +:PROPERTIES: +:EXPORT_FILE_NAME: description-single-line-escaping-backslashes-org-special-block-yaml +:END: +#+begin_description +Test to check that backslashes in =\|= and =\\= are correctly escaped. +#+end_description +Post content. +***** Multi-line description with backslashes (YAML) :escaping:backslashes:front_matter:multi_line: +:PROPERTIES: +:EXPORT_FILE_NAME: description-multi-line-escaping-backslashes-org-special-block-yaml +:END: +#+begin_description +Test to check that backslashes in =\|= and =\\= are correctly escaped +in the front-matter. +#+end_description +Post content. ** Locale :locale: :PROPERTIES: :EXPORT_HUGO_WITH_LOCALE: t diff --git a/test/site/content/posts/description-multi-line-escaping-backslashes-org-special-block-toml.md b/test/site/content/posts/description-multi-line-escaping-backslashes-org-special-block-toml.md new file mode 100644 index 0000000..131c2d3 --- /dev/null +++ b/test/site/content/posts/description-multi-line-escaping-backslashes-org-special-block-toml.md @@ -0,0 +1,11 @@ ++++ +title = "Multi-line description with backslashes (TOML)" +description = """ + Test to check that backslashes in `\\|` and `\\\\` are correctly escaped + in the front-matter. + """ +tags = ["description", "special-block", "toml", "escaping", "backslashes", "front-matter", "multi-line"] +draft = false ++++ + +Post content. diff --git a/test/site/content/posts/description-multi-line-escaping-backslashes-org-special-block-yaml.md b/test/site/content/posts/description-multi-line-escaping-backslashes-org-special-block-yaml.md new file mode 100644 index 0000000..9ddac2a --- /dev/null +++ b/test/site/content/posts/description-multi-line-escaping-backslashes-org-special-block-yaml.md @@ -0,0 +1,10 @@ +--- +title: "Multi-line description with backslashes (YAML)" +description: > + Test to check that backslashes in `\|` and `\\` are correctly escaped + in the front-matter. +tags: ["description", "special-block", "yaml", "escaping", "backslashes", "front-matter", "multi-line"] +draft: false +--- + +Post content. diff --git a/test/site/content/posts/description-single-line-escaping-backslashes-org-special-block-toml.md b/test/site/content/posts/description-single-line-escaping-backslashes-org-special-block-toml.md new file mode 100644 index 0000000..1883b13 --- /dev/null +++ b/test/site/content/posts/description-single-line-escaping-backslashes-org-special-block-toml.md @@ -0,0 +1,8 @@ ++++ +title = "Single-line description with backslashes (TOML)" +description = "Test to check that backslashes in `\\|` and `\\\\` are correctly escaped." +tags = ["description", "special-block", "toml", "escaping", "backslashes", "front-matter", "single-line"] +draft = false ++++ + +Post content. diff --git a/test/site/content/posts/description-single-line-escaping-backslashes-org-special-block-yaml.md b/test/site/content/posts/description-single-line-escaping-backslashes-org-special-block-yaml.md new file mode 100644 index 0000000..1d96b97 --- /dev/null +++ b/test/site/content/posts/description-single-line-escaping-backslashes-org-special-block-yaml.md @@ -0,0 +1,8 @@ +--- +title: "Single-line description with backslashes (YAML)" +description: "Test to check that backslashes in `\\|` and `\\\\` are correctly escaped." +tags: ["description", "special-block", "yaml", "escaping", "backslashes", "front-matter", "single-line"] +draft: false +--- + +Post content.