Skip to content

Commit

Permalink
Add tags hierarchy support
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Jan 5, 2022
1 parent e05c713 commit de5dfdf
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 0 deletions.
50 changes: 50 additions & 0 deletions ox-hugo.el
Expand Up @@ -3537,6 +3537,56 @@ INFO is a plist used as a communication channel."
;; (message "[fm tags DBG] %S" tags)
;; (message "[fm categories DBG] %S" categories)
;; (message "[fm keywords DBG] %S" keywords)

;; Append group tags to user-set tags if tag groups are defined in
;; the buffer.
(when (and org-group-tags org-tag-groups-alist)
(let (tag-groups-alist-mod)

;; Copy `org-tag-groups-alist' to `tag-groups-alist-mod' while
;; modifying the tags and categories as defined by
;; `org-hugo-tag-processing-functions'.
(dolist (group org-tag-groups-alist)
(let ((group-mod group))
(dolist (fn org-hugo-tag-processing-functions group-mod)
(setq group-mod (funcall fn group-mod info)))
(push group-mod tag-groups-alist-mod)))

(dolist (t-or-c (append tags categories))
(let ((to-be-searched `(,t-or-c)))
(while (> (length to-be-searched) 0)
;; (message "[tag group DBG] t and c to search: %S" to-be-searched)
(let ((tc (pop to-be-searched)))
(dolist (group tag-groups-alist-mod)
;; (message "[tag group DBG] Searching %s in %S" tc group)
(when (member tc group)
(let ((head-tag (car group)))
(if (org-hugo--category-p head-tag)
(let ((head-cat (replace-regexp-in-string "\\`@" "" head-tag)))
(unless (member head-cat categories)
(push head-cat categories)
;; (message "[tag group DBG] .... Adding cat %s" head-cat)
))
(unless (member head-tag tags)
(push head-tag tags)
;; (message "[tag group DBG] .... Adding tag %s" head-tag)
))
;; Add the current `head-tag' as the new tag to
;; search if current tag or category (`tc') is not
;; the `head-tag', and if it's not already in the
;; search list.
(unless (or (string= tc head-tag)
(member head-tag to-be-searched))
(push head-tag to-be-searched))))))))))
;; (message "[tag group DBG] updated tags: %S" tags)
;; (message "[tag group DBG] updated categories: %S" categories)

;; Overwrite the 'tags and 'categories key values in `data' with
;; the updated values.
;; https://stackoverflow.com/a/40815365/1219634
(setf (alist-get 'tags data) tags)
(setf (alist-get 'categories data) categories))

(setq data (org-hugo--replace-keys-maybe data info))
(setq ret (org-hugo--gen-front-matter data fm-format))
(if (and (string= "toml" fm-format)
Expand Down
67 changes: 67 additions & 0 deletions test/site/content-org/tag-hierarchy.org
@@ -0,0 +1,67 @@
#+hugo_base_dir: ../

#+seq_todo: TODO DRAFT DONE

#+property: header-args :eval never-export

#+startup: indent

#+macro: doc [[https://ox-hugo.scripter.co/doc/$1][$2]]
#+macro: oxhugoissue =ox-hugo= Issue #[[https://github.com/kaushalmodi/ox-hugo/issues/$1][$1]]

#+author:

#+tags: [ @GTD : Control Persp ]
#+tags: [ Control : Context Task ]
#+tags: [ Persp : Vision Goal AOF Project ]

#+tags: [ @food : @fruit @vegetable @has__seeds ]
#+tags: [ @fruit : apple banana ]
#+tags: [ apple : pink_lady granny_smith ]
#+tags: [ @vegetable : potato squash ]
#+tags: [ @has__seeds : apple squash ]

#+filetags: tag-groups tag-hierarchy

* Body
{{{oxhugoissue(341)}}}

https://orgmode.org/manual/Tag-Hierarchy.html
* Tag Hierarchy: Basic :Vision:
:PROPERTIES:
:EXPORT_FILE_NAME: tag-hierarchy-basic
:END:
#+begin_description
Test the example tag groups from
https://orgmode.org/manual/Tag-Hierarchy.html.
#+end_description
#+include: "./tag-hierarchy.org::*Body" :only-contents t
* Tag Hierarchy: Multiple Tags from Groups :Vision:AOF:Persp:Context:
:PROPERTIES:
:EXPORT_FILE_NAME: tag-hierarchy-multiple-tags
:END:
#+begin_description
Test multiple tags from tag groups -- including adding tags which are
already group tags of some other tags.
#+end_description
#+include: "./tag-hierarchy.org::*Body" :only-contents t

Here, ~Persp~ tag is added manually, but it is also the group head for
~Vision~ and ~AOF~ tags.
* Tag Hierarchy: Tags with Multiple Groups :squash:
:PROPERTIES:
:EXPORT_FILE_NAME: tag-hierarchy-tags-with-multiple-group-heads
:END:
#+begin_description
Test tags which belong in multiple groups and so they have multiple
group head tags.
#+end_description
#+include: "./tag-hierarchy.org::*Body" :only-contents t
* Tag Hierarchy: Tags with hyphens and spaces :pink_lady:
:PROPERTIES:
:EXPORT_FILE_NAME: tag-hierarchy-tags-with-hyphens-and-spaces
:END:
#+begin_description
Test tags containing hyphens and spaces
#+end_description
#+include: "./tag-hierarchy.org::*Body" :only-contents t
14 changes: 14 additions & 0 deletions test/site/content/posts/tag-hierarchy-basic.md
@@ -0,0 +1,14 @@
+++
title = "Tag Hierarchy: Basic"
description = """
Test the example tag groups from
<https://orgmode.org/manual/Tag-Hierarchy.html>.
"""
tags = ["Persp", "tag-groups", "tag-hierarchy", "Vision"]
categories = ["GTD"]
draft = false
+++

`ox-hugo` Issue #[341](https://github.com/kaushalmodi/ox-hugo/issues/341)

<https://orgmode.org/manual/Tag-Hierarchy.html>
17 changes: 17 additions & 0 deletions test/site/content/posts/tag-hierarchy-multiple-tags.md
@@ -0,0 +1,17 @@
+++
title = "Tag Hierarchy: Multiple Tags from Groups"
description = """
Test multiple tags from tag groups -- including adding tags which are
already group tags of some other tags.
"""
tags = ["Control", "tag-groups", "tag-hierarchy", "Vision", "AOF", "Persp", "Context"]
categories = ["GTD"]
draft = false
+++

`ox-hugo` Issue #[341](https://github.com/kaushalmodi/ox-hugo/issues/341)

<https://orgmode.org/manual/Tag-Hierarchy.html>

Here, `Persp` tag is added manually, but it is also the group head for
`Vision` and `AOF` tags.
@@ -0,0 +1,11 @@
+++
title = "Tag Hierarchy: Tags with hyphens and spaces"
description = "Test tags containing hyphens and spaces"
tags = ["apple", "tag-groups", "tag-hierarchy", "pink-lady"]
categories = ["food", "fruit", "has seeds"]
draft = false
+++

`ox-hugo` Issue #[341](https://github.com/kaushalmodi/ox-hugo/issues/341)

<https://orgmode.org/manual/Tag-Hierarchy.html>
@@ -0,0 +1,14 @@
+++
title = "Tag Hierarchy: Tags with Multiple Groups"
description = """
Test tags which belong in multiple groups and so they have multiple
group head tags.
"""
tags = ["tag-groups", "tag-hierarchy", "squash"]
categories = ["food", "vegetable", "has seeds"]
draft = false
+++

`ox-hugo` Issue #[341](https://github.com/kaushalmodi/ox-hugo/issues/341)

<https://orgmode.org/manual/Tag-Hierarchy.html>

0 comments on commit de5dfdf

Please sign in to comment.