Permalink
Browse files

Enable replacing any key in the front-matter

New Org keyword/property:

    #+HUGO_FRONT_MATTER_KEY_REPLACE
    :EXPORT_HUGO_FRONT_MATTER_KEY_REPLACE:

This commit obsoletes the 2 new keywords/properties pairs introduced
in an earlier commit today:
e5e6e87

Earlier approach was hard-coded to replace only tags and categories
keys in the front-matter.

This new approach used in this commit can replace *any* front-matter
key with the string the user wants.

The syntax is:

    #+HUGO_FRONT_MATTER_KEY_REPLACE: oldkey>newkey

You can also do multiple key replacements:

    #+HUGO_FRONT_MATTER_KEY_REPLACE: oldkey1>newkey1 oldkey2>newkey2

Note that:

1. There are no spaces around the special character ">".
2. Spaces are used to only separate multiple replacements are shown in
   the second example above.
3. The replacements are literal.. there are no regular expressions
   involved.
  • Loading branch information...
kaushalmodi committed Jan 24, 2018
1 parent 240dd55 commit b72a5fb05cf19ce145535986f053bd309ecaf188
@@ -664,8 +664,7 @@ newer."
(:hugo-auto-set-lastmod "HUGO_AUTO_SET_LASTMOD" nil org-hugo-auto-set-lastmod)
(:hugo-custom-front-matter "HUGO_CUSTOM_FRONT_MATTER" nil nil)
(:hugo-blackfriday "HUGO_BLACKFRIDAY" nil nil)
(:hugo-front-matter-key-org-tags "HUGO_FRONT_MATTER_KEY_ORG_TAGS" nil "tags")
(:hugo-front-matter-key-org-categories "HUGO_FRONT_MATTER_KEY_ORG_CATEGORIES" nil "categories")
(:hugo-front-matter-key-replace "HUGO_FRONT_MATTER_KEY_REPLACE" nil nil space)
;; Front matter variables
;; https://gohugo.io/content-management/front-matter/#front-matter-variables
@@ -1144,6 +1143,58 @@ cannot be formatted in Hugo-compatible format."
date-nocolon))))
date-str))
(defun org-hugo--replace-keys-maybe (data info)
"Return DATA with its keys replaced, maybe.
The keys in DATA are replaced if HUGO_FRONT_MATTER_KEY_REPLACE is
set appropriately.
The replacement syntax is:
#+HUGO_FRONT_MATTER_KEY_REPLACE: oldkey>newkey
You can also do multiple key replacements:
#+HUGO_FRONT_MATTER_KEY_REPLACE: oldkey1>newkey1 oldkey2>newkey2
Above examples are using the keyword
HUGO_FRONT_MATTER_KEY_REPLACE, but the same also applies when
using its subtree property form
:EXPORT_HUGO_FRONT_MATTER_KEY_REPLACE:.
Note that:
1. There are no spaces around the special character \">\".
2. Spaces are used to only separate multiple replacements are shown in
the second example above.
3. The replacements are literal.. there are no regular expressions
involved."
(let* ((repl-str (plist-get info :hugo-front-matter-key-replace))
(repl-str (when (org-string-nw-p repl-str)
(org-trim repl-str)))
(valid (and repl-str
(string-match-p ">" repl-str))))
(when valid
(let* ((repl-list (split-string repl-str))
(repl-alist (let (alist)
(dolist (repl repl-list)
(when (and (stringp repl)
(string-match-p ">" repl))
(let* ((pair (split-string repl ">"))
(repl-pair (mapcar #'intern pair)))
(push repl-pair alist))))
alist)))
;; (message "[ox-hugo replace-key str DBG] %S" repl-str)
;; (message "[ox-hugo replace-key list DBG] %S" repl-list)
;; (message "[ox-hugo replace-key alist DBG] %S" repl-alist)
(when repl-alist
(dolist (repl repl-alist)
(let ((key-orig (nth 0 repl))
(key-repl (nth 1 repl)))
;; https://emacs.stackexchange.com/a/3398/115
(setf (car (assoc key-orig data)) key-repl))))))
data))
;;; Transcode Functions
@@ -2218,8 +2269,6 @@ INFO is a plist used as a communication channel."
(all-t-and-c-str (org-entry-get (point) "ALLTAGS"))
(all-t-and-c (when (stringp all-t-and-c-str)
(org-split-string all-t-and-c-str ":")))
(tags-key (intern
(plist-get info :hugo-front-matter-key-org-tags)))
(tags (or
;; Look for tags set using #+HUGO_TAGS keyword, or
;; EXPORT_HUGO_TAGS property if available.
@@ -2231,8 +2280,6 @@ INFO is a plist used as a communication channel."
(tags-list (org-hugo--transform-org-tags tags-list info)))
;; (message "[get fm DBG] tags: tags-list = %S" tags-list)
tags-list)))
(categories-key (intern
(plist-get info :hugo-front-matter-key-org-categories)))
(categories (or
;; Look for categories set using
;; #+HUGO_CATEGORIES keyword, or
@@ -2294,8 +2341,8 @@ INFO is a plist used as a communication channel."
(markup . ,(org-export-data (plist-get info :hugo-markup) info))
(outputs . ,outputs)
(slug . ,(org-export-data (plist-get info :hugo-slug) info))
(,tags-key . ,tags)
(,categories-key . ,categories)
(tags . ,tags)
(categories . ,categories)
(type . ,(org-export-data (plist-get info :hugo-type) info))
(url . ,(org-export-data (plist-get info :hugo-url) info))
(weight . ,weight)
@@ -2316,11 +2363,10 @@ INFO is a plist used as a communication channel."
;; (message "[custom fm data DBG] %S" custom-fm-data)
;; (message "[fm resources OUT DBG] %S" resources)
;; (message "[fm data DBG] %S" data)
;; (message "[fm tags key DBG] %s" tags-key)
;; (message "[fm categories key DBG] %s" categories-key)
;; (message "[fm tags DBG] %S" tags)
;; (message "[fm categories DBG] %S" categories)
;; (message "[fm keywords DBG] %S" keywords)
(setq data (org-hugo--replace-keys-maybe data info))
(org-hugo--gen-front-matter data fm-format)))
(defun org-hugo--calc-weight ()
@@ -3743,21 +3743,44 @@ comma-separated.
:EXPORT_KEYWORDS: abc
:EXPORT_KEYWORDS+: def
:END:
** Alternative front-matter keys :alternative:keys:
*** Alternative "tags" key :tags:
** Replace front-matter keys :keys:replace:
*** Replace only "linkTitle" key :linktitle:
:PROPERTIES:
:EXPORT_FILE_NAME: alternative-tags-key
:EXPORT_HUGO_FRONT_MATTER_KEY_ORG_TAGS: keywords
:EXPORT_FILE_NAME: replace-only-linktitle-key
:EXPORT_HUGO_LINKTITLE: Replace only "linkTitle" key
:EXPORT_HUGO_FRONT_MATTER_KEY_REPLACE: linkTitle>linktitle
:END:
This post will have the =tags= key in front-matter renamed to
This post will have the =linkTitle= key in front-matter replaced with
=linktitle=.
*** Replace only "tags" key :tags:
:PROPERTIES:
:EXPORT_FILE_NAME: replace-only-tags-key
:EXPORT_HUGO_FRONT_MATTER_KEY_REPLACE: tags>keywords
:END:
This post will have the =tags= key in front-matter replaced with
=keywords=.
*** Alternative "categories" key :categories:@test_cat_x:
*** Replace only "categories" key :categories:@test_cat_x:
:PROPERTIES:
:EXPORT_FILE_NAME: alternative-categories-key
:EXPORT_HUGO_FRONT_MATTER_KEY_ORG_CATEGORIES: cats
:EXPORT_FILE_NAME: replace-only-categories-key
:EXPORT_HUGO_FRONT_MATTER_KEY_REPLACE: categories>cats
:END:
This post will have the =categories= key in front-matter renamed to
This post will have the =categories= key in front-matter replaced with
=cats=.
*** Replace both "tags" and "categories" keys :tags:categories:@test_cat_x:
:PROPERTIES:
:EXPORT_FILE_NAME: replace-both-tags-and-categories-keys
:EXPORT_HUGO_FRONT_MATTER_KEY_REPLACE: tags>keywords categories>cats
:END:
This post will have the =tags= key in front-matter replaced with
=keywords= and =categories= replaced with =cats=.
*** Replace keys using multiple properties :@test_cat_x:tags:categories:
:PROPERTIES:
:EXPORT_FILE_NAME: replace-keys-using-multiple-properties
:EXPORT_HUGO_FRONT_MATTER_KEY_REPLACE: tags>keywords
:EXPORT_HUGO_FRONT_MATTER_KEY_REPLACE+: categories>cats
:END:
This post will have the =tags= key in front-matter replaced with
=keywords= and =categories= replaced with =cats=.
* Real Examples :real_examples:
:PROPERTIES:
:EXPORT_HUGO_SECTION: real-examples

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,9 @@
+++
title = "Replace both \"tags\" and \"categories\" keys"
keywords = ["front-matter", "keys", "replace", "tags", "categories"]
cats = ["test-cat-x"]
draft = false
+++
This post will have the `tags` key in front-matter replaced with
`keywords` and `categories` replaced with `cats`.
@@ -0,0 +1,9 @@
+++
title = "Replace keys using multiple properties"
keywords = ["front-matter", "keys", "replace", "tags", "categories"]
cats = ["test-cat-x"]
draft = false
+++
This post will have the `tags` key in front-matter replaced with
`keywords` and `categories` replaced with `cats`.
@@ -0,0 +1,9 @@
+++
title = "Replace only \"categories\" key"
tags = ["front-matter", "keys", "replace", "categories"]
cats = ["test-cat-x"]
draft = false
+++
This post will have the `categories` key in front-matter replaced with
`cats`.
@@ -0,0 +1,9 @@
+++
title = "Replace only \"linkTitle\" key"
linktitle = "Replace only \"linkTitle\" key"
tags = ["front-matter", "keys", "replace", "linktitle"]
draft = false
+++
This post will have the `linkTitle` key in front-matter replaced with
`linktitle`.
@@ -0,0 +1,8 @@
+++
title = "Replace only \"tags\" key"
keywords = ["front-matter", "keys", "replace", "tags"]
draft = false
+++
This post will have the `tags` key in front-matter replaced with
`keywords`.

0 comments on commit b72a5fb

Please sign in to comment.