emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] [babel][PATCHES] exporting inline source code


From: Nicolas Berthier
Subject: Re: [O] [babel][PATCHES] exporting inline source code
Date: Fri, 01 Aug 2014 14:23:50 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux)

Nicolas Goaziou wrote:

> Hello,
>
> Nicolas Berthier <address@hidden> writes:
>
>> In any case, as advised earlier in the discussion, I updated one patch
>> to integrate new tests for the new feature. I also slightly simplified
>> the HTML export patch to avoid useless display attributes.
>
> Thank you for this nice patch. Some comments follow.
>
>> +   "\\(?:^\\|[^-[:alnum:]]\\)\\(src_\\([^ \f\t\n\r\v\\[]+\\)"
>
> I think "[^ \f\t\n\r\v[]" is enough.

Indeed.  I fixed it in the new attached patch.  Thanks.

As for the definition of what enters into a language name, I suggest
leaving it for a subsequent modification as it's not the intended goal
of this patch.

>> +(defcustom org-babel-exp-inline-code-template
>> +  "src_%lang[%switches%flags]{%body}"
>> +  "Template used to export the body of inline code blocks.
>> +This template may be customized to include additional information
>> +such as the code block name, or the values of particular header
>> +arguments.  The template is filled out using `org-fill-template',
>> +and the following %keys may be used.
>> +
>> + lang ------ the language of the code block
>> + name ------ the name of the code block
>> + body ------ the body of the code block
>> + switches -- the switches associated to the code block
>> + flags ----- the flags passed to the code block
>> +
>> +In addition to the keys mentioned above, every header argument
>> +defined for the code block may be used as a key and will be
>> +replaced with its value."
>> +  :group 'org-babel
>> +  :type 'string)
>
> You need to add :version and :package-version values for new defcustoms.

Fixed with what I assume are the correct version numbers.  Correct me if
they are wrong.

>> +(ert-deftest ob-exp/exports-inline-code ()
>> +  (should
>> +   (string-match
>> +    (replace-regexp-in-string
>> +     "\\\\\\[]{" "\\(?:\\[]\\)?{" ;accept both src_sh[]{...} or src_sh{...}
>> +     (regexp-quote "Here is one in the middle src_sh[]{echo 1} of a line.
>> +Here is one at the end of a line. src_sh[]{echo 2}
>> +src_sh[]{echo 3} Here is one at the beginning of a line.
>> +Here is one that is also evaluated: src_sh[]{echo 4} =4=")
>> +     nil t)
>> +    (org-test-at-id "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076"
>> +      (org-narrow-to-subtree)
>> +      (org-test-with-expanded-babel-code (buffer-string))))))
>
> It is a matter of taste, but I think tests should be self-contained. In
> particular, it isn't fun debugging `org-test-at-id'. The same goes for
> other tests.

New tests in "testing/lisp/test-ob.el" are akin already existing tests
in this file that were easy to adapt.  Considering that the patch adds
mostly export-related plus minor syntactic stuff, I think testing babel
evaluation itself is not as critical as testing the babel export
functionality w.r.t the inline code export, and the org-element parser
w.r.t the new syntax.  So, I did not add self-contained tests in
"test-ob.el" itself, and concentrated on "test-ob-exp.el" and
"test-org-element.el".  Then, the new patch adds some more
self-contained tests to ease debugging simple cases, as well as basic
tests for the inline source block parser in
`test-org-element/inline-src-block-parser'.

Besides, adding new tests made me encounter two unexpected results
during export:

- First, I discovered that the data resulting from calls to
  `org-babel-parse-header-arguments' were malformed when it was called
  with strings starting with a single space (e.g., " :foo bar") as it
  led to `(intern "")' calls in this function (leading to errors in some
  other place).

  For now, I fixed it in the first patch by further extending
  `org-babel-inline-src-block-regexp' to wipe out all spaces before the
  headers matching group (as `org-babel-src-block-regexp' does), even
  though handling cases where `arg' equals "" in
  `org-babel-parse-header-arguments' may be a cleaner solution;

- Secondly, wrapping of results when exporting code with ":results code"
  switches (as in "src_emacs-lisp[:exports results :results code]{(+
  1 1)}") led to unexpected results.  I doubt writing such kind of thing
  is actually useful, but it does not seem to cost too much to handle
  simple cases anyway.  I added two new failing tests to exhibit such
  cases in the first patch, and attached a third patch fixing this
  problem.  However, I am not even sure about the results that ought to
  be expected from the export of such code blocks, and it is still easy
  to find code blocks that break it (if only by writing code printing
  newlines).

All tests pass after each patch.

>> +      (if lang
>> +      (format "<code class=\"src src-%s\"%s>%s</code>" lang label code)
>> +      (format "<code class=\"example\"%s>\n%s</code>" label code)))))
>
> LANG cannot be nil. So,
>
>   (format "<code class=\"src src-%s\"%s>%s</code>" lang label code)
>
> is sufficient.

Done. Thanks.

>From 66f602987e6479e73e13e78235c9a7f6316e1a7d Mon Sep 17 00:00:00 2001
From: Nicolas Berthier <address@hidden>
Date: Fri, 13 Jun 2014 15:32:54 +0200
Subject: [PATCH 1/3] ob: Support for exporting inline source code

* lisp/ob-exp.el (org-babel-exp-inline-code-template): New
customizable variable to export inline source code (similar to
`org-babel-exp-code-template').
(org-babel-exp-code): New `type' argument to differentiate between
inline and standard code blocks.

* lisp/ob-core.el (org-babel-inline-src-block-regexp): Allow empty set
of switches and header arguments as in "src_sh[]{echo foo;}".  Also
permit spaces before them.

* testint/lisp/test-org-element.el
(test-org-element/inline-src-block-parser): Test extended syntax for
inline source code.

* testing/lisp/test-ob-exp.el (ob-exp/exports-inline-code): New
function for testing inline source code handling.  Also add three new
failing tests exhibiting unexpected results with ":results code"
switches.

* testing/lisp/test-ob.el
(test-org-babel/org-babel-get-inline-src-block-matches): Test for
inline source blocks with empty header arguments.

* testing/examples/babel.org: New sections for testing (i) exported
inline source code (used by `ob-exp/exports-inline-code'); (ii)
parsing inline source blocks with empty header arguments (used by
`test-org-babel/org-babel-get-inline-src-block-matches').

Until now pieces of inline source code were handled as standard code
blocks during export.  These changes enable them to be exported.
---
 lisp/ob-core.el                  |  4 +-
 lisp/ob-exp.el                   | 32 ++++++++++++--
 testing/examples/babel.org       | 22 ++++++++++
 testing/lisp/test-ob-exp.el      | 93 ++++++++++++++++++++++++++++++++++++++++
 testing/lisp/test-ob.el          | 71 +++++++++++++++++-------------
 testing/lisp/test-org-element.el | 22 ++++++++++
 6 files changed, 208 insertions(+), 36 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index ccc0878..c5beb60 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -203,9 +203,9 @@ This string must include a \"%s\" which will be replaced by 
the results."
 (defvar org-babel-inline-src-block-regexp
   (concat
    ;; (1) replacement target (2) lang
-   "\\(?:^\\|[^-[:alnum:]]\\)\\(src_\\([^ \f\t\n\r\v]+\\)"
+   "\\(?:^\\|[^-[:alnum:]]\\)\\(src_\\([^ \f\t\n\r\v[]+\\)"
    ;; (3,4) (unused, headers)
-   "\\(\\|\\[\\(.*?\\)\\]\\)"
+   "\\(\\|\\[[ \t]*\\(.*?\\)\\]\\)"
    ;; (5) body
    "{\\([^\f\n\r\v]+?\\)}\\)")
   "Regexp used to identify inline src-blocks.")
diff --git a/lisp/ob-exp.el b/lisp/ob-exp.el
index 3a47661..492bc35 100644
--- a/lisp/ob-exp.el
+++ b/lisp/ob-exp.el
@@ -318,10 +318,10 @@ The function respects the value of the :exports header 
argument."
        (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result 
info)))))
     (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
       ('none (funcall silently) (funcall clean) "")
-      ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
+      ('code (funcall silently) (funcall clean) (org-babel-exp-code info type))
       ('results (org-babel-exp-results info type nil hash) "")
       ('both (org-babel-exp-results info type nil hash)
-            (org-babel-exp-code info)))))
+            (org-babel-exp-code info type)))))
 
 (defcustom org-babel-exp-code-template
   "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
@@ -343,7 +343,29 @@ replaced with its value."
   :group 'org-babel
   :type 'string)
 
-(defun org-babel-exp-code (info)
+(defcustom org-babel-exp-inline-code-template
+  "src_%lang[%switches%flags]{%body}"
+  "Template used to export the body of inline code blocks.
+This template may be customized to include additional information
+such as the code block name, or the values of particular header
+arguments.  The template is filled out using `org-fill-template',
+and the following %keys may be used.
+
+ lang ------ the language of the code block
+ name ------ the name of the code block
+ body ------ the body of the code block
+ switches -- the switches associated to the code block
+ flags ----- the flags passed to the code block
+
+In addition to the keys mentioned above, every header argument
+defined for the code block may be used as a key and will be
+replaced with its value."
+  :group 'org-babel
+  :type 'string
+  :version "24.5"
+  :package-version '(Org . "8.3"))
+
+(defun org-babel-exp-code (info type)
   "Return the original code block formatted for export."
   (setf (nth 1 info)
        (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
@@ -354,7 +376,9 @@ replaced with its value."
               info org-babel-exp-reference-buffer)
            (nth 1 info))))
   (org-fill-template
-   org-babel-exp-code-template
+   (if (eq type 'inline)
+       org-babel-exp-inline-code-template 
+       org-babel-exp-code-template)
    `(("lang"  . ,(nth 0 info))
      ("body"  . ,(org-escape-code-in-string (nth 1 info)))
      ("switches" . ,(let ((f (nth 3 info)))
diff --git a/testing/examples/babel.org b/testing/examples/babel.org
index a8015d1..8bae65b 100644
--- a/testing/examples/babel.org
+++ b/testing/examples/babel.org
@@ -200,6 +200,17 @@ Here is one in the middle src_sh{echo 1} of a line.
 Here is one at the end of a line. src_sh{echo 2}
 src_sh{echo 3} Here is one at the beginning of a line.
 
+* exported inline source block
+:PROPERTIES:
+:ID:       cd54fc88-1b6b-45b6-8511-4d8fa7fc8076
+:results:  silent
+:exports:  code
+:END:
+Here is one in the middle src_sh{echo 1} of a line.
+Here is one at the end of a line. src_sh{echo 2}
+src_sh{echo 3} Here is one at the beginning of a line.
+Here is one that is also evaluated: src_sh[:exports both]{echo 4}
+
 * mixed blocks with exports both
   :PROPERTIES:
   :ID:       5daa4d03-e3ea-46b7-b093-62c1b7632df3
@@ -283,6 +294,17 @@ src_sh{echo "One"} block at start of line
  One spaced block in  src_sh{ echo "middle" } of line
 src_sh{echo 2} blocks on the src_emacs-lisp{"same"} line
  Inline block with src_sh[:results silent]{ echo "parameters" }.
+
+* org-babel-get-inline-src-block-matches (with empty args)
+  :PROPERTIES:
+  :results:  silent
+  :ID:       d55dada7-de0e-4340-8061-787cccbedee5
+  :END:
+src_sh[]{echo "One"} block at start of line
+ One spaced block in  src_sh[]{ echo "middle" } of line
+src_sh[]{echo 2} blocks on the src_emacs-lisp[]{"same"} line
+ Inline block with src_sh[:results silent]{ echo "parameters" }.
+
 * exporting a code block with a name
   :PROPERTIES:
   :ID:       b02ddd8a-eeb8-42ab-8664-8a759e6f43d9
diff --git a/testing/lisp/test-ob-exp.el b/testing/lisp/test-ob-exp.el
index 1fe810b..e319bd2 100644
--- a/testing/lisp/test-ob-exp.el
+++ b/testing/lisp/test-ob-exp.el
@@ -197,6 +197,99 @@ Here is one at the end of a line. =2=
       (org-narrow-to-subtree)
       (org-test-with-expanded-babel-code (buffer-string))))))
 
+(ert-deftest ob-exp/exports-inline-code ()
+  (should
+   (string-match "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)}$"
+                (org-test-with-temp-text
+                 "src_emacs-lisp[:exports code]{(+ 1 1)}"
+                 (org-export-execute-babel-code)
+                 (buffer-string))))
+  (should
+   (string-match "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)}$"
+                (org-test-with-temp-text
+                 "src_emacs-lisp[ :exports code ]{(+ 1 1)}"
+                 (org-export-execute-babel-code)
+                 (buffer-string))))
+  (should
+   (string-match "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)} =2=$"
+                (org-test-with-temp-text
+                 "src_emacs-lisp[:exports both]{(+ 1 1)}"
+                 (org-export-execute-babel-code)
+                 (buffer-string))))
+  (should
+   (string-match "\\`=2=$"
+                (org-test-with-temp-text
+                 "src_emacs-lisp[:exports results :results scalar]{(+ 1 1)}"
+                 (org-export-execute-babel-code)
+                 (buffer-string))))
+  (should
+   (let ((text "foosrc_emacs-lisp[:exports code]{(+ 1 1)}"))
+     (string-match (regexp-quote text)
+                  (org-test-with-temp-text
+                   text
+                   (org-export-execute-babel-code)
+                   (buffer-string)))))
+  (should
+   (let ((text "src_emacs lisp{(+ 1 1)}"))
+     (string-match (regexp-quote text)
+                  (org-test-with-temp-text
+                   text
+                   (org-export-execute-babel-code)
+                   (buffer-string)))))
+  (should
+   (string-match
+    (replace-regexp-in-string
+     "\\\\\\[]{" "\\(?:\\[]\\)?{" ;accept both src_sh[]{...} or src_sh{...}
+     (regexp-quote "Here is one in the middle src_sh[]{echo 1} of a line.
+Here is one at the end of a line. src_sh[]{echo 2}
+src_sh[]{echo 3} Here is one at the beginning of a line.
+Here is one that is also evaluated: src_sh[]{echo 4} =4=")
+     nil t)
+    (org-test-at-id "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076"
+      (org-narrow-to-subtree)
+      (org-test-with-expanded-babel-code (buffer-string))))))
+
+(ert-deftest ob-exp/exports-inline-code-double-eval-bug ()
+  "Failing for now as the result actually is
+`#+BEGIN_SRC emacs-lisp\n2#+END_SRC\n'.
+
+Based on default header arguments for inline code blocks (:exports
+results), the resulting code block `src_emacs-lisp{2}' should also be
+evaluated."
+  :expected-result :failed
+  (should
+   (string-match "\\`=2=$"
+                (org-test-with-temp-text
+                 "src_emacs-lisp[:exports results :results code]{(+ 1 1)}"
+                 (org-export-execute-babel-code)
+                 (buffer-string)))))
+
+(ert-deftest ob-exp/exports-inline-code-eval-code-once-bug ()
+  "Ibid above, except that the resulting inline code block should not
+be evaluated. Result for now is
+`#+BEGIN_SRC emacs-lisp :exports code\n2#+END_SRC\n'"
+  :expected-result :failed
+  (should
+   (string-match "\\`src_emacs-lisp\\(?:\\[]\\)?{2}$"
+                (org-test-with-temp-text
+                 (concat "src_emacs-lisp[:exports results :results code "
+                         ":results_switches \":exports code\"]{(+ 1 1)}")
+                 (org-export-execute-babel-code)
+                 (buffer-string)))))
+
+(ert-deftest ob-exp/exports-inline-code-double-eval-exports-both-bug ()
+  "Failing for now as the result actually is 
+`src_emacs-lisp[]{(+ 1 1)} #+BEGIN_SRC emacs-lisp\n2#+END_SRC\n'."
+  :expected-result :failed
+  (should
+   (string-match (concat "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)} "
+                        "src_emacs-lisp\\(?:\\[]\\)?{2}$")
+                (org-test-with-temp-text
+                 (concat "src_emacs-lisp[:exports both :results code "
+                         ":results_switches \":exports code\"]{(+ 1 1)}")
+                 (org-export-execute-babel-code)
+                 (buffer-string)))))
+
 (ert-deftest ob-exp/export-call-line-information ()
   (org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c"
     (org-narrow-to-subtree)
diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
index 1162162..fdb5a3e 100644
--- a/testing/lisp/test-ob.el
+++ b/testing/lisp/test-ob.el
@@ -249,38 +249,49 @@ this is simple"
     (should (= 14 (org-babel-execute-src-block)))))
 
 (ert-deftest test-org-babel/inline-src-blocks ()
-  (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
-    (macrolet ((at-next (&rest body)
-                       `(progn
-                          (move-end-of-line 1)
-                          (re-search-forward org-babel-inline-src-block-regexp 
nil t)
-                          (goto-char (match-beginning 1))
-                          (save-match-data ,@body))))
-      (at-next (should (equal 1 (org-babel-execute-src-block))))
-      (at-next (should (equal 2 (org-babel-execute-src-block))))
-      (at-next (should (equal 3 (org-babel-execute-src-block)))))))
+  (macrolet ((at-next (&rest body)
+              `(progn
+                 (move-end-of-line 1)
+                 (re-search-forward org-babel-inline-src-block-regexp nil t)
+                 (goto-char (match-beginning 1))
+                 (save-match-data ,@body))))
+    (org-test-at-id
+     "54cb8dc3-298c-4883-a933-029b3c9d4b18"
+     (at-next (should (equal 1 (org-babel-execute-src-block))))
+     (at-next (should (equal 2 (org-babel-execute-src-block))))
+     (at-next (should (equal 3 (org-babel-execute-src-block)))))
+    (org-test-at-id
+     "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076"
+     (at-next (should (equal 1 (org-babel-execute-src-block))))
+     (at-next (should (equal 2 (org-babel-execute-src-block))))
+     (at-next (should (equal 3 (org-babel-execute-src-block))))
+     (at-next (should (equal 4 (org-babel-execute-src-block)))))))
 
 (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
-  (org-test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74"
-    (let ((test-point (point)))
-      (should (fboundp 'org-babel-get-inline-src-block-matches))
-      (should (re-search-forward "src_" nil t)) ;; 1
-      (should (org-babel-get-inline-src-block-matches))
-      (should (re-search-forward "}" nil (point-at-bol))) ;; 1
-      (should-not (org-babel-get-inline-src-block-matches))
-      (should (re-search-forward "in" nil t)) ;; 2
-      (should-not (org-babel-get-inline-src-block-matches))
-      (should (re-search-forward "echo" nil t)) ;; 2
-      (should (org-babel-get-inline-src-block-matches))
-      (should (re-search-forward "blocks" nil t)) ;; 3
-      (backward-char 8) ;; 3
-      (should (org-babel-get-inline-src-block-matches))
-      (forward-char 1) ;;3
-      (should-not (org-babel-get-inline-src-block-matches))
-      (should (re-search-forward ":results" nil t)) ;; 4
-      (should (org-babel-get-inline-src-block-matches))
-      (end-of-line)
-      (should-not (org-babel-get-inline-src-block-matches)))))
+  (flet ((test-at-id (id)
+          (org-test-at-id 
+           id
+           (let ((test-point (point)))
+             (should (fboundp 'org-babel-get-inline-src-block-matches))
+             (should (re-search-forward "src_" nil t)) ;; 1
+             (should (org-babel-get-inline-src-block-matches))
+             (should (re-search-forward "}" nil (point-at-bol))) ;; 1
+             (should-not (org-babel-get-inline-src-block-matches))
+             (should (re-search-forward "in" nil t)) ;; 2
+             (should-not (org-babel-get-inline-src-block-matches))
+             (should (re-search-forward "echo" nil t)) ;; 2
+             (should (org-babel-get-inline-src-block-matches))
+             (should (re-search-forward "blocks" nil t)) ;; 3
+             (backward-char 8) ;; 3
+             (should (org-babel-get-inline-src-block-matches))
+             (forward-char 1) ;;3
+             (should-not (org-babel-get-inline-src-block-matches))
+             (should (re-search-forward ":results" nil t)) ;; 4
+             (should (org-babel-get-inline-src-block-matches))
+             (end-of-line)
+             (should-not (org-babel-get-inline-src-block-matches))))))
+    (test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74")
+    (test-at-id "d55dada7-de0e-4340-8061-787cccbedee5")))
 
 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
   (let ((test-line "src_sh{echo 1}"))
diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el
index 1705a4d..0bd8501 100644
--- a/testing/lisp/test-org-element.el
+++ b/testing/lisp/test-org-element.el
@@ -1051,6 +1051,28 @@ Some other text
   (should
    (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
      (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
+  ;; With switches.
+  (should
+   (org-test-with-temp-text "src_emacs-lisp[:foo bar]{(+ 1 1)}"
+     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
+  (should
+   (org-test-with-temp-text "src_emacs-lisp[ :foo bar]{(+ 1 1)}"
+     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
+  ;; Empty switches.
+  (should
+   (org-test-with-temp-text "src_emacs-lisp[]{(+ 1 1)}"
+     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
+  ;; Invalid syntax.
+  (should-not
+   (org-test-with-temp-text "foosrc_emacs-lisp[]{(+ 1 1)}"
+     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
+  (should-not
+   (org-test-with-temp-text "src_emacs-lisp[]foo{(+ 1 1)}"
+     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
+  ;; Invalid language name
+  (should-not
+   (org-test-with-temp-text "src_emacs-\tlisp{(+ 1 1)}"
+     (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
   ;; Test parsing at the beginning of an item.
   (should
    (org-test-with-temp-text "- src_emacs-lisp{(+ 1 1)}"
-- 
2.0.0.rc4

>From 7596530cb0b59aee5060d333c9ecd0ece6dc6374 Mon Sep 17 00:00:00 2001
From: Nicolas Berthier <address@hidden>
Date: Fri, 13 Jun 2014 16:39:18 +0200
Subject: [PATCH 2/3] ox-html: Support for exporting inline source code to HTML

* lisp/ox-html.el (org-html-inline-src-block): support for exporting
inline source code to HTML.
---
 lisp/ox-html.el | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index 22d0b04..a730984 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -2411,7 +2411,13 @@ CONTENTS holds the contents of the item.  INFO is a 
plist holding
 contextual information."
   (let* ((org-lang (org-element-property :language inline-src-block))
         (code (org-element-property :value inline-src-block)))
-    (error "Cannot export inline src block")))
+    (let ((lang (org-element-property :language inline-src-block))
+         (code (org-html-format-code inline-src-block info))
+         (label (let ((lbl (org-element-property :name inline-src-block)))
+                  (if (not lbl) ""
+                      (format " id=\"%s\""
+                              (org-export-solidify-link-text lbl))))))
+      (format "<code class=\"src src-%s\"%s>%s</code>" lang label code))))
 
 ;;;; Inlinetask
 
-- 
2.0.0.rc4

>From 326d51913a0d69fa42c67814f11f2c844acc0076 Mon Sep 17 00:00:00 2001
From: Nicolas Berthier <address@hidden>
Date: Fri, 1 Aug 2014 11:28:05 +0200
Subject: [PATCH 3/3] ob-core: Preserve inline-ness of source blocks when
 inserting results

* lisp/ob-core.el (org-babel-insert-result): Preserve inline-ness of
source blocks.

* testing/lisp/test-ob-exp.el: Update newly passing tests.
---
 lisp/ob-core.el             | 36 ++++++++++++++++++++++++------------
 testing/lisp/test-ob-exp.el | 19 +++++--------------
 2 files changed, 29 insertions(+), 26 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index c5beb60..50a13c2 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2094,9 +2094,11 @@ drawer -- results are added directly to the Org-mode 
file as with
           \"raw\", but are wrapped in a RESULTS drawer, allowing
           them to later be replaced or removed automatically.
 
-org ----- results are added inside of a \"#+BEGIN_SRC org\" block.
-          They are not comma-escaped when inserted, but Org syntax
-          here will be discarded when exporting the file.
+org ----- results are added inside of a \"src_org{}\" or \"#+BEGIN_SRC
+          org\" block depending on whether the current source block is
+          inline or not.  They are not comma-escaped when inserted,
+          but Org syntax here will be discarded when exporting the
+          file.
 
 html ---- results are added inside of a #+BEGIN_HTML block.  This
           is a good option if you code block will output html
@@ -2108,8 +2110,9 @@ latex --- results are added inside of a #+BEGIN_LATEX 
block.
 
 code ---- the results are extracted in the syntax of the source
           code of the language being evaluated and are added
-          inside of a #+BEGIN_SRC block with the source-code
-          language set appropriately.  Note this relies on the
+          inside of a source block with the source-code language
+          set appropriately.  Also, source block inlining is
+          preserved in this case.  Note this relies on the
           optional LANG argument."
   (if (stringp result)
       (progn
@@ -2171,12 +2174,15 @@ code ---- the results are extracted in the syntax of 
the source
                 ((member "prepend" result-params)))) ; already there
              (setq results-switches
                    (if results-switches (concat " " results-switches) ""))
-             (let ((wrap (lambda (start finish &optional no-escape)
-                           (goto-char end) (insert (concat finish "\n"))
-                           (goto-char beg) (insert (concat start "\n"))
+             (let ((wrap (lambda (start finish &optional no-escape no-newlines)
+                           (goto-char end)
+                           (insert (concat finish (unless no-newlines "\n")))
+                           (goto-char beg)
+                           (insert (concat start (unless no-newlines "\n")))
                            (unless no-escape
                              (org-escape-code-in-region (min (point) end) end))
-                           (goto-char end) (goto-char (point-at-eol))
+                           (goto-char end)
+                           (unless no-newlines (goto-char (point-at-eol)))
                            (setq end (point-marker))))
                    (proper-list-p (lambda (it) (and (listp it) (null (cdr 
(last it)))))))
                ;; insert results based on type
@@ -2224,10 +2230,16 @@ code ---- the results are extracted in the syntax of 
the source
                  (funcall wrap "#+BEGIN_LaTeX" "#+END_LaTeX"))
                 ((member "org" result-params)
                  (goto-char beg) (if (org-at-table-p) (org-cycle))
-                 (funcall wrap "#+BEGIN_SRC org" "#+END_SRC"))
+                 (if inlinep
+                     (funcall wrap "src_org{" "}" nil t)
+                     (funcall wrap "#+BEGIN_SRC org" "#+END_SRC")))
                 ((member "code" result-params)
-                 (funcall wrap (format "#+BEGIN_SRC %s%s" (or lang "none") 
results-switches)
-                          "#+END_SRC"))
+                 (let ((lang (or lang "none")))
+                   (if inlinep
+                       (funcall wrap (format "src_%s[%s]{" lang 
results-switches)
+                                "}" nil t)
+                       (funcall wrap (format "#+BEGIN_SRC %s%s" lang 
results-switches)
+                                "#+END_SRC"))))
                 ((member "raw" result-params)
                  (goto-char beg) (if (org-at-table-p) (org-cycle)))
                 ((or (member "drawer" result-params)
diff --git a/testing/lisp/test-ob-exp.el b/testing/lisp/test-ob-exp.el
index e319bd2..456f794 100644
--- a/testing/lisp/test-ob-exp.el
+++ b/testing/lisp/test-ob-exp.el
@@ -249,14 +249,10 @@ Here is one that is also evaluated: src_sh[]{echo 4} =4=")
       (org-narrow-to-subtree)
       (org-test-with-expanded-babel-code (buffer-string))))))
 
-(ert-deftest ob-exp/exports-inline-code-double-eval-bug ()
-  "Failing for now as the result actually is
-`#+BEGIN_SRC emacs-lisp\n2#+END_SRC\n'.
-
-Based on default header arguments for inline code blocks (:exports
+(ert-deftest ob-exp/exports-inline-code-double-eval ()
+  "Based on default header arguments for inline code blocks (:exports
 results), the resulting code block `src_emacs-lisp{2}' should also be
 evaluated."
-  :expected-result :failed
   (should
    (string-match "\\`=2=$"
                 (org-test-with-temp-text
@@ -264,11 +260,9 @@ evaluated."
                  (org-export-execute-babel-code)
                  (buffer-string)))))
 
-(ert-deftest ob-exp/exports-inline-code-eval-code-once-bug ()
+(ert-deftest ob-exp/exports-inline-code-eval-code-once ()
   "Ibid above, except that the resulting inline code block should not
-be evaluated. Result for now is
-`#+BEGIN_SRC emacs-lisp :exports code\n2#+END_SRC\n'"
-  :expected-result :failed
+be evaluated."
   (should
    (string-match "\\`src_emacs-lisp\\(?:\\[]\\)?{2}$"
                 (org-test-with-temp-text
@@ -277,10 +271,7 @@ be evaluated. Result for now is
                  (org-export-execute-babel-code)
                  (buffer-string)))))
 
-(ert-deftest ob-exp/exports-inline-code-double-eval-exports-both-bug ()
-  "Failing for now as the result actually is 
-`src_emacs-lisp[]{(+ 1 1)} #+BEGIN_SRC emacs-lisp\n2#+END_SRC\n'."
-  :expected-result :failed
+(ert-deftest ob-exp/exports-inline-code-double-eval-exports-both ()
   (should
    (string-match (concat "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)} "
                         "src_emacs-lisp\\(?:\\[]\\)?{2}$")
-- 
2.0.0.rc4

Regards,

-- 
Nicolas Berthier                                        FSF Member #7975

reply via email to

[Prev in Thread] Current Thread [Next in Thread]