Browse Source

Org Babel now supports sed scripts

* doc/org.texi: Signal new babel language.

* lisp/ob-sed.el:
* testing/examples/ob-sed-test.org:
* testing/lisp/test-ob-sed.el: New files.
Bjarte Johansen 9 years ago
parent
commit
f6c75f2e41
4 changed files with 205 additions and 2 deletions
  1. 3 2
      doc/org.texi
  2. 107 0
      lisp/ob-sed.el
  3. 35 0
      testing/examples/ob-sed-test.org
  4. 60 0
      testing/lisp/test-ob-sed.el

+ 3 - 2
doc/org.texi

@@ -15056,8 +15056,9 @@ Code blocks in the following languages are supported.
 @item Processing.js @tab processing @tab Python @tab python 
 @item R @tab R @tab Ruby @tab ruby 
 @item Sass @tab sass @tab Scheme @tab scheme 
-@item GNU Screen @tab screen @tab shell @tab sh 
-@item SQL @tab sql @tab SQLite @tab sqlite
+@item GNU Screen @tab screen @tab Sed @tab sed
+@item shell @tab sh @tab SQL @tab sql
+@item SQLite @tab sqlite @tab @tab
 @end multitable
 
 Language-specific documentation is available for some languages.  If

+ 107 - 0
lisp/ob-sed.el

@@ -0,0 +1,107 @@
+;;; ob-sed.el --- org-babel functions for sed scripts
+
+;; Copyright (C) 2015 Free Software Foundation
+
+;; Author: Bjarte Johansen
+;; Keywords: literate programming, reproducible research
+;; Version: 0.1.0
+
+;; This file is part of GNU Emacs.
+
+;;; 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. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Provides a way to evaluate sed scripts in Org mode.
+
+;;; Usage:
+
+;; Add to your Emacs config:
+
+;; (org-babel-do-load-languages
+;;  'org-babel-load-languages
+;;  '((sed . t)))
+
+;; In addition to the normal header arguments, ob-sed also provides
+;; :cmd-line and :in-file. :cmd-line allows one to pass other flags to
+;; the sed command like the "--in-place" flag which makes sed edit the
+;; file pass to it instead of outputting to standard out or to a
+;; different file. :in-file is a header arguments that allows one to
+;; tell Org Babel which file the sed script to act on.
+
+;;; Code:
+(require 'ob)
+
+(defvar org-babel-sed-command "sed"
+  "Name of the sed executable command.")
+
+(defvar org-babel-tangle-lang-exts)
+(add-to-list 'org-babel-tangle-lang-exts '("sed" . "sed"))
+
+(defconst org-babel-header-args:sed
+  '((:cmd-line . :any)
+    (:in-file  . :any))
+  "Sed specific header arguments.")
+
+(defvar org-babel-default-header-args:sed '()
+  "Default arguments for evaluating a sed source block.")
+
+(defun org-babel-execute:sed (body params)
+  "Execute a block of sed code with Org Babel.
+BODY is the source inside a sed source block and PARAMS is an
+association list over the source block configurations.  This
+function is called by `org-babel-execute-src-block'."
+  (message "executing sed source code block")
+  (let* ((result-params (cdr (assq :result-params params)))
+         (cmd-line (cdr (assq :cmd-line params)))
+         (in-file (cdr (assq :in-file params)))
+	 (code-file (let ((file (org-babel-temp-file "sed-")))
+                      (with-temp-file file
+			(insert body)) file))
+	 (stdin (let ((stdin (cdr (assq :stdin params))))
+		   (when stdin
+		     (let ((tmp (org-babel-temp-file "sed-stdin-"))
+			   (res (org-babel-ref-resolve stdin)))
+		       (with-temp-file tmp
+			 (insert res))
+		       tmp))))
+         (cmd (mapconcat #'identity
+			 (remq nil
+			       (list org-babel-sed-command
+				     (format "--file=\"%s\"" code-file)
+				     cmd-line
+				     in-file))
+			 " ")))
+    (org-babel-reassemble-table
+     (let ((results
+            (cond
+             (stdin (with-temp-buffer
+                      (call-process-shell-command cmd stdin (current-buffer))
+                      (buffer-string)))
+             (t (org-babel-eval cmd "")))))
+       (when results
+         (org-babel-result-cond result-params
+	   results
+	   (let ((tmp (org-babel-temp-file "sed-results-")))
+	     (with-temp-file tmp (insert results))
+	     (org-babel-import-elisp-from-file tmp)))))
+     (org-babel-pick-name
+      (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
+     (org-babel-pick-name
+      (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
+
+(provide 'ob-sed)
+;;; ob-sed.el ends here

+ 35 - 0
testing/examples/ob-sed-test.org

@@ -0,0 +1,35 @@
+#+PROPERTY: results silent scalar
+#+Title: a collection of examples for ob-sed tests
+
+* Test simple execution of sed script
+  :PROPERTIES:
+  :ID:       C7E7CA6A-2601-42C9-B534-4102D62E458D
+  :END:
+
+  #+NAME: ex1
+  #+BEGIN_EXAMPLE
+    An example sentence.
+  #+END_EXAMPLE
+
+  #+BEGIN_SRC sed :stdin ex1
+    s/n example/ processed/
+    2 d
+  #+END_SRC
+
+* Test :in-file header argument
+  :PROPERTIES:
+  :ID:       54EC49AA-FE9F-4D58-812E-00FC87FAF562
+  :END:
+
+  #+BEGIN_SRC sed :in-file test1.txt
+  s/test/tested/
+  #+END_SRC
+
+* Test :cmd-line header argument
+  :PROPERTIES:
+  :ID:       E3C6A8BA-39FF-4840-BA8E-90D5C4365AB1
+  :END:
+
+  #+BEGIN_SRC sed :in-file test2.txt :cmd-line "-i"
+    s/test/tested again/
+  #+END_SRC

+ 60 - 0
testing/lisp/test-ob-sed.el

@@ -0,0 +1,60 @@
+;;; test-ob-sed.el --- tests for ob-sed.el
+
+;; Copyright (c) 2015 Bjarte Johansen
+;; Authors: Bjarte Johansen
+
+;; This file is not part of GNU Emacs.
+
+;; 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 of the License, 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+(org-test-for-executable "sed")
+(unless (featurep 'ob-sed)
+  (signal 'missing-test-dependency "Support for Sed code blocks"))
+
+(ert-deftest ob-sed-test/simple-execution-of-script ()
+  "Test simple execution of script."
+  (org-test-at-id "C7E7CA6A-2601-42C9-B534-4102D62E458D"
+    (org-babel-next-src-block)
+    (should (string= "A processed sentence.\n"
+		     (org-babel-execute-src-block)))))
+
+(ert-deftest ob-sed-test/in-file-header-argument ()
+  "Test :in-file header argument."
+  (org-test-at-id "54EC49AA-FE9F-4D58-812E-00FC87FAF562"
+    (let ((default-directory temporary-file-directory))
+      (with-temp-buffer
+	(insert "A test file.")
+	(write-file "test1.txt"))
+      (org-babel-next-src-block)
+      (should (string= "A tested file.\n"
+		       (org-babel-execute-src-block))))))
+
+(ert-deftest ob-sed-test/cmd-line-header-argument ()
+  "Test :cmd-line header argument."
+  (org-test-at-id "E3C6A8BA-39FF-4840-BA8E-90D5C4365AB1"
+    (let ((default-directory temporary-file-directory))
+      (with-temp-buffer
+	(insert "A test file.")
+	(write-file "test2.txt"))
+      (org-babel-next-src-block)
+      (org-babel-execute-src-block)
+      (should (string= "A tested again file.\n"
+		       (with-temp-buffer
+			 (insert-file-contents "test2.txt")
+			 (buffer-string)))))))
+
+
+
+;;; test-ob-sed ends here