Skip to content

Commit

Permalink
Merge pull request #596 from kaushalmodi/fix-broken-links-export-opti…
Browse files Browse the repository at this point in the history
…ons-parsing

Fix broken links export options parsing.

Fixes #587.
  • Loading branch information
kaushalmodi authored Mar 18, 2022
2 parents 3484e4a + c8c9cb8 commit e1a3e07
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
35 changes: 20 additions & 15 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -1910,12 +1910,15 @@ This function will never return nil."
"")))
(substring (md5 title) 0 hash-len)))

(defun org-hugo--get-elem-with-prop (prop &optional _info)
(defun org-hugo--get-elem-with-prop (prop &optional pom _info)
"Find the first element with PROP property in the current tree.
PROP is a property symbol with a : prefix, example:
`:EXPORT_FILE_NAME'.
Optional argument POM is the position or marker from which the
upward search for PROP should begin.
Return a cons of type (ELEM . PVAL) where ELEM is the element
containing the property PROP and PVAL is the property's value.
Expand All @@ -1926,13 +1929,17 @@ versions for the issue that `org-element-at-point' does not
return an element with all the inherited properties. That issue
is fixed in Org main branch at least as of 2022-03-17."
(org-with-wide-buffer
;; (message (format "[search prop DBG] point 1 : %S" (point)))
(when pom
(goto-char pom))
;; (message (format "[search prop DBG] point 2 : %S" (point)))
(org-back-to-heading-or-point-min :invisible-ok)
(let ((elem (org-element-at-point))
(level t)
pval)
(catch :found
(while elem
;; (message (format "[search prop DBG] elem : %S" elem ))
;; (message (format "[search prop DBG] prop %S, elem : %S" prop elem))
(setq pval (org-element-property prop elem))
;; (message "[search prop DBG] level %S, pval %S" level pval)
(when (or pval (null level))
Expand Down Expand Up @@ -1973,26 +1980,16 @@ INFO is a plist used as a communication channel.
Return nil if none of the above are true."
(org-with-wide-buffer
(let ((heading-begin (org-element-property :begin heading)))
(when (numberp heading-begin)
(when heading-begin
(goto-char heading-begin)))
(let ((file (org-string-nw-p (org-export-get-node-property :EXPORT_FILE_NAME heading inherit-export-file-name)))
bundle slug)
;; (message "[org-hugo--heading-get-slug DBG] EXPORT_FILE_NAME: %S" file)
(when file
;; (message "buffer : %S" (current-buffer))
;; (message "point min : %S" (point-min))
;; (message "point max : %S" (point-max))
(setq bundle (let* ((elem-pval (org-hugo--get-elem-with-prop :EXPORT_HUGO_BUNDLE))
(pval (when elem-pval
(cdr elem-pval))))
pval))
;; (setq bundle (org-string-nw-p (or (org-export-get-node-property :EXPORT_HUGO_BUNDLE heading :inherited)
;; (plist-get info :hugo-bundle)
;; (cdr (org-hugo--get-elem-with-prop :EXPORT_HUGO_BUNDLE)))))

;; (message "[org-hugo--heading-get-slug DBG] EXPORT_HUGO_BUNDLE: %S" bundle)
;; (message "[org-hugo--heading-get-slug DBG] point %S" (point))
;; (message "[org-hugo--heading-get-slug DBG] bundle 2: %S" (cdr (org-hugo--get-elem-with-prop :EXPORT_HUGO_BUNDLE)))

(cond
;; Leaf or branch bundle landing page.
Expand Down Expand Up @@ -4487,15 +4484,23 @@ links."
(let ((type (org-element-property :type el)))
(when (member type '("custom-id" "id" "fuzzy"))
(let* ((raw-link (org-element-property :raw-link el))

(destination (if (string= type "fuzzy")
(progn
;; Derived from ox.el -> `org-export-data'. If a broken link is seen
;; and if `broken-links' option is not nil, ignore the error.
(condition-case err
(org-export-resolve-fuzzy-link el info)
(org-link-broken
(unless (plist-get info :with-broken-links)
(unless (or (plist-get info :with-broken-links)
;; Parse the `:EXPORT_OPTIONS' property if set
;; in a parent heading.
(plist-get
(org-export--parse-option-keyword
(or (cdr (org-hugo--get-elem-with-prop
:EXPORT_OPTIONS
(org-element-property :begin el)))
""))
:with-broken-links))
(user-error "Unable to resolve link: %S" (nth 1 err))))))
(org-export-resolve-id-link el (org-export--collect-tree-properties ast info))))
(source-path (org-hugo--heading-get-slug el info :inherit-export-file-name))
Expand Down
28 changes: 28 additions & 0 deletions test/ert/telement.el
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,33 @@
<point>"
(cdr (org-hugo--get-elem-with-prop :EXPORT_HUGO_SECTION))))))

(ert-deftest test-elem/starting-position ()
"Test property lookup by specifying the starting position."

;; Here, the needed property is not found because the starting
;; position for search is set to the beginning of the buffer.
(should
(equal nil
(org-test-with-parsed-data
"
* Heading 1<point>
:PROPERTIES:
:EXPORT_OPTIONS: toc:t
:END:"
(cdr (org-hugo--get-elem-with-prop :EXPORT_OPTIONS 1)))))

;; Here, the needed property *is* found because the starting
;; position for search is set correctly, and that overrides the init
;; position set by <point> in this test.
(should
(string= "toc:t"
(org-test-with-parsed-data
"<point>
* Heading 2
:PROPERTIES:
:EXPORT_OPTIONS: toc:t
:END:"
(cdr (org-hugo--get-elem-with-prop :EXPORT_OPTIONS 2))))))


(provide 'telement)
5 changes: 0 additions & 5 deletions test/site/content-org/issues/issue-587-2.org
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
#+author:
#+macro: issue ~ox-hugo~ Issue #[[https://github.com/kaushalmodi/ox-hugo/issues/$1][$1]]

# Tue Mar 15 13:26:37 EDT 2022 - kmodi
# Workaround: "#+options: broken-links:t" works, but the
# ":EXPORT_OPTIONS: broken-links:t" property does not work for now.
#+options: broken-links:t

* Allow broken links (Subtree based export) :broken_links:
:PROPERTIES:
:EXPORT_FILE_NAME: issue-587-subtree-based-export
Expand Down

0 comments on commit e1a3e07

Please sign in to comment.