Skip to content

Commit

Permalink
org-hugo--heading-get-slug: Now returns nil if not post subtree
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Mar 16, 2022
1 parent 7e21ec0 commit 75345ee
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 132 deletions.
126 changes: 55 additions & 71 deletions ox-hugo.el
Expand Up @@ -1869,7 +1869,7 @@ Return nil if id is not found."
(goto-char element-begin)
(org-id-get))))

(defun org-hugo-get-heading-slug(element info)
(defun org-hugo-get-heading-slug (element info)
"Return the slug string derived from an Org heading ELEMENT.
The slug string is parsed from the ELEMENT's `:title' property.
INFO is a plist used as a communication channel.
Expand All @@ -1890,7 +1890,7 @@ This function will never return nil."
"")))
(substring (md5 title) 0 hash-len)))

(defun org-hugo--heading-get-slug(heading info)
(defun org-hugo--heading-get-slug (heading _info)
"Return the slug string derived from an Org HEADING element.
1. If HEADING has `:EXPORT_FILE_NAME' and `:EXPORT_HUGO_SLUG'
Expand All @@ -1911,81 +1911,65 @@ This function will never return nil."
The `:EXPORT_HUGO_SECTION' property is prepended to all of the
above options.
5. If none of the above are true, the slug string is parsed from
the HEADING's `:title' property.
6. Return nil if the `:title' property is nil or an empty string.
INFO is a plist used as a communication channel."
(let ((current-elem-post-subtree-p (org-string-nw-p (org-export-get-node-property :EXPORT_FILE_NAME heading)))
(file (org-string-nw-p (org-export-get-node-property :EXPORT_FILE_NAME heading :inherited)))
Return nil if none of the above are true."
(let ((file (org-string-nw-p (org-export-get-node-property :EXPORT_FILE_NAME heading)))
hugo-slug bundle slug)
;; (message "[org-hugo--heading-get-slug DBG] EXPORT_FILE_NAME: %S" file)
(when file
(setq hugo-slug (org-string-nw-p (org-export-get-node-property :EXPORT_HUGO_SLUG heading :inherited))))
(unless hugo-slug
(setq bundle (org-string-nw-p (org-export-get-node-property :EXPORT_HUGO_BUNDLE heading :inherited)))
;; (message "[org-hugo--heading-get-slug DBG] EXPORT_HUGO_BUNDLE: %S" bundle)
)
(setq hugo-slug (org-string-nw-p (org-export-get-node-property :EXPORT_HUGO_SLUG heading :inherited)))
(unless hugo-slug
(setq bundle (org-string-nw-p (org-export-get-node-property :EXPORT_HUGO_BUNDLE heading :inherited)))
;; (message "[org-hugo--heading-get-slug DBG] EXPORT_HUGO_BUNDLE: %S" bundle)
)

(cond
(hugo-slug
;; (message "[org-hugo--heading-get-slug DBG] hugo-slug: %S" hugo-slug)
(setq slug hugo-slug))
;; Leaf or branch bundle landing page.
((and bundle file (member file '("index" ;Leaf bundle
"_index" ;Branch bundle
)))
(setq slug bundle)
;; (message "[org-hugo--heading-get-slug DBG] bundle slug: %S" slug)
)
;; It's a Hugo page bundle, but the file is neither index nor
;; _index. So likely a page in a branch bundle.
((and bundle file)
(setq slug (concat (file-name-as-directory bundle) file))
;; (message "[org-hugo--heading-get-slug DBG] branch bundle file slug: %S" slug)
)
;; Only EXPORT_FILE_NAME is set.
(file
(setq slug file))
(t
;; Do nothing
))

(when slug
(let ((pheading heading)
section fragment fragments)
(setq section (org-string-nw-p (org-export-get-node-property :EXPORT_HUGO_SECTION heading :inherited)))

;; Iterate over all parents of heading, and collect section
;; path fragments.
(while (and pheading
(not (org-export-get-node-property :EXPORT_HUGO_SECTION pheading nil)))
;; Add the :EXPORT_HUGO_SECTION* value to the fragment list.
(when (setq fragment (org-export-get-node-property :EXPORT_HUGO_SECTION* pheading nil))
(push fragment fragments))
(setq pheading (org-element-property :parent pheading)))

(when section
(setq slug (concat (file-name-as-directory section)
(mapconcat #'file-name-as-directory fragments "")
slug)))
;; (message "[org-hugo--heading-get-slug DBG] section: %S" section)
;; (message "[org-hugo--heading-get-slug DBG] section + slug: %S" slug)
(cond
(hugo-slug
;; (message "[org-hugo--heading-get-slug DBG] hugo-slug: %S" hugo-slug)
(setq slug hugo-slug))
;; Leaf or branch bundle landing page.
((and bundle file (member file '("index" ;Leaf bundle
"_index" ;Branch bundle
)))
(setq slug bundle)
;; (message "[org-hugo--heading-get-slug DBG] bundle slug: %S" slug)
)
;; It's a Hugo page bundle, but the file is neither index nor
;; _index. So likely a page in a branch bundle.
((and bundle file)
(setq slug (concat (file-name-as-directory bundle) file))
;; (message "[org-hugo--heading-get-slug DBG] branch bundle file slug: %S" slug)
)
;; Only EXPORT_FILE_NAME is set.
(file
(setq slug file))
(t
;; Do nothing
))

;; Finally use the `:title' property to add anchor if applicable.
(unless current-elem-post-subtree-p
(let ((anchor (org-string-nw-p
(org-hugo-slug
(org-export-data-with-backend
(org-element-property :title heading) 'md info)
:allow-double-hyphens))))
;; (message "[org-hugo--heading-get-slug DBG] anchor: %S" anchor)
(when (org-string-nw-p anchor)
(setq slug (format "%s#%s" (or slug "") anchor)))))
;; (message "[org-hugo--heading-get-slug DBG] FINAL slug: %S" slug)
slug))
;; Prefix with section and fragmented sections if any.
(when slug
(let ((pheading heading)
section fragment fragments)
(setq section (org-string-nw-p (org-export-get-node-property :EXPORT_HUGO_SECTION heading :inherited)))

;; Iterate over all parents of heading, and collect section
;; path fragments.
(while (and pheading
(not (org-export-get-node-property :EXPORT_HUGO_SECTION pheading nil)))
;; Add the :EXPORT_HUGO_SECTION* value to the fragment list.
(when (setq fragment (org-export-get-node-property :EXPORT_HUGO_SECTION* pheading nil))
(push fragment fragments))
(setq pheading (org-element-property :parent pheading)))

(when section
(setq slug (concat (file-name-as-directory section)
(mapconcat #'file-name-as-directory fragments "")
slug)))
;; (message "[org-hugo--heading-get-slug DBG] section: %S" section)
;; (message "[org-hugo--heading-get-slug DBG] section + slug: %S" slug)
))
;; (message "[org-hugo--heading-get-slug DBG] FINAL slug: %S" slug)
slug)))

(defun org-hugo--get-anchor(element info)
"Return anchor string for Org heading ELEMENT.
Expand Down
75 changes: 14 additions & 61 deletions test/ert/tslug.el
Expand Up @@ -23,8 +23,8 @@
(require 'org-test-lib)
(require 'ox-hugo)

(ert-deftest test-slug/title ()
"Test derivation of the slug from heading title."
(ert-deftest test-slug/return-nil ()
"Test nil return conditions."

;; Empty title
(should
Expand All @@ -34,25 +34,16 @@
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info)))))

;; Heading with EXPORT_FILE_NAME.
(should
(string= "#some-heading"
(org-test-with-parsed-data
"* Some Heading<point>"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info)))))

;; Only EXPORT_HUGO_SLUG, and no EXPORT_FILE_NAME. So heading is
;; used for deriving slug.
(should
(string= "#some-heading"
(org-test-with-parsed-data
"* Some Heading<point>
:PROPERTIES:
:EXPORT_HUGO_SLUG: slug
:END:"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info)))))
(equal nil
(org-test-with-parsed-data
"* Some Heading<point>"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info))))))

(ert-deftest test-slug/title ()
"Test slug when EXPORT_HUGO_SLUG is set."
;; EXPORT_FILE_NAME + EXPORT_HUGO_SLUG
(should
(string= "slug"
Expand Down Expand Up @@ -221,43 +212,6 @@
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info))))))

;; Slugs + anchors
(ert-deftest test-slug/slugs-and-anchors ()

;; Anchor on a regular page in a section
(should
(string= "section/file#some-heading"
(org-test-with-parsed-data
"* Section
:PROPERTIES:
:EXPORT_HUGO_SECTION: section
:END:
** Some page
:PROPERTIES:
:EXPORT_FILE_NAME: file
:END:
*** Some Heading<point>"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info)))))

;; Anchor on a leaf bundle in a section
(should
(string= "section/leaf#some-heading"
(org-test-with-parsed-data
"* Section
:PROPERTIES:
:EXPORT_HUGO_SECTION: section
:END:
** Leaf Bundle
:PROPERTIES:
:EXPORT_HUGO_BUNDLE: leaf
:EXPORT_FILE_NAME: index
:END:
*** Some Heading<point>"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info))))))


;; Section fragments
(ert-deftest test-slug/section-fragments ()

Expand All @@ -283,9 +237,9 @@
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info)))))

;; Anchor on a branch page in a branch bundle in section fragments.
;; Branch page in a branch bundle in section fragments.
(should
(string= "section/sec2/sec3/branch/branch-page#some-heading"
(string= "section/sec2/sec3/branch/branch-page"
(org-test-with-parsed-data
"* Section
:PROPERTIES:
Expand All @@ -304,11 +258,10 @@
:PROPERTIES:
:EXPORT_HUGO_BUNDLE: branch
:END:
***** Some page
***** Some page<point>
:PROPERTIES:
:EXPORT_FILE_NAME: branch-page
:END:
****** Some Heading<point>"
:END:"
(let ((el (org-element-at-point)))
(org-hugo--heading-get-slug el info))))))

Expand Down

0 comments on commit 75345ee

Please sign in to comment.