Browse Source

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.
Carsten Dominik 17 years ago
parent
commit
49f0a8d8b7
3 changed files with 62 additions and 11 deletions
  1. 44 10
      lisp/org-exp.el
  2. 8 1
      lisp/org-export-latex.el
  3. 10 0
      lisp/org.el

+ 44 - 10
lisp/org-exp.el

@@ -1659,12 +1659,15 @@ the current file."
 	    (desc (match-end 2))
 	    (desc (match-end 2))
 	    (link (org-link-unescape (match-string 1)))
 	    (link (org-link-unescape (match-string 1)))
 	    (slink (org-solidify-link-text link))
 	    (slink (org-solidify-link-text link))
-	    found props pos
+	    found props pos cref
 	    (target
 	    (target
 	     (cond
 	     (cond
 	      ((cdr (assoc slink target-alist)))
 	      ((cdr (assoc slink target-alist)))
 	      ((and (string-match "^id:" link)
 	      ((and (string-match "^id:" link)
 		    (cdr (assoc (substring link 3) target-alist))))
 		    (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)
 	      ((string-match org-link-types-re link) nil)
 	      ((or (file-name-absolute-p link)
 	      ((or (file-name-absolute-p link)
 		   (string-match "^\\." link))
 		   (string-match "^\\." link))
@@ -1834,6 +1837,7 @@ from the buffer."
 	 '((html "HTML" "BEGIN_HTML" "END_HTML")
 	 '((html "HTML" "BEGIN_HTML" "END_HTML")
 	   (ascii "ASCII" "BEGIN_ASCII" "END_ASCII")
 	   (ascii "ASCII" "BEGIN_ASCII" "END_ASCII")
 	   (latex "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
 	   (latex "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
+	(case-fold-search t)
 	fmt)
 	fmt)
 
 
     (while formatters
     (while formatters
@@ -2246,7 +2250,7 @@ in the list) and remove property and value from the list in LISTVAR."
 	lang code trans opts)
 	lang code trans opts)
     (goto-char (point-min))
     (goto-char (point-min))
     (while (re-search-forward
     (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)
 	    nil t)
       (if (match-end 1)
       (if (match-end 1)
 	  ;; src segments
 	  ;; 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)
     (let (num cont rtn named rpllbl keepp)
       (setq num (string-match "[-+]n\\>" (or opts ""))
       (setq num (string-match "[-+]n\\>" (or opts ""))
 	    cont (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)
       (setq rtn code)
       (when (equal lang "org")
       (when (equal lang "org")
 	(setq rtn (with-temp-buffer
 	(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
 (defun org-export-number-lines (text backend
 				     &optional skip1 skip2 number cont
 				     &optional skip1 skip2 number cont
 				     replace-labels)
 				     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))
   (setq skip1 (or skip1 0) skip2 (or skip2 0))
   (if (not cont) (setq org-export-last-code-line-counter-value 0))
   (if (not cont) (setq org-export-last-code-line-counter-value 0))
   (with-temp-buffer
   (with-temp-buffer
@@ -2362,7 +2369,8 @@ Numbering lines works for all three major backends (html, latex, and ascii)."
 	(if number
 	(if number
 	    (insert (format fm (incf n)))
 	    (insert (format fm (incf n)))
 	  (forward-char 1))
 	  (forward-char 1))
-	(when (looking-at ".*?\\([ \t]+\\(((\\(.*?\\)))\\)\\)")
+	(when (and (not (eq replace-labels 'keep))
+		   (looking-at ".*?\\([ \t]+\\(((\\(.*?\\)))\\)\\)"))
 	  (setq ref (match-string 3))
 	  (setq ref (match-string 3))
 	  (if replace-labels
 	  (if replace-labels
 	      (progn
 	      (progn
@@ -2472,7 +2480,7 @@ underlined headlines.  The default is 3."
 		  :add-text (plist-get opt-plist :text))
 		  :add-text (plist-get opt-plist :text))
 		 "\n"))
 		 "\n"))
 	 thetoc have-headings first-heading-pos
 	 thetoc have-headings first-heading-pos
-	 table-open table-buffer)
+	 table-open table-buffer link desc)
 
 
     (let ((inhibit-read-only t))
     (let ((inhibit-read-only t))
       (org-unmodified
       (org-unmodified
@@ -2585,9 +2593,19 @@ underlined headlines.  The default is 3."
       (setq line (org-html-expand-for-ascii line))
       (setq line (org-html-expand-for-ascii line))
       ;; Replace links with the description when possible
       ;; Replace links with the description when possible
       (while (string-match org-bracket-link-regexp line)
       (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
       (when custom-times
 	(setq line (org-translate-time line)))
 	(setq line (org-translate-time line)))
       (cond
       (cond
@@ -3421,7 +3439,7 @@ lang=\"%s\" xml:lang=\"%s\">
 
 
 	  ;; Format the links
 	  ;; Format the links
 	  (setq start 0)
 	  (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 start (match-beginning 0))
 	    (setq path (save-match-data (org-link-unescape
 	    (setq path (save-match-data (org-link-unescape
 					 (match-string 3 line))))
 					 (match-string 3 line))))
@@ -3479,6 +3497,13 @@ lang=\"%s\" xml:lang=\"%s\">
 				(org-export-html-format-desc desc)
 				(org-export-html-format-desc desc)
 				"</a>")))
 				"</a>")))
 
 
+	     ((string= type "coderef")
+
+	      (setq rpl (format "<a href=\"#coderef-%s\" class=\"coderef\" onmouseover=\"CodeHighlightOn(this, 'coderef-%s');\" onmouseout=\"CodeHighlightOff(this, 'coderef-%s');\">%s</a>"
+				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))))
 	     ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
 	      ;; The link protocol has a function for format the link
 	      ;; The link protocol has a function for format the link
 	      (setq rpl
 	      (setq rpl
@@ -3806,6 +3831,15 @@ lang=\"%s\" xml:lang=\"%s\">
 	    (kill-buffer (current-buffer)))
 	    (kill-buffer (current-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)
 (defun org-export-html-format-href (s)
   "Make sure the S is valid as a href reference in an XHTML document."
   "Make sure the S is valid as a href reference in an XHTML document."
   (save-match-data
   (save-match-data

+ 8 - 1
lisp/org-export-latex.el

@@ -1159,7 +1159,7 @@ If TIMESTAMPS, convert timestamps, otherwise delete them."
   ;; or send some warnings.
   ;; or send some warnings.
   "Convert links to LaTeX."
   "Convert links to LaTeX."
   (goto-char (point-min))
   (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
     (org-if-unprotected
      (goto-char (match-beginning 0))
      (goto-char (match-beginning 0))
      (let* ((re-radio org-export-latex-all-targets-re)
      (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)
 		      (if (or (file-name-absolute-p raw-path)
 			      (string-match "^\\.\\.?/" raw-path))
 			      (string-match "^\\.\\.?/" raw-path))
 			  "file")))
 			  "file")))
+	    (coderefp (equal type "coderef"))
 	    (caption (org-find-text-property-in-string 'org-caption raw-path))
 	    (caption (org-find-text-property-in-string 'org-caption raw-path))
 	    (attr (org-find-text-property-in-string 'org-attributes raw-path))
 	    (attr (org-find-text-property-in-string 'org-attributes raw-path))
 	    (label (org-find-text-property-in-string 'org-label 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
 	    imgp radiop
 	    ;; define the path of the link
 	    ;; define the path of the link
 	    (path (cond
 	    (path (cond
+		   ((member type '("coderef"))
+		    raw-path)
 		   ((member type '("http" "https" "ftp"))
 		   ((member type '("http" "https" "ftp"))
 		    (concat type ":" raw-path))
 		    (concat type ":" raw-path))
 		   ((and re-radio (string-match re-radio 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 "}") "")
 			    (if label (concat "\\label{" label "}") "")
 			    (or caption "")))
 			    (or caption "")))
 		(if floatp "\\end{figure}\n"))))
 		(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}"
 	     (radiop (insert (format "\\hyperref[%s]{%s}"
 				     (org-solidify-link-text raw-path) desc)))
 				     (org-solidify-link-text raw-path) desc)))
 	     ((not type)
 	     ((not type)

+ 10 - 0
lisp/org.el

@@ -3537,6 +3537,8 @@ Here is what the match groups contain after a match:
 3: path
 3: path
 4: [desc]
 4: [desc]
 5: 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
 (defvar org-any-link-re nil
   "Regular expression matching any link.")
   "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
 	org-any-link-re
 	(concat "\\(" org-bracket-link-regexp "\\)\\|\\("
 	(concat "\\(" org-bracket-link-regexp "\\)\\|\\("
 		org-angle-link-re "\\)\\|\\("
 		org-angle-link-re "\\)\\|\\("