Skip to content

Commit

Permalink
Add org-hugo-default-draft-state defcustom
Browse files Browse the repository at this point in the history
- Break out draft state parsing to a separate function
  org-hugo--parse-draft-state.
- Add tests for combinations of setting (and not setting)
  EXPORT_HUGO_DRAFT property and Org TODO states for
  drafting/undrafting a subtree post.

Ref: #171
  • Loading branch information
kaushalmodi committed Jul 9, 2018
1 parent 50cddf5 commit 9fe197e
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 38 deletions.
89 changes: 72 additions & 17 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,20 @@ directory where all Hugo posts should go by default."
:type 'directory
:safe #'stringp)

(defcustom org-hugo-default-draft-state nil
"Default value of a post's draft state.
When nil, a post is considered to be a draft only when its TODO
state is set to TODO or DRAFT explicitly, or if it is drafted
explicitly using the HUGO_DRAFT keyword/property.
When non-nil, a post is considered to be a draft, unless its TODO
state is set to DONE explicitly, or if it is undrafted explicitly
using the HUGO_DRAFT keyword/property."
:group 'org-export-hugo
:type 'boolean
:safe #'booleanp)

(defcustom org-hugo-footer ""
"String to be appended at the end of each Hugo post.
Expand Down Expand Up @@ -794,8 +808,8 @@ newer."
;; description
(:description "DESCRIPTION" nil nil)
;; draft
;; "draft" value is also interpreted by TODO state
;; of a post as Org subtree.
;; "draft" value interpreted by the TODO state of a
;; post as Org subtree gets higher precedence.
(:hugo-draft "HUGO_DRAFT" nil nil)
;; expiryDate
(:hugo-expirydate "HUGO_EXPIRYDATE" nil nil)
Expand Down Expand Up @@ -1398,6 +1412,60 @@ INFO is a plist used as a communication channel."
(org-html-fix-class-name todo)
(org-hugo--replace-underscores-with-spaces todo))))

;;;; Parse draft state
(defun org-hugo--parse-draft-state (info)
"Parse the draft state of the post heading at point.
Return a \"true\" or \"false\" string.
For per-subtree export flow, the draft state parsed from the Org
TODO state has a higher precedence than the value of HUGO_DRAFT
keyword/property.
INFO is a plist used as a communication channel."
(let* ((todo-keyword (org-entry-get (point) "TODO"))
(draft (cond
((stringp todo-keyword)
(let ((valid-kwds '((todo . ("TODO"))
(draft . ("DRAFT"))
(done . ("DONE")))))
(cond
((member todo-keyword (cdr (assoc 'todo valid-kwds)))
t)
((member todo-keyword (cdr (assoc 'draft valid-kwds)))
(let ((title (org-entry-get (point) "ITEM"))) ;Post title
(message "[ox-hugo] `%s' post is marked as a DRAFT" title))
t)
((member todo-keyword (cdr (assoc 'done valid-kwds)))
nil)
(t
(let (valid-kwds-list)
(dolist (kwd-cons valid-kwds)
(let ((kwd-list (cdr kwd-cons)))
(dolist (kwd kwd-list)
(push kwd valid-kwds-list))))
(user-error "The Org TODO keyword %S is invalid; it needs to be one of %S"
todo-keyword valid-kwds-list))))))
(;; If the HUGO_DRAFT keyword/property *is* set, but
;; not to nil.
(plist-get info :hugo-draft)
(let* ((draft-1 (org-hugo--front-matter-value-booleanize (plist-get info :hugo-draft)))
(is-draft (if (string= "true" draft-1) t nil)))
(when is-draft
(let* ((entry (org-element-at-point))
(is-subtree (org-element-property :EXPORT_FILE_NAME entry))
(title (if is-subtree
(org-entry-get (point) "ITEM")
(or (car (plist-get info :title)) "<EMPTY TITLE>"))))
(message "[ox-hugo] `%s' post is marked as a DRAFT" title)))
is-draft))
(t ;Neither of Org TODO state and HUGO_DRAFT keyword/property are set
org-hugo-default-draft-state)))
(draft-bool-str (org-hugo--front-matter-value-booleanize (symbol-name draft))))
;; (message "dbg: draft-state: todo keyword=%S HUGO_DRAFT=%S draft=%S"
;; todo-keyword (plist-get info :hugo-draft) draft-bool-str)
draft-bool-str))



;;; Transcode Functions
Expand Down Expand Up @@ -2726,7 +2794,6 @@ the Hugo front-matter."
INFO is a plist used as a communication channel."
;; (message "[hugo front matter DBG] info: %S" (pp info))
(let* ((fm-format (plist-get info :hugo-front-matter-format))
(title (org-entry-get (point) "ITEM")) ;Post title
(author-list (and (plist-get info :with-author)
(let ((author-raw
(org-string-nw-p
Expand Down Expand Up @@ -2772,19 +2839,7 @@ INFO is a plist used as a communication channel."
(outputs-raw (org-string-nw-p (plist-get info :hugo-outputs)))
(outputs (when outputs-raw
(org-split-string outputs-raw " ")))
(todo-keyword (org-entry-get (point) "TODO"))
(draft (cond
((and todo-keyword
(string= "TODO" todo-keyword))
"true")
((and todo-keyword
(string= "DRAFT" todo-keyword))
(message "[ox-hugo] `%s' post is marked as a DRAFT" title)
"true")
((org-hugo--plist-get-true-p info :hugo-draft)
(org-hugo--front-matter-value-booleanize (org-hugo--plist-get-true-p info :hugo-draft)))
(t
"false")))
(draft (org-hugo--parse-draft-state info))
(headless (when (org-hugo--plist-get-true-p info :hugo-headless)
(org-hugo--front-matter-value-booleanize (org-hugo--plist-get-true-p info :hugo-headless))))
(all-t-and-c-str (org-entry-get (point) "ALLTAGS"))
Expand Down Expand Up @@ -2908,7 +2963,6 @@ INFO is a plist used as a communication channel."
(resources . ,resources)))
(data `,(append data weight-data custom-fm-data)))
;; (message "[get fm DBG] tags: %s" tags)
;; (message "dbg: todo-state: keyword=%S draft=%S" todo-keyword draft)
;; (message "dbg: hugo tags: %S" (plist-get info :hugo-tags))
;; (message "[get fm info DBG] %S" info)
;; (message "[get fm blackfriday DBG] %S" blackfriday)
Expand Down Expand Up @@ -3620,6 +3674,7 @@ buffer and returned as a string in Org format."
"No Org mode shadows found in =load-path="))
"** =ox-hugo= defcustoms"
,(format "|org-hugo-default-section-directory |%S|" org-hugo-default-section-directory)
,(format "|org-hugo-default-draft-state |%S|" org-hugo-default-draft-state)
,(format "|org-hugo-use-code-for-kbd |%S|" org-hugo-use-code-for-kbd)
,(format "|org-hugo-preserve-filling |%S|" org-hugo-preserve-filling)
,(format "|org-hugo-delete-trailing-ws |%S|" org-hugo-delete-trailing-ws)
Expand Down
79 changes: 71 additions & 8 deletions test/site/content-org/all-posts.org
Original file line number Diff line number Diff line change
Expand Up @@ -6229,7 +6229,8 @@ Round {30}: {9802}/{10000}

本文是tensorflow官方推荐教程:[[http://neuralnetworksanddeeplearning.com/][Neural Networks and Deep Learning]]的笔记整理,原文
[[https://github.com/Vonng/Math/blob/master/nndl/nn-intro.md][Github Page]]。
* TODO Pre-Draft State
* Testing Draft state :draft:
** TODO Pre-Draft State
:PROPERTIES:
:EXPORT_FILE_NAME: draft-state-todo
:EXPORT_DATE: 2017-07-12T17:05:41-04:00
Expand All @@ -6239,23 +6240,85 @@ should be set to =true=.

Idea to to mark a post or blog idea as =TODO= that you yet have to
start writing.
* DRAFT Draft state
** HUGO_DRAFT set to true
:PROPERTIES:
:EXPORT_FILE_NAME: draft-state-draft
:EXPORT_DATE: 2017-07-12T13:46:16-04:00
:EXPORT_HUGO_DRAFT: true
:END:
If a post has the =DRAFT= keyword too, the =draft= front matter variable
should be set to =true=.
The value of ~EXPORT_HUGO_DRAFT~ state property should have no effect
if an Org TODO state is set for the post subtree.
*** DRAFT HUGO_DRAFT true, DRAFT state :todo:
:PROPERTIES:
:EXPORT_FILE_NAME: hugo-draft-true-todo-state-draft
:END:
If a post has the Org TODO state set to =DRAFT=, the =draft= front
matter variable should be set to =true= regardless of the value of
~EXPORT_HUGO_DRAFT~ property.

Idea is to mark a post as =DRAFT= that you have already started
writing, or are in the process at the moment, but it is not yet ready
to be published
* DRAFT Draft state with other headlines :post_heading_followed_soon_with_subheading:
*** DONE HUGO_DRAFT true, DONE state (Override) :todo:done:override:
CLOSED: [2018-07-09 Mon 10:38]
:PROPERTIES:
:EXPORT_FILE_NAME: hugo-draft-true-todo-state-done
:END:
This post has ~EXPORT_HUGO_DRAFT~ set to ~"true"~. But the *DONE*
state of Org TODO overrides that.
*** HUGO_DRAFT true, no TODO state :property:
:PROPERTIES:
:EXPORT_FILE_NAME: hugo-draft-true-todo-state-none
:END:
This post does not have any Org TODO state. So it uses the value of
~EXPORT_HUGO_DRAFT~.
** HUGO_DRAFT set to false
:PROPERTIES:
:EXPORT_HUGO_DRAFT: false
:END:
The value of ~EXPORT_HUGO_DRAFT~ state property should have no effect
if an Org TODO state is set for the post subtree.
*** DRAFT HUGO_DRAFT false, DRAFT state (Override) :todo:override:
:PROPERTIES:
:EXPORT_FILE_NAME: hugo-draft-false-todo-state-draft
:END:
This post has ~EXPORT_HUGO_DRAFT~ set to ~"false"~. But the *DRAFT*
state of Org TODO overrides that.
*** DONE HUGO_DRAFT false, DONE state :todo:done:
:PROPERTIES:
:EXPORT_FILE_NAME: hugo-draft-false-todo-state-done
:END:
The *DONE* state of Org TODO sets ~draft~ to ~false~ for this post.
*** HUGO_DRAFT false, no TODO state :property:
:PROPERTIES:
:EXPORT_FILE_NAME: hugo-draft-false-todo-state-none
:END:
This post does not have any Org TODO state. So it uses the value of
~EXPORT_HUGO_DRAFT~.
** HUGO_DRAFT not set
*** DRAFT HUGO_DRAFT not set, DRAFT state :todo:
:PROPERTIES:
:EXPORT_FILE_NAME: hugo-draft-none-todo-state-draft
:END:
This post is marked as draft as the Org TODO state is set to ~DRAFT~.
*** DONE HUGO_DRAFT not set, DONE state :todo:done:
CLOSED: [2018-07-09 Mon 11:00]
:PROPERTIES:
:EXPORT_FILE_NAME: hugo-draft-none-todo-state-done
:END:
This post is not marked as draft as the Org TODO state is set to
~DONE~.
*** HUGO_DRAFT not set, no TODO state either :default:
:PROPERTIES:
:EXPORT_FILE_NAME: hugo-draft-none-todo-state-none
:END:
This post has neither the ~EXPORT_HUGO_DRAFT~ property set, nor the
Org TODO state. So the draft state defaults to the value of
~org-hugo-default-draft-state~.
** DRAFT Draft state with other headlines :post_heading_followed_soon_with_subheading:
:PROPERTIES:
:EXPORT_FILE_NAME: draft-state-other-headings-draft
:EXPORT_DATE: 2017-07-12T13:46:16-04:00
:END:
** The "TODO" state of this heading (which is nil) should be ignored
*** The "TODO" state of this heading (which is nil) should be ignored
If a post has the =DRAFT= state set, the =draft= front matter variable
should be set to =true=, even if the post has a sub-heading
immediately after the post heading.
Expand Down
12 changes: 0 additions & 12 deletions test/site/content/posts/draft-state-draft.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
+++
title = "Draft state with other headlines"
date = 2017-07-12T13:46:16-04:00
tags = ["post-heading-followed-soon-with-subheading"]
tags = ["draft", "post-heading-followed-soon-with-subheading"]
draft = true
+++

Expand Down
1 change: 1 addition & 0 deletions test/site/content/posts/draft-state-todo.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
+++
title = "Pre-Draft State"
date = 2017-07-12T17:05:41-04:00
tags = ["draft"]
draft = true
+++

Expand Down
7 changes: 7 additions & 0 deletions test/site/content/posts/hugo-draft-false-todo-state-done.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
+++
title = "HUGO_DRAFT false, DONE state"
tags = ["draft", "todo", "done"]
draft = false
+++

The **DONE** state of Org TODO sets `draft` to `false` for this post.
8 changes: 8 additions & 0 deletions test/site/content/posts/hugo-draft-false-todo-state-draft.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
+++
title = "HUGO_DRAFT false, DRAFT state (Override)"
tags = ["draft", "todo", "override"]
draft = true
+++

This post has `EXPORT_HUGO_DRAFT` set to `"false"`. But the **DRAFT**
state of Org TODO overrides that.
8 changes: 8 additions & 0 deletions test/site/content/posts/hugo-draft-false-todo-state-none.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
+++
title = "HUGO_DRAFT false, no TODO state"
tags = ["draft", "property"]
draft = false
+++

This post does not have any Org TODO state. So it uses the value of
`EXPORT_HUGO_DRAFT`.
9 changes: 9 additions & 0 deletions test/site/content/posts/hugo-draft-none-todo-state-done.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
+++
title = "HUGO_DRAFT not set, DONE state"
date = 2018-07-09T11:00:00+00:00
tags = ["draft", "todo", "done"]
draft = false
+++

This post is not marked as draft as the Org TODO state is set to
`DONE`.
7 changes: 7 additions & 0 deletions test/site/content/posts/hugo-draft-none-todo-state-draft.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
+++
title = "HUGO_DRAFT not set, DRAFT state"
tags = ["draft", "todo"]
draft = true
+++

This post is marked as draft as the Org TODO state is set to `DRAFT`.
9 changes: 9 additions & 0 deletions test/site/content/posts/hugo-draft-none-todo-state-none.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
+++
title = "HUGO_DRAFT not set, no TODO state either"
tags = ["draft", "default"]
draft = false
+++

This post has neither the `EXPORT_HUGO_DRAFT` property set, nor the
Org TODO state. So the draft state defaults to the value of
`org-hugo-default-draft-state`.
9 changes: 9 additions & 0 deletions test/site/content/posts/hugo-draft-true-todo-state-done.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
+++
title = "HUGO_DRAFT true, DONE state (Override)"
date = 2018-07-09T10:38:00+00:00
tags = ["draft", "todo", "done", "override"]
draft = false
+++

This post has `EXPORT_HUGO_DRAFT` set to `"true"`. But the **DONE**
state of Org TODO overrides that.
13 changes: 13 additions & 0 deletions test/site/content/posts/hugo-draft-true-todo-state-draft.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
+++
title = "HUGO_DRAFT true, DRAFT state"
tags = ["draft", "todo"]
draft = true
+++

If a post has the Org TODO state set to `DRAFT`, the `draft` front
matter variable should be set to `true` regardless of the value of
`EXPORT_HUGO_DRAFT` property.

Idea is to mark a post as `DRAFT` that you have already started
writing, or are in the process at the moment, but it is not yet ready
to be published
8 changes: 8 additions & 0 deletions test/site/content/posts/hugo-draft-true-todo-state-none.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
+++
title = "HUGO_DRAFT true, no TODO state"
tags = ["draft", "property"]
draft = true
+++

This post does not have any Org TODO state. So it uses the value of
`EXPORT_HUGO_DRAFT`.

0 comments on commit 9fe197e

Please sign in to comment.