Browse Source

org-export: Remove useless INFO argument from `org-export-unravel-code'

* contrib/lisp/org-export.el (org-export-unravel-code): Remove INFO
  argument.  Fix a bug preventing code references to be properly
  recognized.
(org-export-format-code-default): Apply signature change.
(org-export-resolve-coderef): Fix a bug preventing code references to
be properly recognized.
* EXPERIMENTAL/org-e-latex.el (org-e-latex-src-block): Apply signature
  change.
* testing/lisp/test-org-export.el: Add tests.
Nicolas Goaziou 13 years ago
parent
commit
beb024687b
3 changed files with 71 additions and 17 deletions
  1. 2 2
      EXPERIMENTAL/org-e-latex.el
  2. 14 15
      contrib/lisp/org-export.el
  3. 55 0
      testing/lisp/test-org-export.el

+ 2 - 2
EXPERIMENTAL/org-e-latex.el

@@ -1720,7 +1720,7 @@ contextual information."
 	      ;; Language.
 	      ;; Language.
 	      (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
 	      (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
 	      ;; Source code.
 	      ;; Source code.
-	      (let* ((code-info (org-export-unravel-code src-block contents))
+	      (let* ((code-info (org-export-unravel-code src-block))
 		     (max-width
 		     (max-width
 		      (apply 'max
 		      (apply 'max
 			     (mapcar 'length
 			     (mapcar 'length
@@ -1768,7 +1768,7 @@ contextual information."
 	 ;; Source code.
 	 ;; Source code.
 	 (format
 	 (format
 	  "\\begin{lstlisting}\n%s\\end{lstlisting}"
 	  "\\begin{lstlisting}\n%s\\end{lstlisting}"
-	  (let* ((code-info (org-export-unravel-code src-block info))
+	  (let* ((code-info (org-export-unravel-code src-block))
 		 (max-width
 		 (max-width
 		  (apply 'max
 		  (apply 'max
 			 (mapcar 'length
 			 (mapcar 'length

+ 14 - 15
contrib/lisp/org-export.el

@@ -2805,12 +2805,12 @@ depending on src-block or example element's switches."
    (lambda (el)
    (lambda (el)
      (with-temp-buffer
      (with-temp-buffer
        (insert (org-trim (org-element-property :value el)))
        (insert (org-trim (org-element-property :value el)))
-       (let* ((label-fmt (or (org-element-property :label-fmt el)
-			     org-coderef-label-format))
+       (let* ((label-fmt (regexp-quote
+			  (or (org-element-property :label-fmt el)
+			      org-coderef-label-format)))
 	      (ref-re
 	      (ref-re
 	       (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
 	       (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
-		       (regexp-quote
-			(replace-regexp-in-string "%s" ref label-fmt nil t)))))
+		       (replace-regexp-in-string "%s" ref label-fmt nil t))))
 	 ;; Element containing REF is found.  Resolve it to either
 	 ;; Element containing REF is found.  Resolve it to either
 	 ;; a label or a line number, as needed.
 	 ;; a label or a line number, as needed.
 	 (when (re-search-backward ref-re nil t)
 	 (when (re-search-backward ref-re nil t)
@@ -2972,19 +2972,18 @@ ELEMENT is excluded from count."
     ;; Return value.
     ;; Return value.
     loc))
     loc))
 
 
-(defun org-export-unravel-code (element info)
+(defun org-export-unravel-code (element)
   "Clean source code and extract references out of it.
   "Clean source code and extract references out of it.
 
 
-ELEMENT has either a `src-block' an `example-block' type.  INFO
-is a plist used as a communication channel.
+ELEMENT has either a `src-block' an `example-block' type.
 
 
 Return a cons cell whose CAR is the source code, cleaned from any
 Return a cons cell whose CAR is the source code, cleaned from any
 reference and protective comma and CDR is an alist between
 reference and protective comma and CDR is an alist between
 relative line number (integer) and name of code reference on that
 relative line number (integer) and name of code reference on that
 line (string)."
 line (string)."
   (let* ((line 0) refs
   (let* ((line 0) refs
-	 ;; Get code and clean it. Remove blank lines at its beginning
-	 ;; and end. Also remove protective commas.
+	 ;; Get code and clean it.  Remove blank lines at its
+	 ;; beginning and end.  Also remove protective commas.
 	 (code (let ((c (replace-regexp-in-string
 	 (code (let ((c (replace-regexp-in-string
 			 "\\`\\([ \t]*\n\\)+" ""
 			 "\\`\\([ \t]*\n\\)+" ""
 			 (replace-regexp-in-string
 			 (replace-regexp-in-string
@@ -3001,14 +3000,14 @@ line (string)."
 		   (replace-regexp-in-string
 		   (replace-regexp-in-string
 		    "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
 		    "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
 	 ;; Get format used for references.
 	 ;; Get format used for references.
-	 (label-fmt (or (org-element-property :label-fmt element)
-			  org-coderef-label-format))
+	 (label-fmt (regexp-quote
+		     (or (org-element-property :label-fmt element)
+			 org-coderef-label-format)))
 	 ;; Build a regexp matching a loc with a reference.
 	 ;; Build a regexp matching a loc with a reference.
 	 (with-ref-re
 	 (with-ref-re
 	  (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
 	  (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
-		  (regexp-quote
-		   (replace-regexp-in-string
-		    "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t)))))
+		  (replace-regexp-in-string
+		   "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
     ;; Return value.
     ;; Return value.
     (cons
     (cons
      ;; Code with references removed.
      ;; Code with references removed.
@@ -3066,7 +3065,7 @@ spaces.  Code references, on the other hand, appear flushed to
 the right, separated by six white spaces from the widest line of
 the right, separated by six white spaces from the widest line of
 code."
 code."
   ;; Extract code and references.
   ;; Extract code and references.
-  (let* ((code-info (org-export-unravel-code element info))
+  (let* ((code-info (org-export-unravel-code element))
          (code (car code-info))
          (code (car code-info))
          (code-lines (org-split-string code "\n"))
          (code-lines (org-split-string code "\n"))
 	 (refs (and (org-element-property :retain-labels element)
 	 (refs (and (org-element-property :retain-labels element)

+ 55 - 0
testing/lisp/test-org-export.el

@@ -313,6 +313,10 @@ body\n")))
 	(org-test-with-temp-text "* Head1\n* Head2 (note)\n"
 	(org-test-with-temp-text "* Head1\n* Head2 (note)\n"
 	  (should (equal (org-export-as 'test) "* Head1\n")))))))
 	  (should (equal (org-export-as 'test) "* Head1\n")))))))
 
 
+
+
+;; Footnotes
+
 (ert-deftest test-org-export/footnotes ()
 (ert-deftest test-org-export/footnotes ()
   "Test footnotes specifications."
   "Test footnotes specifications."
   (let ((org-footnote-section nil))
   (let ((org-footnote-section nil))
@@ -380,6 +384,10 @@ body\n")))
 	(should (= (length (org-export-collect-footnote-definitions tree info))
 	(should (= (length (org-export-collect-footnote-definitions tree info))
 		   4))))))
 		   4))))))
 
 
+
+
+;;; Links
+
 (ert-deftest test-org-export/fuzzy-links ()
 (ert-deftest test-org-export/fuzzy-links ()
   "Test fuzz link export specifications."
   "Test fuzz link export specifications."
   ;; 1. Links to invisible (keyword) targets should be ignored.
   ;; 1. Links to invisible (keyword) targets should be ignored.
@@ -565,6 +573,53 @@ Another text. (ref:text)
 		       "text"))))))
 		       "text"))))))
 
 
 
 
+
+;;; Src-block and example-block
+
+(ert-deftest test-org-export/unravel-code ()
+  "Test `org-export-unravel-code' function."
+  (let ((org-coderef-label-format "(ref:%s)"))
+    ;; 1. Code without reference.
+    (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
+      (should (equal (org-export-unravel-code (org-element-current-element))
+		     '("(+ 1 1)\n"))))
+    ;; 2. Code with reference.
+    (org-test-with-temp-text
+	"#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
+      (should (equal (org-export-unravel-code (org-element-current-element))
+		     '("(+ 1 1)\n" (1 . "test")))))
+    ;; 3. Code with user-defined reference.
+    (org-test-with-temp-text
+	"#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
+      (should (equal (org-export-unravel-code (org-element-current-element))
+		     '("(+ 1 1)\n" (1 . "test")))))
+    ;; 4. Code references keys are relative to the current block.
+    (org-test-with-temp-text "
+#+BEGIN_EXAMPLE -n
+\(+ 1 1)
+#+END_EXAMPLE
+#+BEGIN_EXAMPLE +n
+\(+ 2 2)
+\(+ 3 3) (ref:one)
+#+END_EXAMPLE"
+      (goto-line 5)
+      (should (equal (org-export-unravel-code (org-element-current-element))
+		     '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))
+    ;; 5. Free up comma-protected lines.
+    ;;
+    ;; 5.1. In an Org source block, every line is protected.
+    (org-test-with-temp-text
+	"#+BEGIN_SRC org\n,* Test\n,# comment\n,Text\n#+END_SRC"
+      (should (equal (org-export-unravel-code (org-element-current-element))
+		     '("* Test\n# comment\nText\n"))))
+    ;; 5.2. In other blocks, only headlines, comments and keywords are
+    ;;      protected.
+    (org-test-with-temp-text
+	"#+BEGIN_EXAMPLE\n,* Headline\n, * Not headline\n,Keep\n#+END_EXAMPLE"
+      (should (equal (org-export-unravel-code (org-element-current-element))
+		     '("* Headline\n, * Not headline\n,Keep\n"))))))
+
+
 
 
 (provide 'test-org-export)
 (provide 'test-org-export)
 ;;; test-org-export.el end here
 ;;; test-org-export.el end here