Skip to content

Commit

Permalink
Allow auto-export on save to be enabled using .dir-locals.el
Browse files Browse the repository at this point in the history
Works for either flow: per-subtree or per-file

Fixes #185.
  • Loading branch information
kaushalmodi committed Aug 10, 2018
1 parent 0e9c312 commit e968873
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .dir-locals.el
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
(fill-column . 70)
(sentence-end-double-space . t)))
(org-mode . ((mode . auto-fill)))
(markdown-mode . ((mode . auto-fill))))
(markdown-mode . ((mode . auto-fill)))
("test/site/content-org"
. ((org-mode . ((org-hugo-auto-export-on-save . t))))))
46 changes: 38 additions & 8 deletions ox-hugo-sugar.el
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,27 @@

;;; Code:

(defvar-local org-hugo-auto-export-on-save nil
"First enable flag for `org-hugo-export-wim-to-md-after-save'.
This is a buffer local variable to enable auto-exporting using
`ox-hugo' on file saving. It is designed to be used in
.dir-locals.el files.
For example, put the below in your project's .dir-locals.el if
the Org files in the \"content-org\" sub-directory are meant to
be auto-exported using `ox-hugo':
\((\"content-org\"
. ((org-mode . ((org-hugo-auto-export-on-save . t)))))) ")
(put 'org-hugo-auto-export-on-save 'safe-local-variable 'booleanp)

(defvar org-hugo-allow-export-after-save t
"Enable flag for `org-hugo-export-wim-to-md-after-save'.
When nil, the above function will not export the Org file to
Hugo-compatible Markdown.
"Second enable flag for `org-hugo-export-wim-to-md-after-save'.
This variable is useful only if using Org Capture flow with
per-subtree export flow. See the ‘Auto-export on Saving’ section
in this package's documentation for an example.
This variable is usually set to nil by the user in
`org-capture-before-finalize-hook' and set to t again in
Expand All @@ -25,19 +42,32 @@ Note that the export after save will not work until
`org-hugo-export-wim-to-md-after-save' is added to the
`after-save-hook' by the user.")

;;;###autoload
(defun org-hugo-export-wim-to-md-after-save ()
"Fn for `after-save-hook' to run `org-hugo-export-wim-to-md'.
The export is also skipped if `org-hugo-allow-export-after-save'
is nil. This variable is intended to be toggled dynamically in
`org-capture-before-finalize-hook' and
The export only if both and `org-hugo-auto-export-on-save' and
`org-hugo-allow-export-after-save' are non-nil.
`org-hugo-allow-export-after-save' is intended to be toggled
dynamically in `org-capture-before-finalize-hook' and
`org-capture-after-finalize-hook' hooks. See the ‘Auto-export on
Saving’ section in this package's documentation for an example."
(save-excursion
(when org-hugo-allow-export-after-save
(when (and org-hugo-auto-export-on-save ;Should be set to t only in .dir-locals.el
;; `org-hugo-allow-export-after-save' is t by default,
;; and should be set to nil only temporarily, like
;; during Org Capture.
org-hugo-allow-export-after-save)
(org-hugo-export-wim-to-md))))

(defun org-hugo-org-mode-hook-fn ()
"Hook function to enable/disable auto-export of Org file/subtree on file save.
It is enabled if `org-hugo-auto-export-on-save' is non-nil."
(add-hook 'after-save-hook #'org-hugo-export-wim-to-md-after-save :append :local))

(add-hook 'org-mode-hook #'org-hugo-org-mode-hook-fn)


(provide 'ox-hugo-sugar)

Expand Down

0 comments on commit e968873

Please sign in to comment.