Skip to content

Commit

Permalink
Support menu in YAML front matter, still broken
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Jul 17, 2017
1 parent 40c076a commit 935cb8a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#+HUGO_BASE_DIR: ../
#+HUGO_FRONT_MATTER_FORMAT: yaml

* Menu Meta Data in YAML Front Matter
:PROPERTIES:
:EXPORT_FILE_NAME: menu-meta-data-yaml
:EXPORT_DATE: 2017/07/17
:END:
Testing the addition of /menu/ meta data to the front matter.

This comment has been minimized.

Copy link
@titaniumbones

titaniumbones Jul 18, 2017

Contributor

should probably add some menu-specific metadata.

75 changes: 26 additions & 49 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ directory where all Hugo posts should go by default."
(:hugo-base-dir "HUGO_BASE_DIR" nil nil)
(:hugo-static-images "HUGO_STATIC_IMAGES" nil "images")
(:hugo-code-fence "HUGO_CODE_FENCE" nil "t")
(:hugo-static-images "HUGO_STATIC_IMAGES" "images" nil)
(:hugo-menu-name "HUGO_MENU_NAME" "main" nil)
(:hugo-menu-weight "HUGO_MENU_WEIGHT" 20 nil)
(:hugo-menu-name "HUGO_MENU_NAME" nil "main")
(:hugo-menu-weight "HUGO_MENU_WEIGHT" nil 20)
(:hugo-menu-parent "HUGO_MENU_PARENT" nil nil)))


Expand Down Expand Up @@ -425,7 +424,7 @@ INFO is a plist holding export options."
INFO is a plist used as a communication channel."
;; (message "[hugo front matter DBG] info: %S" (pp info))
(let* ((fm-format (org-export-data (plist-get info :hugo-front-matter-format) info))
(let* ((fm-format (plist-get info :hugo-front-matter-format))
(hugo-date-fmt "%Y-%m-%dT%T%z")
(date-nocolon (or ;; Get the date from the subtree property `EXPORT_DATE' if available
(org-string-nw-p (org-export-data (plist-get info :date) info))
Expand All @@ -445,7 +444,14 @@ INFO is a plist used as a communication channel."
(org-export-data (plist-get info :tags) info))))
(draft (or org-hugo--draft-state
(org-export-data (plist-get info :hugo-draft) info)))
(data `((title . ,(org-export-data (plist-get info :title) info))
(title (org-export-data (plist-get info :title) info))
(menu-name (org-string-nw-p (plist-get info :hugo-menu-name)))
(menu-identifier (org-hugo--slug title))
(menu-parent (plist-get info :hugo-menu-parent))
(menu-weight (plist-get info :hugo-menu-weight))
(menu (when menu-name
(cons menu-name `(,menu-identifier ,menu-parent ,menu-weight))))

This comment has been minimized.

Copy link
@titaniumbones

titaniumbones Jul 18, 2017

Contributor

Are you sure you dont want to make the cdr of menu and alist whose structure is analogous to the rest of data itself, like:

(menu (when-menu-name
  (cons menu-name `((identifier . ,menu-identifier) (parent . ,menu-parent) (weight . ,menu-weight))))

Then you can dolist your way through the alist as in the rest of the defun.

I was working on something like this but I'm sure any implementation of yours would be cleaner.

(data `((title . ,title)
(date . ,date)
(description . ,(org-export-data (plist-get info :hugo-description) info))
(tags . ,tags)
Expand All @@ -460,23 +466,19 @@ INFO is a plist used as a communication channel."
(markup . ,(org-export-data (plist-get info :hugo-markup) info))
(slug . ,(org-export-data (plist-get info :hugo-slug) info))
(url . ,(org-export-data (plist-get info :hugo-url) info))
(menu . ,(org-export-data (plist-get info :hugo-menu-name) info))
)))
(message "collect-menu output is: %s" (org-hugo--collect-menu-metadata info fm-format))
;;(message "output of collect-menu is: %s" (car (org-hugo--collect-menu-metadata info)))
;; (message "car of collect-menu: %s" (car (org-hugo--collect-menu-metadata info)))
;; (message "cdr of collect-menu: %s" (cdr (org-hugo--collect-menu-metadata info)))
(org-hugo--gen-front-matter data fm-format info)))

(defun org-hugo--gen-front-matter (data format info)
(menu . ,menu))))
;; (message "[get fm menu DBG] %S" menu)
(org-hugo--gen-front-matter data fm-format)))

(defun org-hugo--gen-front-matter (data format)
"Generate the Hugo post front matter, and return that string.
DATA is an alist of the form \((KEY1 . VAL1) (KEY2 . VAL2) .. \),
where KEY is a symbol and VAL is a string.
Generate the front matter in the specified FORMAT. Valid values
are \"toml\" and \"yaml\"."

(let ((sep (cond ((string= format "toml") "+++\n")
((string= format "yaml") "---\n")
(t "")))
Expand All @@ -488,9 +490,10 @@ are \"toml\" and \"yaml\"."
(dolist (pair data)
(let ((key (symbol-name (car pair)))
(value (cdr pair)))
;; (message "[hugo fm key value DBG] %S %S" key value)
(unless (or (null value)
(string= "" value)
(string= "menu" key))
(and (stringp value)
(string= "" value)))
;; In TOML/YAML, the value portion needs to be wrapped in double quotes
;; TOML example:
;; title = "My Post"
Expand All @@ -516,41 +519,15 @@ are \"toml\" and \"yaml\"."
(string= key "expirydate")
(string= key "draft"))
value)
((string= key "menu")
(message "dbg: %S" value)
(message "Need to translate value to menu fm in yaml/toml here")
"")
(t
(org-hugo--wrap-string-in-quotes value)))))))))
(concat sep front-matter (if (alist-get "menu" data) (org-hugo--collect-menu-metadata info format) (org-hugo--collect-menu-metadata info format) ) sep)))

(defun org-hugo--collect-menu-metadata (info format)
"collect all the menu-related metadata and return a nested plist of the values
to be used by toml and yaml frontmatter creators."
;;(message "info: %s" info)
(let* ((menu-atts '(:hugo-menu-parent "parent" :hugo-menu-weight "weight"))
(menu-name (plist-get info :hugo-menu-name))
(menu-identifier (plist-get info :title))
(menu-weight (plist-get info :hugo-menu-weight))
(menu-parent (plist-get info :hugo-menu-parent))
;; (atts-plist (cl-loop for att in (plist-get-keys menu-atts)
;; collect (plist-get menu-atts att)
;; collect (plist-get info att)))
;;(menu-plist (plist-put '() menu-name `("parent" ,menu-parent "weight" ,menu-weight)))
)


(if (string= format "yaml")
(let ((indent " "))
(format "\nmenu:\n %s:\n %s: %s\n %s: %s\n %s: %s\n"
menu-name
"identifier" (plist-get info :title)
"parent" (plist-get info :hugo-menu-parent )
"weight" (plist-get info :hugo-menu-weight)))
(format "[[menu.%s]]\n %s = %s\n %s = %s\n% s = %s\n"
menu-name
"parent" (plist-get info :hugo-menu-parent )
"identifier" (plist-get info :title)
"weight" (plist-get info :hugo-menu-weight))
)))
(concat sep front-matter sep)))



;;; Interactive functions

;;;###autoload
Expand Down

0 comments on commit 935cb8a

Please sign in to comment.