ox-publish: Implement tools to resolve external fuzzy links

* lisp/ox-publish.el (org-publish-collect-numbering,
  org-publish-resolve-external-fuzzy-link): New functions.
(org-publish-org-to): Add new collecting function to final output
filter.  Move index collecting function to the same filter.
(org-publish-collect-index): Called from final output filter.

An external fuzzy link is: [[file.org::*headline search]]
This commit is contained in:
Nicolas Goaziou 2013-02-23 00:02:24 +01:00
parent 14df16d931
commit 93a70ac598
1 changed files with 55 additions and 15 deletions

View File

@ -566,13 +566,17 @@ Return output file name."
(body-p (plist-get plist :body-only))) (body-p (plist-get plist :body-only)))
(org-export-to-file (org-export-to-file
backend output-file nil nil body-p backend output-file nil nil body-p
;; Install `org-publish-collect-index' in parse tree ;; Add `org-publish-collect-numbering' and
;; filters. It isn't dependent on `:makeindex', since ;; `org-publish-collect-index' to final output
;; we want to keep it up-to-date in cache anyway. ;; filters. The latter isn't dependent on
;; `:makeindex', since we want to keep it up-to-date
;; in cache anyway.
(org-combine-plists (org-combine-plists
plist `(:filter-parse-tree plist
,(cons 'org-publish-collect-index `(:filter-final-output
(plist-get plist :filter-parse-tree))))))) ,(cons 'org-publish-collect-numbering
(cons 'org-publish-collect-index
(plist-get plist :filter-final-output))))))))
;; Remove opened buffer in the process. ;; Remove opened buffer in the process.
(unless visitingp (kill-buffer work-buffer))))) (unless visitingp (kill-buffer work-buffer)))))
@ -913,11 +917,12 @@ the project."
;;; Index generation ;;; Index generation
(defun org-publish-collect-index (tree backend info) (defun org-publish-collect-index (output backend info)
"Update index for a file with TREE in cache. "Update index for a file in cache.
BACKEND is the back-end being used for transcoding. INFO is OUTPUT is the output from transcoding current file. BACKEND is
a plist containing publishing options. the back-end that was used for transcoding. INFO is a plist
containing publishing and export options.
The index relative to current file is stored as an alist. An The index relative to current file is stored as an alist. An
association has the following shape: (TERM FILE-NAME PARENT), association has the following shape: (TERM FILE-NAME PARENT),
@ -931,9 +936,9 @@ its CDR is a string."
(org-publish-cache-set-file-property (org-publish-cache-set-file-property
file :index file :index
(delete-dups (delete-dups
(org-element-map tree 'keyword (org-element-map (plist-get info :parse-tree) 'keyword
(lambda (k) (lambda (k)
(when (equal (upcase (org-element-property :key k)) "INDEX") (when (equal (org-element-property :key k) "INDEX")
(let ((parent (org-export-get-parent-headline k))) (let ((parent (org-export-get-parent-headline k)))
(list (org-element-property :value k) (list (org-element-property :value k)
file file
@ -944,10 +949,13 @@ its CDR is a string."
((let ((id (org-element-property :CUSTOM_ID parent))) ((let ((id (org-element-property :CUSTOM_ID parent)))
(and id (cons 'custom-id id)))) (and id (cons 'custom-id id))))
(t (cons 'name (t (cons 'name
(org-element-property :raw-value parent)))))))) ;; Remove statistics cookie.
(replace-regexp-in-string
"\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
(org-element-property :raw-value parent)))))))))
info)))) info))))
;; Return parse-tree to avoid altering output. ;; Return output unchanged.
tree) output)
(defun org-publish-index-generate-theindex (project directory) (defun org-publish-index-generate-theindex (project directory)
"Retrieve full index from cache and build \"theindex.org\". "Retrieve full index from cache and build \"theindex.org\".
@ -1017,6 +1025,38 @@ publishing directory."
(insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n"))))))) (insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
;;; External Fuzzy Links Resolution
;;
;; This part implements tools to resolve [[file.org::*Some headline]]
;; links, where "file.org" belongs to the current project.
(defun org-publish-collect-numbering (output backend info)
(org-publish-cache-set-file-property
(plist-get info :input-file) :numbering
(mapcar (lambda (entry)
(cons (org-split-string
(replace-regexp-in-string
"\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
(org-element-property :raw-value (car entry))))
(cdr entry)))
(plist-get info :headline-numbering)))
;; Return output unchanged.
output)
(defun org-publish-resolve-external-fuzzy-link (file fuzzy)
"Return numbering for headline matching FUZZY search in FILE.
Return value is a list of numbers, or nil. This function allows
to resolve external fuzzy links like:
[[file.org::*fuzzy][description]"
(cdr (assoc (org-split-string
(if (eq (aref fuzzy 0) ?*) (substring fuzzy 1) fuzzy))
(org-publish-cache-get-file-property
(expand-file-name file) :numbering nil t))))
;;; Caching functions ;;; Caching functions