emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Bjarte Johansen <bjarte.johansen@infomedia.uib.no>
To: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Cc: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: Re: ob-sed
Date: Thu, 28 May 2015 13:47:59 +0200	[thread overview]
Message-ID: <751618D6-4EE2-4803-A550-EECE21006ABA@infomedia.uib.no> (raw)
In-Reply-To: <877frtd7wi.fsf@nicolasgoaziou.fr>

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

I think I have addressed all your comments in the attached patch.

Regards,
Bjarte



[-- Attachment #2: 0001-Org-Babel-now-supports-sed-scripts.patch --]
[-- Type: application/octet-stream, Size: 8358 bytes --]

From 3ffcdde852fcf968504de640f4f282d6688f9471 Mon Sep 17 00:00:00 2001
From: Bjarte Johansen <bjarte.johansen@gmail.com>
Date: Thu, 28 May 2015 13:29:09 +0200
Subject: [PATCH] Org Babel now supports sed scripts

---
 doc/org.texi                     |   5 +-
 lisp/ob-sed.el                   | 100 +++++++++++++++++++++++++++++++++++++++
 testing/examples/ob-sed-test.org |  35 ++++++++++++++
 testing/lisp/test-ob-sed.el      |  60 +++++++++++++++++++++++
 4 files changed, 198 insertions(+), 2 deletions(-)
 create mode 100644 lisp/ob-sed.el
 create mode 100644 testing/examples/ob-sed-test.org
 create mode 100644 testing/lisp/test-ob-sed.el

diff --git a/doc/org.texi b/doc/org.texi
index f10d4f5..f82566f 100644
--- a/doc/org.texi
+++ b/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 Sed @tab sed
+@item @tab shell @tab sh @item SQL @tab sql
+@item @tab SQLite @tab sqlite @tab @tab
 @end multitable
 
 Language-specific documentation is available for some languages.  If
diff --git a/lisp/ob-sed.el b/lisp/ob-sed.el
new file mode 100644
index 0000000..ee32241
--- /dev/null
+++ b/lisp/ob-sed.el
@@ -0,0 +1,100 @@
+;;; 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)))
+
+(require 'ob)
+(require 'sed-mode)
+
+(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
diff --git a/testing/examples/ob-sed-test.org b/testing/examples/ob-sed-test.org
new file mode 100644
index 0000000..aae1323
--- /dev/null
+++ b/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
diff --git a/testing/lisp/test-ob-sed.el b/testing/lisp/test-ob-sed.el
new file mode 100644
index 0000000..c108240
--- /dev/null
+++ b/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
-- 
2.3.2 (Apple Git-55)


  reply	other threads:[~2015-05-28 11:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-27  8:51 ob-sed Bjarte Johansen
2015-05-27  8:56 ` ob-sed Bjarte Johansen
2015-05-27 21:31   ` ob-sed Nicolas Goaziou
2015-05-28 11:47     ` Bjarte Johansen [this message]
2015-05-29  9:00       ` ob-sed Nicolas Goaziou
2015-05-29 13:32         ` ob-sed Bjarte Johansen
2015-05-29 17:08           ` ob-sed Suvayu Ali
2015-05-30 12:32           ` ob-sed Nicolas Goaziou
2015-05-30 12:36             ` ob-sed Bjarte Johansen
2015-05-30 13:01               ` ob-sed Nicolas Goaziou
2015-05-31  2:08               ` ob-sed Nick Dokos

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=751618D6-4EE2-4803-A550-EECE21006ABA@infomedia.uib.no \
    --to=bjarte.johansen@infomedia.uib.no \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    /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).