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.
This commit is contained in:
Nicolas Goaziou 2012-03-10 20:25:32 +01:00
parent 06a1b8615d
commit beb024687b
3 changed files with 71 additions and 17 deletions

View File

@ -1720,7 +1720,7 @@ contextual information."
;; Language.
(or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
;; Source code.
(let* ((code-info (org-export-unravel-code src-block contents))
(let* ((code-info (org-export-unravel-code src-block))
(max-width
(apply 'max
(mapcar 'length
@ -1768,7 +1768,7 @@ contextual information."
;; Source code.
(format
"\\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
(apply 'max
(mapcar 'length

View File

@ -2805,12 +2805,12 @@ depending on src-block or example element's switches."
(lambda (el)
(with-temp-buffer
(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
(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
;; a label or a line number, as needed.
(when (re-search-backward ref-re nil t)
@ -2972,19 +2972,18 @@ ELEMENT is excluded from count."
;; Return value.
loc))
(defun org-export-unravel-code (element info)
(defun org-export-unravel-code (element)
"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
reference and protective comma and CDR is an alist between
relative line number (integer) and name of code reference on that
line (string)."
(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
"\\`\\([ \t]*\n\\)+" ""
(replace-regexp-in-string
@ -3001,14 +3000,14 @@ line (string)."
(replace-regexp-in-string
"^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
;; 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.
(with-ref-re
(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.
(cons
;; 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
code."
;; 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-lines (org-split-string code "\n"))
(refs (and (org-element-property :retain-labels element)

View File

@ -313,6 +313,10 @@ body\n")))
(org-test-with-temp-text "* Head1\n* Head2 (note)\n"
(should (equal (org-export-as 'test) "* Head1\n")))))))
;; Footnotes
(ert-deftest test-org-export/footnotes ()
"Test footnotes specifications."
(let ((org-footnote-section nil))
@ -380,6 +384,10 @@ body\n")))
(should (= (length (org-export-collect-footnote-definitions tree info))
4))))))
;;; Links
(ert-deftest test-org-export/fuzzy-links ()
"Test fuzz link export specifications."
;; 1. Links to invisible (keyword) targets should be ignored.
@ -565,6 +573,53 @@ Another text. (ref: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)
;;; test-org-export.el end here