diff --git a/litorgy/litorgy-exp.el b/litorgy/litorgy-exp.el new file mode 100644 index 000000000..470b5b285 --- /dev/null +++ b/litorgy/litorgy-exp.el @@ -0,0 +1,74 @@ +;;; litorgy-exp.el --- Exportation of litorgy source blocks + +;; Copyright (C) 2009 Eric Schulte + +;; Author: Eric Schulte +;; Keywords: literate programming, reproducible research +;; Homepage: http://orgmode.org +;; Version: 0.01 + +;;; License: + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to the +;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +;; Boston, MA 02110-1301, USA. + +;;; Commentary: + +;; for more information see the comments in litorgy.el + +;;; Code: +(add-to-list 'org-export-blocks '(src litorgy-exp-src-blocks)) + +(defun litorgy-exp-src-blocks (body &rest headers) + "Process src block for export. Depending on the 'export' +headers argument in replace the source code block with... + +both ---- the default, display the code and the results + +code ---- display the code inside the block but do not process + +results - process the block and replace it with the results of + execution + +none ----- do not display either code or results upon export" + (interactive) + (unless headers (error "litorgy can't process a source block without knowing the source code")) + (message "litorgy processing...") + (let* ((lang (car headers)) + (params (litorgy-parse-header-arguments (mapconcat #'identity (cdr headers) " "))) + (export (cdr (assoc :exports params)))) + (case (intern (or export "both")) + ('none "") + ('code (litorgy-exp-code body lang params)) + ('results (litorgy-exp-results body lang params)) + ('both (concat (litorgy-exp-code body lang params) + "\n\n" + (litorgy-exp-results body lang params)))))) + +(defun litorgy-exp-code (body lang params) + (format "#+BEGIN_SRC %s\n%s%s\n#+END_SRC" lang body + (if (string-match "\n$" body) "" "\n"))) + +(defun litorgy-exp-results (body lang params) + (let* ((cmd (intern (concat "litorgy-execute:" lang))) + (result (funcall cmd body params)) + (result-as-org (litorgy-result-to-org-string result))) + (if (stringp result) + (format "#+BEGIN_EXAMPLE\n%s%s\n#+END_EXAMPLE" result + (if (string-match "\n$" body) "" "\n")) + result-as-org))) + +(provide 'litorgy-exp) +;;; litorgy-exp.el ends here diff --git a/litorgy/litorgy-init.el b/litorgy/litorgy-init.el index c583a0405..63700c381 100644 --- a/litorgy/litorgy-init.el +++ b/litorgy/litorgy-init.el @@ -30,10 +30,12 @@ ;;; Code: (require 'org) +(require 'org-exp-blocks) (load "htmlize.el") ;; other versions of htmlize can cause export problems (require 'litorgy) (require 'litorgy-ref) (require 'litorgy-ui) +(require 'litorgy-exp) ;; language specific files (require 'litorgy-script) diff --git a/litorgy/litorgy.el b/litorgy/litorgy.el index a64fa5ced..6abfa530b 100644 --- a/litorgy/litorgy.el +++ b/litorgy/litorgy.el @@ -161,19 +161,23 @@ replace - insert results after the source block replacing any previously inserted results silent -- no results are inserted" + (message (format "-%S-" result)) (if (stringp result) (setq result (litorgy-clean-text-properties result)) (unless (listp result) (setq result (format "%S" result)))) - (if (string-equal insert "replace") (litorgy-remove-result (listp result))) + (if (and insert (string-equal insert "replace")) + (litorgy-remove-result (listp result))) (if (= (length result) 0) (message "no result returned by source block") - (unless (string-equal insert "silent") + (unless (and insert (string-equal insert "silent")) (when (and (stringp result) (not (or (string-equal (substring result -1) "\n") (string-equal (substring result -1) "\r")))) (setq result (concat result "\n"))) (save-excursion - (re-search-forward "^#\\+end_src" nil t) (open-line 1) (forward-char 2) + (if (re-search-forward "^#\\+end_src" nil t) + (progn (open-line 1) (forward-char 2)) + (progn (open-line 1) (forward-char 1))) (if (stringp result) ;; assume the result is a table if it's not a string (litorgy-examplize-region (point) (progn (insert result) (point))) (progn @@ -184,6 +188,11 @@ silent -- no results are inserted" (forward-line -1) (org-cycle))))))) +(defun litorgy-result-to-org-string (result) + "Return RESULT as a string in org-mode format. This function +relies on `litorgy-insert-result'." + (with-temp-buffer (litorgy-insert-result result) (buffer-string))) + (defun litorgy-remove-result (&optional table) "Remove the result following the current source block. If optional argument TABLE is supplied then remove the table diff --git a/rorg.org b/rorg.org index 07c72caa2..43fd4f887 100644 --- a/rorg.org +++ b/rorg.org @@ -3,8 +3,8 @@ #+SEQ_TODO: TODO OPEN | DONE RESOLVED #+STARTUP: oddeven -* Tasks [5/10] -** TODO selective export of text, code, figures [0/2] +* Tasks [4/10] +** TODO selective export of text, code, figures [1/2] [DED] The litorgy buffer contains everything (code, headings and notes/prose describing what you're up to, textual/numeric/graphical code output, etc). However on export to html / LaTeX one might want @@ -12,7 +12,7 @@ want to create a presentation of what you've done which omits the code. -*** TODO export header argument +*** DONE export header argument [EMS] So I think this should be implemented as a property which can be set globally or on the outline header level (I need to review the mechanics of org-mode properties). And then as a source block @@ -25,6 +25,8 @@ show the actual code) - =both= :: show both the source code, and the results +this will be done in [[* (sandbox) selective export][(sandbox) selective export]]. + *** TODO inline source blocks Thinking there should be *two types* of source code blocks. The first should be the *large blocks* we're used to, and these should @@ -113,15 +115,20 @@ posterity. Same for a shell session either in a *shell* buffer, or pasted from another terminal emulator. And python of course. -** DONE folding of code blocks? +** TODO folding of code blocks? [1/2] [DED] In similar way to using outline-minor-mode for folding function bodies, can we fold code blocks? #+begin whatever statements are pretty ugly, and in any case when you're thinking about the overall game plan you don't necessarily want to see the code for each Step. - - Sounds good, and wasn't too hard to implement. Code blocks should - now be fold-able in the same manner as headlines (by pressing TAB - on the first line). + +*** DONE folding of source code block + Sounds good, and wasn't too hard to implement. Code blocks should + now be fold-able in the same manner as headlines (by pressing TAB + on the first line). + +*** TODO folding of results + So, lets do a three-stage tab cycle... First fold the src block, + then fold the results, then unfold. ** DONE a header argument specifying silent evaluation (no output) This would be useful across all types of source block. Currently @@ -195,7 +202,7 @@ This is currently working only with emacs lisp as in the following example in the [[* emacs lisp source reference][emacs lisp source reference]]. -* Bugs [1/1] +* Bugs [1/2] ** RESOLVED Args out of range error The following block resulted in the error below [DED]. It ran without @@ -220,6 +227,22 @@ used to be output when the block returned an empty results string. This should be fixed in the current version, you should now see the following message =no result returned by source block=. +** TODO ruby arrays not recognized as such + +Something is wrong in [[file:litorgy/ligorgy-script.el]] related to the +recognition of ruby arrays as such. + +#+begin_src ruby :results replace +[1, 2, 3, 4] +#+end_src + +: 1 + +#+begin_src python :results replace +[1, 2, 3, 4] +#+end_src + +| 1 | 2 | 3 | 4 | * Sandbox This is a place for code examples @@ -346,6 +369,13 @@ out... (transpose table) #+end_src +#+begin_src emacs-lisp +'(1 2 3 4 5) +#+end_src + +| 1 | 2 | 3 | 4 | 5 | + + *** Ruby and Python #+begin_src ruby :var table=sandbox :results replace @@ -530,7 +560,15 @@ other + 2 -** (sandboc) selective export +** (sandbox) selective export + +#+begin_begin_src :results silent :export code +class Schulte + def initialize + puts :eric + end +end +#+end_begin_src diff --git a/test-export.org b/test-export.org new file mode 100644 index 000000000..01b596c4a --- /dev/null +++ b/test-export.org @@ -0,0 +1,48 @@ +#+TITLE: Testing Litorgy Export +#+OPTIONS: toc:nil ^:nil + + +* tests + +all the tests + +** ruby + +source block is here, but is shouldn't be exported... + +#+begin_src ruby :results silent :exports none +class Schulte + def initialize + puts :eric + end +end +#+end_src + +The source block is here, and lets see the code + +#+begin_src ruby :results silent :exports code +class Schulte + def initialize + puts :eric + end +end +#+end_src + +This is a source block, and lets see the results +#+begin_src ruby :exports results :results replace +:the_results +#+end_src + +** emacs-lisp + +a table + +#+begin_src emacs-lisp +'(1 2 3 4 5) +#+end_src + +hidden + +#+begin_src emacs-lisp :exports none :results silent +9 +#+end_src