Update internal links format

This commit is contained in:
IGARASHI Masanao
2015-06-26 08:56:47 +09:00
parent 29e32c4152
commit 5c8816b921

125
ox-rst.el
View File

@@ -99,7 +99,8 @@
(:rst-inline-images nil nil org-rst-inline-images) (:rst-inline-images nil nil org-rst-inline-images)
(:rst-inline-image-rules nil nil org-rst-inline-image-rules) (:rst-inline-image-rules nil nil org-rst-inline-image-rules)
(:rst-link-org-files-as-html nil nil org-rst-link-org-files-as-html) (:rst-link-org-files-as-html nil nil org-rst-link-org-files-as-html)
(:rst-link-home "RST_LINK_HOME" nil org-rst-link-home)) (:rst-link-home "RST_LINK_HOME" nil org-rst-link-home)
(:rst-link-use-ref-role nil nil org-rst-link-use-ref-role))
:filters-alist '((:filter-options . org-rst-math-block-options-filter) :filters-alist '((:filter-options . org-rst-math-block-options-filter)
(:filter-headline . org-rst-filter-headline-blank-lines) (:filter-headline . org-rst-filter-headline-blank-lines)
(:filter-parse-tree org-rst-math-block-tree-filter (:filter-parse-tree org-rst-math-block-tree-filter
@@ -166,6 +167,10 @@ link's path."
:type '(alist :key-type (string :tag "Type") :type '(alist :key-type (string :tag "Type")
:value-type (regexp :tag "Path"))) :value-type (regexp :tag "Path")))
(defcustom org-rst-link-use-ref-role nil
"Non-nil means export internal links using :ref: role."
:group 'org-export-rst
:type 'boolean)
(defcustom org-rst-text-markup-alist '((bold . "**%s**") (defcustom org-rst-text-markup-alist '((bold . "**%s**")
(code . verb) (code . verb)
@@ -421,82 +426,6 @@ See `org-rst-text-markup-alist' for details."
(t (format fmt text))))) (t (format fmt text)))))
(defun org-rst--describe-links (links info)
"Return a string describing a list of links.
LINKS is a list of link type objects, as returned by
`org-rst--unique-links'. INFO is a plist used as a communication
channel."
(mapconcat
(lambda (link)
(let ((type (org-element-property :type link))
(anchor (let ((desc (org-element-contents link)))
(if desc (org-export-data desc info)
(org-element-property :raw-link link)))))
(cond
;; Coderefs, radio links and fuzzy links are ignored.
((member type '("coderef" "radio" "fuzzy")) nil)
;; Id and custom-id links: Headlines refer to their numbering.
((member type '("custom-id" "id"))
(let ((dest (org-export-resolve-id-link link info)))
(concat
(format
"`%s <%s>`_"
anchor
(if (not dest) "Unknown reference"
(format
"See section %s"
(mapconcat 'number-to-string
(org-export-get-headline-number dest info) "."))))
"\n\n")))
;; Do not add a link that cannot be resolved and doesn't have
;; any description: destination is already visible in the
;; paragraph.
((not (org-element-contents link)) nil)
(t
(concat
(format "`%s <%s>`_" anchor (org-element-property :raw-link link))
"\n\n")))))
links ""))
(defun org-rst--unique-links (element info)
"Return a list of unique link references in ELEMENT.
ELEMENT is either a headline element or a section element. INFO
is a plist used as a communication channel."
(let* (seen
(unique-link-p
(function
;; Return LINK if it wasn't referenced so far, or nil.
;; Update SEEN links along the way.
(lambda (link)
(let ((footprint
;; Normalize description in footprints.
(cons (org-element-property :raw-link link)
(let ((contents (org-element-contents link)))
(and contents
(replace-regexp-in-string
"[ \r\t\n]+" " "
(org-trim
(org-element-interpret-data contents))))))))
;; Ignore LINK if it hasn't been translated already.
;; It can happen if it is located in an affiliated
;; keyword that was ignored.
(when (and (org-string-nw-p
(gethash link (plist-get info :exported-data)))
(not (member footprint seen)))
(push footprint seen) link)))))
;; If at a section, find parent headline, if any, in order to
;; count links that might be in the title.
(headline
(if (eq (org-element-type element) 'headline) element
(or (org-export-get-parent-headline element) element))))
;; Get all links in HEADLINE.
(org-element-map headline 'link
(lambda (l) (funcall unique-link-p l)) info nil nil t)))
(defun org-rst--checkbox (item info) (defun org-rst--checkbox (item info)
"Return checkbox string for ITEM or nil. "Return checkbox string for ITEM or nil.
INFO is a plist used as a communication channel." INFO is a plist used as a communication channel."
@@ -765,18 +694,14 @@ holding contextual information."
(make-string (make-string
(if org-rst-headline-spacing (car org-rst-headline-spacing) (if org-rst-headline-spacing (car org-rst-headline-spacing)
(org-element-property :pre-blank headline)) ?\n)) (org-element-property :pre-blank headline)) ?\n))
;; Even if HEADLINE has no section, there might be some (customid (org-element-property :CUSTOM_ID headline))
;; links in its title that we shouldn't forget to describe. (label (when customid
(links (format ".. _%s:\n\n" customid))))
(unless (or (eq (caar (org-element-contents headline)) 'section))
(let ((title (org-element-property :title headline)))
(when (consp title)
(org-rst--describe-links
(org-rst--unique-links title info) info))))))
(concat (concat
(or label "")
(org-rst--build-title headline info 'underline) (org-rst--build-title headline info 'underline)
"\n" pre-blanks "\n" pre-blanks
(concat (when (org-string-nw-p links) links) contents))))) contents))))
;;;; Horizontal Rule ;;;; Horizontal Rule
@@ -1112,28 +1037,20 @@ INFO is a plist holding contextual information."
(org-export-data (org-element-property :raw-link link) info))) (org-export-data (org-element-property :raw-link link) info)))
(if desc (format "`%s <%s>`_" desc rawlink) (if desc (format "`%s <%s>`_" desc rawlink)
(format "`%s`_" rawlink)))) (format "`%s`_" rawlink))))
;; LINK points to a headline. If headlines are numbered ;; LINK points to a headline.
;; and the link has no description, display headline's
;; number. Otherwise, display description or headline's
;; title.
(headline (headline
(let ((label (if (member type '("custom-id" "id"))
(mapconcat (if (plist-get info :rst-link-use-ref-role)
'number-to-string (if desc (format " :ref:`%s <%s>`" desc raw-path)
(org-export-get-headline-number destination info) (format " :ref:`%s`" raw-path))
"."))) (format "`%s`_" raw-path))
(if (and (plist-get info :section-numbers) (not desc)) (format "`%s`_" (org-rst--build-title destination info nil))))
(format "`%s`_" label) ;; Fuzzy link points to a target.
(format "`%s <%s>`_" label
(or desc
(org-export-data
(org-element-property :title destination) info))))))
;; Fuzzy link points to a target. Do as above.
(otherwise (otherwise
(if (not desc) (format "`%s`_" path) (if (not desc) (format "`%s`_" path)
(format "`%s <%s>`_" desc path)))))) (format "`%s <%s>`_" desc path))))))
;; Coderef: replace link with the reference name or the ;; Coderef: replace link with the reference name or the
;; equivalent line number. ;; equivalent line number. It is not supported in ReST.
((string= type "coderef") ((string= type "coderef")
(format (org-export-get-coderef-format path desc) (format (org-export-get-coderef-format path desc)
(org-export-resolve-coderef path info))) (org-export-resolve-coderef path info)))
@@ -1567,7 +1484,7 @@ a communication channel."
"Transcode a TARGET object from Org to reStructuredText. "Transcode a TARGET object from Org to reStructuredText.
CONTENTS is nil. INFO is a plist holding contextual CONTENTS is nil. INFO is a plist holding contextual
information." information."
(format "\n.. _%s:\n\n" (org-element-property :value target))) (format " _`%s` " (org-element-property :value target)))
;;;; Timestamp ;;;; Timestamp