emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Aaron Ecay <aaronecay@gmail.com>
To: Bastien <bzg@gnu.org>, Brady Trainor <algebrat@uw.edu>
Cc: emacs-orgmode@gnu.org
Subject: Re: export to org, header args disappear
Date: Wed, 21 May 2014 02:03:20 -0400	[thread overview]
Message-ID: <87oayrr7to.fsf@gmail.com> (raw)
In-Reply-To: <87lhtvswc3.fsf@bzg.ath.cx>

[-- Attachment #1: Type: text/plain, Size: 389 bytes --]

Hello Bastien and Brady,

The babel/export interface does not attempt to manage the header args
when it rewrites source blocks.  I think this code is pretty subtle.
Check out the (let (... (replacement ...)) ...) code in
‘org-babel-exp-process-buffer’ and the attached patch which fixes the
problem (superficially; I’m not sure if it’s the correct fundamental
approach).


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-org-fix-export-of-source-blocks-with-header-args.patch --]
[-- Type: text/x-diff, Size: 4496 bytes --]

From 7faf58afa659cf63042464dbd15ad62239416832 Mon Sep 17 00:00:00 2001
From: Aaron Ecay <aaronecay@gmail.com>
Date: Wed, 21 May 2014 01:58:18 -0400
Subject: [PATCH] ox-org: fix export of source blocks with header args

---
 lisp/ob-exp.el              | 52 +++++++++++++++++++++++++--------------------
 testing/lisp/test-ob-exp.el |  6 +++---
 2 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/lisp/ob-exp.el b/lisp/ob-exp.el
index 220a3c3..ce45d84 100644
--- a/lisp/ob-exp.el
+++ b/lisp/ob-exp.el
@@ -311,7 +311,7 @@ The function respects the value of the :exports header argument."
 	     (org-babel-exp-code info)))))
 
 (defcustom org-babel-exp-code-template
-  "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
+  "#+BEGIN_SRC %lang%switches%params\n%body\n#+END_SRC"
   "Template used to export the body of code blocks.
 This template may be customized to include additional information
 such as the code block name, or the values of particular header
@@ -322,7 +322,7 @@ and the following %keys may be used.
  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
+ params ---- the parameters 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
@@ -332,27 +332,33 @@ replaced with its value."
 
 (defun org-babel-exp-code (info)
   "Return the original code block formatted for export."
-  (setf (nth 1 info)
-	(if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
-	    (replace-regexp-in-string
-	     (org-babel-noweb-wrap) "" (nth 1 info))
-	  (if (org-babel-noweb-p (nth 2 info) :export)
-	      (org-babel-expand-noweb-references
-	       info org-babel-exp-reference-buffer)
-	    (nth 1 info))))
-  (org-fill-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)))
-		      (and (org-string-nw-p f) (concat " " f))))
-     ("flags" . ,(let ((f (assq :flags (nth 2 info))))
-		   (and f (concat " " (cdr f)))))
-     ,@(mapcar (lambda (pair)
-		 (cons (substring (symbol-name (car pair)) 1)
-		       (format "%S" (cdr pair))))
-	       (nth 2 info))
-     ("name"  . ,(or (nth 4 info) "")))))
+  ;; Here we assume that point is in the source block, an assumption
+  ;; we inherit from `org-babel-exp-src-block'.
+  (let* ((sb (org-element-at-point))
+	 (params (org-element-property :parameters sb)))
+    (setf (nth 1 info)
+	  (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
+	      (replace-regexp-in-string
+	       (org-babel-noweb-wrap) "" (nth 1 info))
+	    (if (org-babel-noweb-p (nth 2 info) :export)
+		(org-babel-expand-noweb-references
+		 info org-babel-exp-reference-buffer)
+	      (nth 1 info))))
+    (org-fill-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)))
+			(and (org-string-nw-p f) (concat " " f))))
+       ("flags" . ,(let ((f (assq :flags (nth 2 info))))
+		     (and f (concat " " (cdr f)))))
+       ("params" . ,(when (and params (not (string= params "")))
+		      (concat " " params)))
+       ,@(mapcar (lambda (pair)
+		   (cons (substring (symbol-name (car pair)) 1)
+			 (format "%S" (cdr pair))))
+		 (nth 2 info))
+       ("name"  . ,(or (nth 4 info) ""))))))
 
 (defun org-babel-exp-results (info type &optional silent hash)
   "Evaluate and return the results of the current code block for export.
diff --git a/testing/lisp/test-ob-exp.el b/testing/lisp/test-ob-exp.el
index 1fe810b..b5738d5 100644
--- a/testing/lisp/test-ob-exp.el
+++ b/testing/lisp/test-ob-exp.el
@@ -289,7 +289,7 @@ Here is one at the end of a line. =2=
 : 2
 
 #+NAME: src1
-#+BEGIN_SRC emacs-lisp
+#+BEGIN_SRC emacs-lisp :exports both
 \(+ 1 1)
 #+END_SRC"
     (org-test-with-temp-text
@@ -316,9 +316,9 @@ Here is one at the end of a line. =2=
   "Test exporting a source block with a flag."
   (should
    (string-match
-    "\\`#\\+BEGIN_SRC emacs-lisp -some-flag$"
+    "\\`#\\+BEGIN_SRC emacs-lisp -x$"
     (org-test-with-temp-text
-	"#+BEGIN_SRC emacs-lisp :flags -some-flag\n\(+ 1 1)\n#+END_SRC"
+	"#+BEGIN_SRC emacs-lisp -x\n\(+ 1 1)\n#+END_SRC"
       (org-export-execute-babel-code)
       (buffer-string)))))
 
-- 
1.9.3


[-- Attachment #3: Type: text/plain, Size: 15 bytes --]


--
Aaron Ecay

  reply	other threads:[~2014-05-21  6:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-16  3:58 export to org, header args disappear Brady Trainor
2014-05-21  2:24 ` Bastien
2014-05-21  6:03   ` Aaron Ecay [this message]
2014-05-21 12:06     ` Bastien

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87oayrr7to.fsf@gmail.com \
    --to=aaronecay@gmail.com \
    --cc=algebrat@uw.edu \
    --cc=bzg@gnu.org \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).