From e9b1bacbd967ac848d869131ae5cbfd5bfb16140 Mon Sep 17 00:00:00 2001 From: Kaushal Modi Date: Tue, 15 Mar 2022 08:19:47 -0400 Subject: [PATCH 1/4] Add test for file based export allowing broken links Ref: https://github.com/kaushalmodi/ox-hugo/issues/587 --- test/site/content-org/issues/issue-587.org | 20 +++++++++++++++++++ .../issues/issue-587-file-based-export.md | 14 +++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 test/site/content-org/issues/issue-587.org create mode 100644 test/site/content/issues/issue-587-file-based-export.md diff --git a/test/site/content-org/issues/issue-587.org b/test/site/content-org/issues/issue-587.org new file mode 100644 index 00000000..1d128d6a --- /dev/null +++ b/test/site/content-org/issues/issue-587.org @@ -0,0 +1,20 @@ +#+hugo_section: issues +#+hugo_base_dir: ../../ +#+author: +#+macro: issue ~ox-hugo~ Issue #[[https://github.com/kaushalmodi/ox-hugo/issues/$1][$1]] + +#+options: broken-links:t +#+filetags: broken_links + +#+title: Allow broken links (File based export) +#+export_file_name: issue-587-file-based-export + +#+begin_description +Test that exports finish without any error even when the post has +broken links. +#+end_description + +{{{issue(587)}}} + +- something [[foo:bar]] something +- something [[roam:git]] something diff --git a/test/site/content/issues/issue-587-file-based-export.md b/test/site/content/issues/issue-587-file-based-export.md new file mode 100644 index 00000000..952a429e --- /dev/null +++ b/test/site/content/issues/issue-587-file-based-export.md @@ -0,0 +1,14 @@ ++++ +title = "Allow broken links (File based export)" +description = """ + Test that exports finish without any error even when the post has + broken links. + """ +tags = ["broken-links"] +draft = false ++++ + +`ox-hugo` Issue #[587](https://github.com/kaushalmodi/ox-hugo/issues/587) + +- something something +- something something From d744fcdd7df95bc4d4a511e6d2e94a24f70c902d Mon Sep 17 00:00:00 2001 From: Kaushal Modi Date: Tue, 15 Mar 2022 12:47:34 -0400 Subject: [PATCH 2/4] fix: Do not do buffer pre-processing if not doing subtree exports Fixes https://github.com/kaushalmodi/ox-hugo/issues/589. Add internal helper function `org-hugo--buffer-has-valid-post-subtree-p`. Clean up the `org-hugo-export-wim-to-md` docstring. --- ox-hugo.el | 144 ++++++++++++++++++++++++++--------------------------- 1 file changed, 71 insertions(+), 73 deletions(-) diff --git a/ox-hugo.el b/ox-hugo.el index a546e53c..de53ce3a 100644 --- a/ox-hugo.el +++ b/ox-hugo.el @@ -1595,6 +1595,17 @@ This function is almost identical to `org-html--tags' from tag)) tags "")))) +(defun org-hugo--buffer-has-valid-post-subtree-p () + "Return non-nil if the current Org buffer has at least one valid post subtree. + +A valid Hugo post subtree has the `:EXPORT_FILE_NAME:' property +set to a non-empty string." + (org-with-wide-buffer + (catch 'found + (org-map-entries + (lambda () (throw 'found t)) ;Return quickly on finding the first match + "EXPORT_FILE_NAME<>\"\"")))) + ;;; Transcode Functions @@ -4289,14 +4300,7 @@ subtree-number being exported. ;; If the point is not in a valid subtree, check if there's a ;; valid subtree elsewhere in the same Org file. - (let ((valid-subtree-found - (catch 'break - (org-map-entries - (lambda () - (throw 'break t)) - ;; Only map through subtrees where EXPORT_FILE_NAME - ;; property is not empty. - "EXPORT_FILE_NAME<>\"\"")))) + (let ((valid-subtree-found (org-hugo--buffer-has-valid-post-subtree-p))) (when valid-subtree-found (message "Point is not in a valid Hugo post subtree; move to one and try again")) valid-subtree-found)))) @@ -4544,20 +4548,25 @@ Return output file's name." This is an Export \"What I Mean\" function: -- If the current subtree has the \"EXPORT_FILE_NAME\" property, export - that subtree. -- If the current subtree doesn't have that property, but one of its - parent subtrees has, then export from that subtree's scope. -- If none of the subtrees have that property (or if there are no Org - subtrees at all), call `org-hugo--export-file-to-md'. - -- If ALL-SUBTREES is non-nil, export all valid Hugo post subtrees - \(that have the \"EXPORT_FILE_NAME\" property) in the current file - to multiple Markdown posts. -- If ALL-SUBTREES is non-nil, and again if none of the subtrees have - that property (or if there are no Org subtrees), call +- If the current subtree has the \"EXPORT_FILE_NAME\" property, + export only that subtree. Return the return value of + `org-hugo--export-subtree-to-md'. + +- If the current subtree doesn't have that property, but one of + its parent subtrees has, export from that subtree's scope. + Return the return value of `org-hugo--export-subtree-to-md'. + +- If there are no valid Hugo post subtrees (that have the + \"EXPORT_FILE_NAME\" property) in the Org buffer the subtrees + have that property, do file-based + export (`org-hugo--export-file-to-md'), regardless of the value + of ALL-SUBTREES. Return the return value of `org-hugo--export-file-to-md'. +- If ALL-SUBTREES is non-nil and the Org buffer has at least 1 + valid Hugo post subtree, export all those valid post subtrees. + Return a list of output files. + A non-nil optional argument ASYNC means the process should happen asynchronously. The resulting file should be accessible through the `org-export-stack' interface. @@ -4566,64 +4575,53 @@ When optional argument VISIBLE-ONLY is non-nil, don't export contents of hidden elements. The optional argument NOERROR is passed to -`org-hugo--export-file-to-md'. - -- If ALL-SUBTREES is non-nil: - - If valid subtrees are found, return the list of output files. - - If no valid subtrees are found, return value is the same as - that of `org-hugo--export-file-to-md'. - -- If ALL-SUBTREES is nil: - - If `org-hugo--export-subtree-to-md' returns a non-nil value, return that. - - Else return the value of `org-hugo--export-file-to-md'." +`org-hugo--export-file-to-md'." (interactive "P") (let ((f-or-b-name (if (buffer-file-name) (file-name-nondirectory (buffer-file-name)) (buffer-name))) + (buf-has-subtree (org-hugo--buffer-has-valid-post-subtree-p)) ret) - (save-window-excursion - (save-restriction - (widen) - (save-excursion - (if all-subtrees - (progn ;Publish all valid Hugo post subtrees in the file. - (setq org-hugo--subtree-count 0) ;Reset the subtree count - (if org-hugo--preprocess-buffer - (let ((buffer (org-hugo--get-pre-processed-buffer))) - (with-current-buffer buffer - (setq ret (org-map-entries - (lambda () - (org-hugo--export-subtree-to-md - async visible-only :all-subtrees)) - ;; Export only the subtrees where - ;; EXPORT_FILE_NAME property is not - ;; empty. - "EXPORT_FILE_NAME<>\"\"")) - (kill-buffer buffer))) - (setq ret (org-map-entries - (lambda () - (org-hugo--export-subtree-to-md - async visible-only :all-subtrees)) - ;; Export only the subtrees where - ;; EXPORT_FILE_NAME property is not - ;; empty. - "EXPORT_FILE_NAME<>\"\""))) - (if ret - (message "[ox-hugo] Exported %d subtree%s from %s" - org-hugo--subtree-count - (if (= 1 org-hugo--subtree-count) "" "s") - f-or-b-name) - (message "[ox-hugo] No valid Hugo post subtrees were found"))) - - ;; Publish only the current valid Hugo post subtree. - (setq ret (org-hugo--export-subtree-to-md async visible-only))) - - ;; If `ret' is nil, no valid Hugo subtree was found. So - ;; call `org-hugo--export-file-to-md' directly. In that - ;; function, it will be checked if the whole Org file can be - ;; exported. - (unless ret - (setq ret (org-hugo--export-file-to-md f-or-b-name async visible-only noerror)))))) + (cond + ;; Publish all subtrees in the current Org buffer. + ((and buf-has-subtree all-subtrees) + (save-window-excursion + (org-with-wide-buffer + (setq org-hugo--subtree-count 0) ;Reset the subtree count + (if org-hugo--preprocess-buffer + (let ((buffer (org-hugo--get-pre-processed-buffer))) + (with-current-buffer buffer + (setq ret (org-map-entries + (lambda () + (org-hugo--export-subtree-to-md + async visible-only :all-subtrees)) + ;; Export only the subtrees where + ;; EXPORT_FILE_NAME property is not + ;; empty. + "EXPORT_FILE_NAME<>\"\"")) + (kill-buffer buffer))) + (setq ret (org-map-entries + (lambda () + (org-hugo--export-subtree-to-md + async visible-only :all-subtrees)) + ;; Export only the subtrees where + ;; EXPORT_FILE_NAME property is not + ;; empty. + "EXPORT_FILE_NAME<>\"\""))) + (message "[ox-hugo] Exported %d subtree%s from %s" + org-hugo--subtree-count + (if (= 1 org-hugo--subtree-count) "" "s") + f-or-b-name)))) + + ;; Publish only the current valid Hugo post subtree. When + ;; exporting only one subtree, buffer pre-processing is done + ;; inside `org-hugo--export-subtree-to-md'. + ((and buf-has-subtree (not all-subtrees)) + (setq ret (org-hugo--export-subtree-to-md async visible-only))) + + ;; Attempt file-based export. + (t + (setq ret (org-hugo--export-file-to-md f-or-b-name async visible-only noerror)))) ret)) ;;;###autoload From 63c87ee1ecfa0361dc4a6efa71ec994a3a409b9b Mon Sep 17 00:00:00 2001 From: Kaushal Modi Date: Tue, 15 Mar 2022 12:59:33 -0400 Subject: [PATCH 3/4] Move auto-update of org-id locations to the "wim" export function --- ox-hugo.el | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ox-hugo.el b/ox-hugo.el index de53ce3a..5eeb018f 100644 --- a/ox-hugo.el +++ b/ox-hugo.el @@ -4386,12 +4386,7 @@ links." (destination (if (string= type "fuzzy") (org-export-resolve-fuzzy-link link info) - (progn - ;; Update `org-id-locations' if it's nil or empty hash table - ;; to avoid broken link. - (when (or (eq org-id-locations nil) (zerop (hash-table-count org-id-locations))) - (org-id-update-id-locations (directory-files "." :full "\.org\$" :nosort))) - (org-export-resolve-id-link link (org-export--collect-tree-properties ast info))))) + (org-export-resolve-id-link link (org-export--collect-tree-properties ast info)))) (source-path (org-hugo--get-element-path link info)) (destination-path (org-hugo--get-element-path destination info)) (destination-type (org-element-type destination))) @@ -4582,6 +4577,12 @@ The optional argument NOERROR is passed to (buffer-name))) (buf-has-subtree (org-hugo--buffer-has-valid-post-subtree-p)) ret) + + ;; Auto-update `org-id-locations' if it's nil or empty hash table + ;; to avoid broken [[id:..]] type links. + (when (or (eq org-id-locations nil) (zerop (hash-table-count org-id-locations))) + (org-id-update-id-locations (directory-files "." :full "\.org\$" :nosort))) + (cond ;; Publish all subtrees in the current Org buffer. ((and buf-has-subtree all-subtrees) From 8f3ba7d878dc52371102eacaf69abe184cd91255 Mon Sep 17 00:00:00 2001 From: Kaushal Modi Date: Tue, 15 Mar 2022 13:06:09 -0400 Subject: [PATCH 4/4] Silence the "Finding ID locations .." messages --- ox-hugo.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ox-hugo.el b/ox-hugo.el index 5eeb018f..2ee01a1f 100644 --- a/ox-hugo.el +++ b/ox-hugo.el @@ -4581,7 +4581,7 @@ The optional argument NOERROR is passed to ;; Auto-update `org-id-locations' if it's nil or empty hash table ;; to avoid broken [[id:..]] type links. (when (or (eq org-id-locations nil) (zerop (hash-table-count org-id-locations))) - (org-id-update-id-locations (directory-files "." :full "\.org\$" :nosort))) + (org-id-update-id-locations (directory-files "." :full "\.org\$" :nosort) :silent)) (cond ;; Publish all subtrees in the current Org buffer.