emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [babel][PATCHES] exporting inline source code
@ 2014-06-13 17:37 Nicolas Berthier
  2014-06-22  4:02 ` Aaron Ecay
  0 siblings, 1 reply; 12+ messages in thread
From: Nicolas Berthier @ 2014-06-13 17:37 UTC (permalink / raw)
  To: emacs-orgmode

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

Hi,

Please find attached two patches for review, related to the handling
of inline source code.

The first one enables babel to export pieces of inline source code as
inline instead of as standard code blocks as it currently does.  This
is done by adding a new template
(`org-babel-exp-inline-code-template', very similar to
`org-babel-exp-code-template' for standard code blocks), and modifying
`org-babel-exp-code' to either generate a code block or an inline one.

Also, as the generated code may not define any switches nor flags, we
might end up with org elements such as "src_emacs-lisp[]{(message
"foo!")}" during the babel pass.  I think the small modification I
made to `org-babel-inline-src-block-regexp' to allow such constructs
is much simpler than handling this particular case in
`org-babel-exp-code'.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-Support-for-exporting-inline-source-code.patch --]
[-- Type: text/x-patch, Size: 3796 bytes --]

From d6b99e92546a752419ff8b8bf919c7001e8e1ec2 Mon Sep 17 00:00:00 2001
From: Nicolas Berthier <nberth@member.fsf.org>
Date: Fri, 13 Jun 2014 15:32:54 +0200
Subject: [PATCH 1/2] 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;}".

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 |  2 +-
 lisp/ob-exp.el  | 30 ++++++++++++++++++++++++++----
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index e1d4f39..062d4e1 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -203,7 +203,7 @@ 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)
    "\\(\\|\\[\\(.*?\\)\\]\\)"
    ;; (5) body
diff --git a/lisp/ob-exp.el b/lisp/ob-exp.el
index 3a47661..37625ab 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,27 @@ 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)
+
+(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 +374,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)))
-- 
2.0.0.rc4


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


The second patch implements the transcoding of inline source blocks
into HTML.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0002-ox-html.el-Support-for-exporting-inline-source-code-.patch --]
[-- Type: text/x-patch, Size: 1388 bytes --]

From 2ce230d0dda6869b4d91a4a27d40d62fdb0593d2 Mon Sep 17 00:00:00 2001
From: Nicolas Berthier <nberth@member.fsf.org>
Date: Fri, 13 Jun 2014 16:39:18 +0200
Subject: [PATCH 2/2] ox-html.el: 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 | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index 762e1dc..7ece2a9 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -2482,7 +2482,17 @@ 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))))))
+      (if lang
+	  (format "<code style=\"display: inline\" class=\"src src-%s\"%s>%s</code>" 
+		  lang label code)
+	  (format "<code style=\"display: inline\" class=\"example\"%s>\n%s</code>"
+		  label code)))))
 
 ;;;; Inlinetask
 
-- 
2.0.0.rc4


[-- Attachment #5: Type: text/plain, Size: 1063 bytes --]


These patches allow to handle the following piece of Org code, and
then to export it in both LaTeX (both with and without evaluating the
`setup' code block) and HTML.

#+BEGIN_SRC org
  ,#+TITLE: Testing inline code blocks

  The following code and its result
  src_emacs-lisp[:exports both]{(message "foo!")}
  should be inline.

  The following src_sh[:exports code]{eval `cat ~/.emacs`;} should also
  be inline.

  Ibid for src_emacs-lisp[:exports code]{(let ((x 10)) (< (* x 3) 2))}
  and src_emacs-lisp[:exports both]{(message "foo!\nbar!")} (as
  expected?).

  ,* COMMENT setup
  ,#+NAME: setup
  ,#+BEGIN_SRC emacs-lisp :exports code :results silent
    (eval-after-load "ox-latex"
      '(progn
        (set (make-local-variable 'org-latex-listings) t)
        (add-to-list (make-local-variable 'org-latex-packages-alist)
         '("" "listings"))))
  ,#+END_SRC

#+END_SRC

I have started the FSF copyright assignment process, in case these
patches are accepted.

Nicolas

-- 
Nicolas Berthier                                        FSF Member #7975

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2014-08-22 13:39 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-13 17:37 [babel][PATCHES] exporting inline source code Nicolas Berthier
2014-06-22  4:02 ` Aaron Ecay
2014-07-27 17:23   ` Bastien
2014-07-29  7:40     ` Nicolas Berthier
2014-07-29 13:19       ` Bastien
2014-07-30 16:06         ` Nicolas Berthier
2014-07-30 19:26           ` Nicolas Goaziou
2014-07-30 20:01             ` Achim Gratz
2014-07-30 20:23               ` Nicolas Goaziou
2014-08-01 12:23             ` Nicolas Berthier
2014-08-22 12:47               ` Nicolas Goaziou
2014-08-22 13:32                 ` Bastien

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).