* Re: export to org, header args disappear
2014-05-21 2:24 ` Bastien
@ 2014-05-21 6:03 ` Aaron Ecay
2014-05-21 12:06 ` Bastien
0 siblings, 1 reply; 4+ messages in thread
From: Aaron Ecay @ 2014-05-21 6:03 UTC (permalink / raw)
To: Bastien, Brady Trainor; +Cc: emacs-orgmode
[-- 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
^ permalink raw reply related [flat|nested] 4+ messages in thread