1
0
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2025-01-03 11:33:46 +00:00

org-export: Implement "ref" type links

* contrib/lisp/org-element.el (org-element-link-parser): Recognize
  "ref" type links as a new type of Org link.
* contrib/lisp/org-export.el (org-export-resolve-ref-link): New
  function.
* EXPERIMENTAL/org-e-ascii.el (org-e-ascii--describe-links): Do not
  describe "ref" type links at the end of each section.
(org-e-ascii-link): Handle "ref" type links.
* EXPERIMENTAL/org-e-latex.el (org-e-latex-link): Handle "ref" type
  links.

"ref" type links are Org answer to LaTeXisms like "\ref{some-label}".
Hence, [[ref:some-label]] will be replaced with the sequence number of
the element with name "#+name: some-label" during export.
This commit is contained in:
Nicolas Goaziou 2012-02-19 18:20:21 +01:00
parent deb6e7a61e
commit af2a46a76f
4 changed files with 37 additions and 2 deletions

View File

@ -66,6 +66,7 @@
(declare-function org-export-resolve-coderef "org-export" (ref info))
(declare-function org-export-resolve-fuzzy-link "org-export" (link info))
(declare-function org-export-resolve-id-link "org-export" (link info))
(declare-function org-export-resolve-ref-link "org-export" (link info))
(declare-function org-export-secondary-string
"org-export" (secondary backend info))
(declare-function org-export-table-format-info "org-export" (table))
@ -833,8 +834,8 @@ channel."
(org-element-get-property :raw-link link)
(org-export-secondary-string desc 'e-ascii info)))))
(cond
;; Coderefs and radio links are ignored.
((member type '("coderef" "radio")) nil)
;; Coderefs, radio links and ref links are ignored.
((member type '("coderef" "radio" "ref")) nil)
;; Id, custom-id and fuzzy links (with the exception of
;; targets): Headlines refer to their numbering.
((member type '("custom-id" "fuzzy" "id"))
@ -1384,6 +1385,17 @@ INFO is a plist holding contextual information."
(org-element-get-property :path link)
(cdr (assq 'radio-target org-element-object-restrictions)))
'e-ascii info))
;; Ref link: If there's no description (DESC, return link's
;; destination sequence number among elements of same
;; type. Otherwise, use DESC.
((string= type "ref")
(if (org-string-nw-p desc) desc
(format "%d"
(org-export-get-ordinal
(org-export-resolve-ref-link link info)
info nil nil
(lambda (el) (or (org-element-get-property :caption el)
(org-element-get-property :name el)))))))
;; Do not apply a special syntax on fuzzy links pointing to
;; targets.
((and (string= type "fuzzy")

View File

@ -1416,6 +1416,12 @@ INFO is a plist holding contextual information. See
(org-element-parse-secondary-string
path (cdr (assq 'radio-target org-element-object-restrictions)))
'e-latex info)))
;; Ref link: If no description is provided, reference label PATH
;; and display table number. Otherwise move to label but display
;; description instead.
((string= type "ref")
(if (not desc) (format "\\ref{%s}" path)
(format "\\hyperref[%s]{%s}" path desc)))
;; Links pointing to an headline: Find destination and build
;; appropriate referencing command.
((member type '("custom-id" "fuzzy" "id"))

View File

@ -1946,6 +1946,9 @@ Assume point is at the beginning of the link."
;; Explicit type (http, irc, bbdb...). See `org-link-types'.
((string-match org-link-re-with-space3 link)
(setq type (match-string 1 link) path (match-string 2 link)))
;; Ref type: PATH is the name of the target element.
((string-match "^ref:\\(.*\\)" link)
(setq type "ref" path (org-trim (match-string 1 link))))
;; Id type: PATH is the id.
((string-match "^id:\\([-a-f0-9]+\\)" link)
(setq type "id" path (match-string 1 link)))

View File

@ -2641,6 +2641,20 @@ is either \"id\" or \"custom-id\"."
headline))
info 'first-match)))
(defun org-export-resolve-ref-link (link info)
"Return element referenced as LINK destination.
INFO is a plist used as a communication channel.
Assume LINK type is \"ref\" and. Return value is the first
element whose `:name' property matches LINK's `:path', or nil."
(let ((name (org-element-get-property :path link)))
(org-element-map
(plist-get info :parse-tree) org-element-all-elements
(lambda (el local)
(when (string= (org-element-get-property :name el) name) el))
info 'first-match)))
(defun org-export-resolve-coderef (ref info)
"Resolve a code reference REF.