Skip to content

Commit

Permalink
Enable replacing any key in the front-matter
Browse files Browse the repository at this point in the history
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 b72a5fb
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 36 deletions.
66 changes: 56 additions & 10 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 ()
Expand Down
41 changes: 32 additions & 9 deletions test/site/content-org/all-posts.org
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions test/site/content/posts/alternative-categories-key.md

This file was deleted.

8 changes: 0 additions & 8 deletions test/site/content/posts/alternative-tags-key.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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`.
Original file line number Diff line number Diff line change
@@ -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`.
9 changes: 9 additions & 0 deletions test/site/content/posts/replace-only-categories-key.md
Original file line number Diff line number Diff line change
@@ -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`.
9 changes: 9 additions & 0 deletions test/site/content/posts/replace-only-linktitle-key.md
Original file line number Diff line number Diff line change
@@ -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`.
8 changes: 8 additions & 0 deletions test/site/content/posts/replace-only-tags-key.md
Original file line number Diff line number Diff line change
@@ -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.