Skip to content

Commit

Permalink
Refactoring for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Jul 27, 2019
1 parent 1d881ea commit 325c655
Showing 1 changed file with 57 additions and 44 deletions.
101 changes: 57 additions & 44 deletions ox-hugo.el
Expand Up @@ -3793,31 +3793,34 @@ exporting all valid Hugo post subtrees from the current Org file.
(defun org-hugo--get-element-path (element info)
"Return the section path of ELEMENT.
INFO is a plist holding export options."
(let ((root (or (org-export-get-node-property :EXPORT_HUGO_SECTION element t)
(let ((root (or (org-export-get-node-property :EXPORT_HUGO_SECTION element :inherited)
(plist-get info :hugo-section)))
(filename (org-export-get-node-property :EXPORT_FILE_NAME element t))
current-element
fragment
fragments)
(setq current-element element)
;; Iterate over all parents of current-element, and collect section path fragments
(while (and current-element (not (org-export-get-node-property :EXPORT_HUGO_SECTION current-element nil)))
;; Add the :EXPORT_HUGO_SECTION* value to the fragment list
(filename (org-export-get-node-property :EXPORT_FILE_NAME element :inherited))
(current-element element)
fragment fragments)
;; Iterate over all parents of current-element, and collect
;; section path fragments.
(while (and current-element
(not (org-export-get-node-property :EXPORT_HUGO_SECTION current-element nil)))
;; Add the :EXPORT_HUGO_SECTION* value to the fragment list.
(when (setq fragment (org-export-get-node-property :EXPORT_HUGO_SECTION* current-element nil))
(push fragment fragments))
(setq current-element (org-element-property :parent current-element)))
;; Return the root section, section fragments and filename concatenated
;; Return the root section, section fragments and filename
;; concatenated.
(concat
(file-name-as-directory root)
(mapconcat #'file-name-as-directory fragments "")
filename)))

(defun org-hugo--preprocess-buffer ()
"Return a preprocessed copy of the current buffer.
(defun org-hugo--get-pre-processed-buffer ()
"Return a pre-processed copy of the current buffer.
Internal links to other subtrees are converted to external
links."
(let* ((buffer (generate-new-buffer (buffer-name)))
;; Create an abstract syntax tree (AST) of the org document in the current buffer
;; Create an abstract syntax tree (AST) of the org document
;; in the current buffer.
(ast (org-element-parse-buffer))
(org-use-property-inheritance (org-hugo--selective-property-inheritance))
(info (org-combine-plists
Expand All @@ -3832,6 +3835,7 @@ links."
(let ((inhibit-modification-hooks t)
(org-mode-hook nil)
(org-inhibit-startup t))

(org-mode)
;; Copy specific buffer local variables and variables set
;; through BIND keywords.
Expand All @@ -3851,7 +3855,8 @@ links."
;; sent to external process.
(or (not val) (ignore-errors (read (format "%S" val))))
(push (set (make-local-variable var) val) vars)))))
;; Process all link elements in the AST

;; Process all link elements in the AST.
(org-element-map ast 'link
(lambda (link)
(let ((type (org-element-property :type link)))
Expand All @@ -3862,41 +3867,49 @@ links."
(org-export-resolve-id-link link info)))
(source-path (org-hugo--get-element-path link info))
(destination-path (org-hugo--get-element-path destination info)))
;; Change the link if it points to a valid destination outside the subtree
;; Change the link if it points to a valid
;; destination outside the subtree.
(unless (equal source-path destination-path)
(let ((link-copy (org-element-copy link)))
(apply #'org-element-adopt-elements link-copy (org-element-contents link))
(org-element-put-property link-copy :type "file")
(org-element-put-property link-copy :path
(cond
;; If the destination is a heading with the :EXPORT_FILE_NAME
;; property defined, the link should point to the file (without
;; anchor)
((org-element-property :EXPORT_FILE_NAME destination)
(concat destination-path ".org"))
;; Hugo only supports anchors to headlines, so if a "fuzzy" type
;; link points to anything else than a headline, it should point
;; to the file.
((and (string= type "fuzzy") (not (string-prefix-p "*" raw-link)))
(concat destination-path ".org"))
;; In "custom-id" type links, the raw-link matches the anchor of
;; the destination.
((string= type "custom-id")
(concat destination-path ".org::" raw-link))
;; In "id" and "fuzzy" type links, the anchor of the destination
;; is derived from the :CUSTOM_ID property or the title
(t
(let* ((custom-id (org-element-property :CUSTOM_ID destination))
(title (org-element-property :raw-value destination))
(anchor (or custom-id (org-hugo-slug title))))
(concat destination-path ".org::#" anchor)))))
(org-element-put-property
link-copy :path
(cond
;; If the destination is a heading with the
;; :EXPORT_FILE_NAME property defined, the
;; link should point to the file (without
;; anchor).
((org-element-property :EXPORT_FILE_NAME destination)
(concat destination-path ".org"))
;; Hugo only supports anchors to headlines, so
;; if a "fuzzy" type link points to anything
;; else than a headline, it should point to
;; the file.
((and (string= type "fuzzy")
(not (string-prefix-p "*" raw-link)))
(concat destination-path ".org"))
;; In "custom-id" type links, the raw-link
;; matches the anchor of the destination.
((string= type "custom-id")
(concat destination-path ".org::" raw-link))
;; In "id" and "fuzzy" type links, the anchor
;; of the destination is derived from the
;; :CUSTOM_ID property or the title.
(t
(let* ((custom-id (org-element-property :CUSTOM_ID destination))
(title (org-element-property :raw-value destination))
(anchor (or custom-id (org-hugo-slug title))))
(concat destination-path ".org::#" anchor)))))
(org-element-set-element link link-copy))))))))
;; Workaround to prevent exporting of empty special blocks

;; Workaround to prevent exporting of empty special blocks.
(org-element-map ast 'special-block
(lambda (block)
(when (null (org-element-contents block))
(org-element-adopt-elements block ""))))
;; Turn the AST with updated links into an org document

;; Turn the AST with updated links into an Org document.
(insert (org-element-interpret-data ast))
(set-buffer-modified-p nil)))
buffer))
Expand Down Expand Up @@ -4034,7 +4047,7 @@ The optional argument NOERROR is passed to
(if all-subtrees
(progn
(setq org-hugo--subtree-count 0) ;Reset the subtree count
(let ((buffer (org-hugo--preprocess-buffer)))
(let ((buffer (org-hugo--get-pre-processed-buffer)))
(with-current-buffer buffer
(setq ret (org-map-entries
(lambda ()
Expand All @@ -4057,10 +4070,10 @@ The optional argument NOERROR is passed to
(setq ret (org-hugo--export-file-to-md f-or-b-name async visible-only noerror))))

;; Publish only the current subtree.
(let ((buffer (org-hugo--preprocess-buffer))
(outline-path (org-get-outline-path t)))
(let ((current-outline-path (org-get-outline-path :with-self))
(buffer (org-hugo--get-pre-processed-buffer)))
(with-current-buffer buffer
(goto-char (org-find-olp outline-path t))
(goto-char (org-find-olp current-outline-path :this-buffer))
(setq ret (org-hugo--export-subtree-to-md async visible-only)))
(kill-buffer buffer))
(unless ret
Expand Down

0 comments on commit 325c655

Please sign in to comment.