Skip to content

Commit

Permalink
feat: Use the latest LOGBOOK note timestamp to update lastmod
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed May 12, 2022
1 parent 062e88e commit 5ba0248
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
11 changes: 5 additions & 6 deletions doc/ox-hugo-manual.org
Expand Up @@ -876,8 +876,9 @@ Also see the [[*Custom Front-matter Parameters][Custom Front-matter Parameters]]
4. =#+date:= keyword
- Precedence for =lastmod= parsing ::
1. Last (second or later) transition to a /DONE/ state recorded in
=:LOGBOOK:= (see {{{titleref(Drawers#logbook-dates,Dates parsed
from ~:LOGBOOK:~ drawers)}}})
=:LOGBOOK:=, or timestamp of the last added note (see
{{{titleref(Drawers#logbook-dates,Dates parsed from ~:LOGBOOK:~
drawers)}}})
2. =lastmod= set automatically if =:EXPORT_HUGO_AUTO_SET_LASTMOD: t=
*and* if it's not derived from the =:LOGBOOK:= drawer.
3. =EXPORT_HUGO_LASTMOD= subtree property or =#+hugo_lastmod:= keyword
Expand Down Expand Up @@ -4410,10 +4411,8 @@ state changed or under the heading where the ~org-add-note~ command
~org-done-keywords~ values[fn:15] i.e. transition to /DONE/ state.
- ~lastmod~ :: This front-matter variable is updated with the
timestamp associated with the *last* TODO state transition to the
/DONE/ state.

Note that if only one transition to the /DONE/ state was recorded,
then this front-matter variable remains unset.
/DONE/ state, or with the *last* added note (whichever is the
latest).

#+begin_note
The ~date~ and ~lastmod~ values parsed from the ~:LOGBOOK:~ drawer
Expand Down
28 changes: 21 additions & 7 deletions ox-hugo.el
Expand Up @@ -1847,13 +1847,17 @@ holding contextual information."
(push `(to_state . ,to-state) logbook-entry)
;; (message "[ox-hugo logbook DBG] org-done-keywords: %S" org-done-keywords)
(when (member to-state org-done-keywords)
;; The first TODO state change entry will be the latest one, and
;; `:logbook-date' would already have been set to that.
;; So if `:logbook-lastmod' is not set, set that that to the
;; value of `:logbook-date'.
;; The first parsed TODO state change entry will be the
;; latest one, and `:logbook-date' would already have
;; been set to that. So if `:logbook-lastmod' is not set,
;; set that that to the value of `:logbook-date'.
;; *This always works because the newest state change or note
;; entry is always put to the top of the LOGBOOK.*
(unless (plist-get info :logbook-lastmod)
(when (plist-get info :logbook-date)
(plist-put info :logbook-lastmod (plist-get info :logbook-date))))
;; `:logbook-date' will keep on getting updating until the last
;; parsed (first entered) "state changed to DONE" entry.
(plist-put info :logbook-date timestamp)))
(when from-state ;For debug purpose
(push `(from_state . ,from-state) logbook-entry))))
Expand All @@ -1863,7 +1867,13 @@ holding contextual information."
(let ((note (string-trim
(match-string-no-properties 1 para-raw-str))))
;; (message "[ox-hugo logbook DBG] note : %s @ %s" note timestamp)
(push `(note . ,note) logbook-entry)))
(push `(note . ,note) logbook-entry)
;; Update the `lastmod' field using the
;; note's timestamp.
;; *This always works because the newest state change or note
;; entry is always put to the top of the LOGBOOK.*
(unless (plist-get info :logbook-lastmod)
(plist-put info :logbook-lastmod timestamp))))

(t
(user-error "LOGBOOK drawer entry is neither a state change, nor a note."))))
Expand All @@ -1883,11 +1893,15 @@ holding contextual information."
nil)) ;lambda return for (org-element-map lst 'item
nil) ;lambda return for (org-element-map drawer 'plain-list ..
nil :first-match) ;The 'logbook element will have only one 'plain-list element
;; TODO: Code to save the notes content and date/lastmod
;; timestamps to appropriate front-matter.
;; (message "[ox-hugo logbook DBG] logbook-notes : %S" logbook-notes)
;; (message "[ox-hugo logbook DBG] logbook derived `date' : %S" (plist-get info :logbook-date))
;; (message "[ox-hugo logbook DBG] logbook derived `lastmod' : %S" (plist-get info :logbook-lastmod))

;; Don't set `:logbook-lastmod' if `:logbook-date' is not
;; parsed from the LOGBOOK drawer (which means that the post
;; is not marked as DONE).
(unless (plist-get info :logbook-date)
(plist-put info :logbook-lastmod nil))
(plist-put info :logbook logbook-notes)
"")) ;Nothing from the LOGBOOK gets exported to the Markdown body
(t
Expand Down
8 changes: 8 additions & 0 deletions test/site/content-org/all-posts.org
Expand Up @@ -7313,6 +7313,12 @@ This test removes the ~foo~ front-matter key from the exported file
:EXPORT_FILE_NAME: parsing-notes-from-logbook
:END:
:LOGBOOK:
- Note taken on [2022-05-12 Thu 08:13] \\
Another update to the post. This update timestamp should update the
~lastmod~.
- State "DONE" from "TEST__TODO" <2022-05-10 Tue 08:13>
- State "TODO" from "DRAFT" <2022-05-08 Sun 06:13>
- State "DRAFT" from "DONE" <2022-05-06 Fri 04:12>
- Note taken on [2022-05-04 Wed 13:15] \\
This new note added last should be the first element of the
~[[logbook.notes]]~ TOML table array.
Expand Down Expand Up @@ -7366,6 +7372,8 @@ note first in the TOML table array to the oldest note at the last.
#+begin_description
Parse notes from LOGBOOK drawers in top-level and nested headings.
#+end_description
The ~lastmod~ field is not set for this post (even if it has a note
with timestamp) because this post is not marked as DONE.
**** Sub-heading
:LOGBOOK:
- Note taken on [2022-05-11 Wed 12:18] \\
Expand Down
4 changes: 4 additions & 0 deletions test/site/content/posts/logbook-notes-in-nested-headings.md
Expand Up @@ -19,6 +19,10 @@ draft = false
note = "Note in the top-heading LOGBOOK drawer"
+++

The `lastmod` field is not set for this post (even if it has a note
with timestamp) because this post is not marked as DONE.


## Sub-heading {#sub-heading}


Expand Down
7 changes: 7 additions & 0 deletions test/site/content/posts/parsing-notes-from-logbook.md
Expand Up @@ -6,10 +6,17 @@ description = """
"""
date = 2018-09-06T11:25:00+00:00
layout = "alternate-single"
lastmod = 2022-05-12T08:13:00+00:00
tags = ["front-matter", "notes", "logbook"]
draft = false
[logbook]
[logbook._toplevel]
[[logbook._toplevel.notes]]
timestamp = 2022-05-12T08:13:00+00:00
note = """
Another update to the post. This update timestamp should update the
`lastmod`.
"""
[[logbook._toplevel.notes]]
timestamp = 2022-05-04T13:15:00+00:00
note = """
Expand Down

0 comments on commit 5ba0248

Please sign in to comment.