Skip to content

Commit

Permalink
Prevent error if front-matter key to be replaced does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Jan 25, 2018
1 parent fb21e82 commit 1aa2536
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -1198,8 +1198,10 @@ Note that:
(dolist (repl repl-alist)
(let ((key-orig (car repl))
(key-repl (cdr repl)))
;; https://emacs.stackexchange.com/a/3398/115
(setf (car (assoc key-orig data)) key-repl)))))
(let ((found-key-cell (assoc key-orig data)))
(when found-key-cell
;; https://emacs.stackexchange.com/a/3398/115
(setf (car found-key-cell) key-repl)))))))
data))


Expand Down
15 changes: 15 additions & 0 deletions test/site/content-org/all-posts.org
Original file line number Diff line number Diff line change
Expand Up @@ -3788,6 +3788,21 @@ This post will have the =tags= key in front-matter replaced with
:END:
This post will have the =tags= and =categories= keys in the
front-matter swapped.
*** Replace custom keys :custom:
:PROPERTIES:
:EXPORT_FILE_NAME: replace-custom-keys
:EXPORT_HUGO_CUSTOM_FRONT_MATTER: :foo 1 :bar 2
:EXPORT_HUGO_FRONT_MATTER_KEY_REPLACE: foo>myfoo bar>mybar
:END:
This post will have the =foo= key in front-matter replaced with
=myfoo= and =bar= replaced with =mybar=.
*** Attempt to replace non-existing keys :non_existing:
:PROPERTIES:
:EXPORT_FILE_NAME: replace-non-existing-keys
:EXPORT_HUGO_FRONT_MATTER_KEY_REPLACE: toto>zulu
:END:
Here, attempt is made to replace a non-existing key =toto=, but still,
there shouldn't be any error.
* Real Examples :real_examples:
:PROPERTIES:
:EXPORT_HUGO_SECTION: real-examples
Expand Down
10 changes: 10 additions & 0 deletions test/site/content/posts/replace-custom-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
+++
title = "Replace custom keys"
tags = ["front-matter", "keys", "replace", "custom"]
draft = false
myfoo = 1
mybar = 2
+++

This post will have the `foo` key in front-matter replaced with
`myfoo` and `bar` replaced with `mybar`.
8 changes: 8 additions & 0 deletions test/site/content/posts/replace-non-existing-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
+++
title = "Attempt to replace non-existing keys"
tags = ["front-matter", "keys", "replace", "non-existing"]
draft = false
+++

Here, attempt is made to replace a non-existing key `toto`, but still,
there shouldn't be any error.

0 comments on commit 1aa2536

Please sign in to comment.