From 49f0a8d8b7a96f405413057d0165d10c93736f66 Mon Sep 17 00:00:00 2001 From: Carsten Dominik Date: Fri, 26 Dec 2008 17:37:53 +0100 Subject: [PATCH] Coderefs: Insert links in export backends This patch covers the three main backends, html, latex, and ascii. It adds the code to make sure the coderef links identified by the preprocessing will be implemented correctly when a backend does its work. --- lisp/org-exp.el | 54 +++++++++++++++++++++++++++++++++++++++--------- lisp/org-export-latex.el | 9 +++++++- lisp/org.el | 10 +++++++++ 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/lisp/org-exp.el b/lisp/org-exp.el index 8a78548ce..3bbe11a4d 100644 --- a/lisp/org-exp.el +++ b/lisp/org-exp.el @@ -1659,12 +1659,15 @@ the current file." (desc (match-end 2)) (link (org-link-unescape (match-string 1))) (slink (org-solidify-link-text link)) - found props pos + found props pos cref (target (cond ((cdr (assoc slink target-alist))) ((and (string-match "^id:" link) (cdr (assoc (substring link 3) target-alist)))) + ((string-match "^((\\(.*\\)))$" link) + (setq cref (match-string 1 link)) + (concat "coderef:" cref)) ((string-match org-link-types-re link) nil) ((or (file-name-absolute-p link) (string-match "^\\." link)) @@ -1834,6 +1837,7 @@ from the buffer." '((html "HTML" "BEGIN_HTML" "END_HTML") (ascii "ASCII" "BEGIN_ASCII" "END_ASCII") (latex "LaTeX" "BEGIN_LaTeX" "END_LaTeX"))) + (case-fold-search t) fmt) (while formatters @@ -2246,7 +2250,7 @@ in the list) and remove property and value from the list in LISTVAR." lang code trans opts) (goto-char (point-min)) (while (re-search-forward - "\\(^#\\+BEGIN_SRC:?[ \t]+\\([^ \t\n]+\\)\\(.*\\)\n\\([^\000]+?\n\\)#\\+END_SRC.*\\)\\|\\(^#\\+BEGIN_EXAMPLE:?[ \t]+\\(.*\\)\n\\([^\000]+?\n\\)#\\+END_EXAMPLE.*\\)" + "\\(^#\\+BEGIN_SRC:?[ \t]+\\([^ \t\n]+\\)\\(.*\\)\n\\([^\000]+?\n\\)#\\+END_SRC.*\\)\\|\\(^#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)#\\+END_EXAMPLE.*\\)" nil t) (if (match-end 1) ;; src segments @@ -2276,7 +2280,9 @@ Numbering lines works for all three major backends (html, latex, and ascii)." (let (num cont rtn named rpllbl keepp) (setq num (string-match "[-+]n\\>" (or opts "")) cont (string-match "\\+n\\>" (or opts "")) - rpllbl (string-match "-r\\>" (or opts ""))) + rpllbl (string-match "-r\\>" (or opts "")) + keepp (string-match "-k\\>" (or opts ""))) + (if keepp (setq rpllbl 'keep)) (setq rtn code) (when (equal lang "org") (setq rtn (with-temp-buffer @@ -2335,7 +2341,8 @@ Numbering lines works for all three major backends (html, latex, and ascii)." (defun org-export-number-lines (text backend &optional skip1 skip2 number cont replace-labels) - (if (not number) (setq replace-labels nil)) ;; must use names if no numbers + (if (and (not number) (not (eq replace-labels 'keep))) + (setq replace-labels nil)) ;; must use names if no numbers (setq skip1 (or skip1 0) skip2 (or skip2 0)) (if (not cont) (setq org-export-last-code-line-counter-value 0)) (with-temp-buffer @@ -2362,7 +2369,8 @@ Numbering lines works for all three major backends (html, latex, and ascii)." (if number (insert (format fm (incf n))) (forward-char 1)) - (when (looking-at ".*?\\([ \t]+\\(((\\(.*?\\)))\\)\\)") + (when (and (not (eq replace-labels 'keep)) + (looking-at ".*?\\([ \t]+\\(((\\(.*?\\)))\\)\\)")) (setq ref (match-string 3)) (if replace-labels (progn @@ -2472,7 +2480,7 @@ underlined headlines. The default is 3." :add-text (plist-get opt-plist :text)) "\n")) thetoc have-headings first-heading-pos - table-open table-buffer) + table-open table-buffer link desc) (let ((inhibit-read-only t)) (org-unmodified @@ -2585,9 +2593,19 @@ underlined headlines. The default is 3." (setq line (org-html-expand-for-ascii line)) ;; Replace links with the description when possible (while (string-match org-bracket-link-regexp line) - (setq line (replace-match - (if (match-end 3) "[\\3]" "[\\1]") - t nil line))) + (setq link (match-string 1 line) + desc (match-string (if (match-end 3) 3 1) line)) + (if (and (> (length link) 8) + (equal (substring link 0 8) "coderef:")) + (setq line (replace-match + (format (org-export-get-coderef-format (substring link 8) desc) + (cdr (assoc + (substring link 8) + org-export-code-refs))) + t t line)) + (setq line (replace-match + (if (match-end 3) "[\\3]" "[\\1]") + t nil line)))) (when custom-times (setq line (org-translate-time line))) (cond @@ -3421,7 +3439,7 @@ lang=\"%s\" xml:lang=\"%s\"> ;; Format the links (setq start 0) - (while (string-match org-bracket-link-analytic-regexp line start) + (while (string-match org-bracket-link-analytic-regexp++ line start) (setq start (match-beginning 0)) (setq path (save-match-data (org-link-unescape (match-string 3 line)))) @@ -3479,6 +3497,13 @@ lang=\"%s\" xml:lang=\"%s\"> (org-export-html-format-desc desc) ""))) + ((string= type "coderef") + + (setq rpl (format "%s" + path path path + (format (org-export-get-coderef-format path (and descp desc)) + (cdr (assoc path org-export-code-refs)))))) + ((functionp (setq fnc (nth 2 (assoc type org-link-protocols)))) ;; The link protocol has a function for format the link (setq rpl @@ -3806,6 +3831,15 @@ lang=\"%s\" xml:lang=\"%s\"> (kill-buffer (current-buffer))) (current-buffer))))) +(defun org-export-get-coderef-format (path desc) + (save-match-data + (if (and desc (string-match + (regexp-quote (concat "((" path "))")) + desc)) + (replace-match "%s" t t desc) + "%s"))) + + (defun org-export-html-format-href (s) "Make sure the S is valid as a href reference in an XHTML document." (save-match-data diff --git a/lisp/org-export-latex.el b/lisp/org-export-latex.el index 1f29b9214..98c5ad9c4 100644 --- a/lisp/org-export-latex.el +++ b/lisp/org-export-latex.el @@ -1159,7 +1159,7 @@ If TIMESTAMPS, convert timestamps, otherwise delete them." ;; or send some warnings. "Convert links to LaTeX." (goto-char (point-min)) - (while (re-search-forward org-bracket-link-analytic-regexp nil t) + (while (re-search-forward org-bracket-link-analytic-regexp++ nil t) (org-if-unprotected (goto-char (match-beginning 0)) (let* ((re-radio org-export-latex-all-targets-re) @@ -1171,6 +1171,7 @@ If TIMESTAMPS, convert timestamps, otherwise delete them." (if (or (file-name-absolute-p raw-path) (string-match "^\\.\\.?/" raw-path)) "file"))) + (coderefp (equal type "coderef")) (caption (org-find-text-property-in-string 'org-caption raw-path)) (attr (org-find-text-property-in-string 'org-attributes raw-path)) (label (org-find-text-property-in-string 'org-label raw-path)) @@ -1178,6 +1179,8 @@ If TIMESTAMPS, convert timestamps, otherwise delete them." imgp radiop ;; define the path of the link (path (cond + ((member type '("coderef")) + raw-path) ((member type '("http" "https" "ftp")) (concat type ":" raw-path)) ((and re-radio (string-match re-radio raw-path)) @@ -1211,6 +1214,10 @@ If TIMESTAMPS, convert timestamps, otherwise delete them." (if label (concat "\\label{" label "}") "") (or caption ""))) (if floatp "\\end{figure}\n")))) + (coderefp + (insert (format + (org-export-get-coderef-format path desc) + (cdr (assoc path org-export-code-refs))))) (radiop (insert (format "\\hyperref[%s]{%s}" (org-solidify-link-text raw-path) desc))) ((not type) diff --git a/lisp/org.el b/lisp/org.el index b03d8bb80..c86ad6543 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -3537,6 +3537,8 @@ Here is what the match groups contain after a match: 3: path 4: [desc] 5: desc") +(defvar org-bracket-link-analytic-regexp++ nil + "Like org-bracket-link-analytic-regexp, but include coderef internal type.") (defvar org-any-link-re nil "Regular expression matching any link.") @@ -3583,6 +3585,14 @@ This should be called after the variable `org-link-types' has changed." "\\]" "\\(\\[" "\\([^]]+\\)" "\\]\\)?" "\\]") + org-bracket-link-analytic-regexp++ + (concat + "\\[\\[" + "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?" + "\\([^]]+\\)" + "\\]" + "\\(\\[" "\\([^]]+\\)" "\\]\\)?" + "\\]") org-any-link-re (concat "\\(" org-bracket-link-regexp "\\)\\|\\(" org-angle-link-re "\\)\\|\\(" -- 2.11.4.GIT