#!/usr/bin/emacs --script ;; -*- coding: utf-8-unix; -*- ;; modified from ob-tanbgle.el:org-babel-tangle-publish (defun roklein/tangle-publish (filename pub-dir) "Tangle FILENAME and place the results in PUB-DIR." (unless (file-exists-p pub-dir) (make-directory pub-dir t)) (mapc (lambda (el) (rename-file el (concat (directory-file-name pub-dir) "/" (file-name-nondirectory el)) t)) (org-babel-tangle-file filename))) (defun roklein/tangle-with-include (filename &optional output-filename output-directory) "Extract the body of source blocks in FILENAME. Optional argument OUTPUT-FILE can be used to have a more original file name for the tangled files (instead of FILENAME.lang, where filename retains its 'org' ending). Optional argument OUTPUT-DIRECTORY can be used to have the tangles files moved to it. Note, as the original file is exported to org first, a file FILENAME.org, or -- if given as argument -- OUTPUT-FILE will remain in the directory of the original FILENAME file." (cond ;; filename without extension must not be outfile, or the original ;; org file would be overwritten. ((equal filename output-filename) (message "%s\n" "'output-filename' must not be the same as 'filename' minus extension.") (usage)) ;; does the original orgfile exist and is readable? ((or (not (file-exists-p filename)) (not (file-readable-p filename))) (message "%s\n" "Org file 'filename' does not exist or is not readable.") (usage)) ;; does the output-directory exist? ((and output-directory (or (not (file-exists-p output-directory)) (not (file-accessible-directory-p output-directory))) (message "%s\n" "output directory does not exist"))) (t (load "~/.emacs") (find-file filename) (setq org-confirm-babel-evaluate nil) (let ((new-org-file (org-org-export-to-org)) (overwrite-existing (when output-filename (if (file-exists-p output-filename) (y-or-n-p (concat "Output file " output-filename " already exists. Overwrite? ")) t)))) (when (and output-filename overwrite-existing) (rename-file new-org-file output-filename overwrite-existing)) (when (not output-filename) (setq output-filename new-org-file)) (message "output filename: %s" output-filename) (roklein/tangle-publish output-filename (or output-directory ".")))))) (defun usage() (message "\n%s\n" "Usage: tangle.el filename [output-filename output-directory]")) (defun main () (let ((argc (length command-line-args-left))) (cond ((< argc 1) (message "%s\n" "Too few arguments.") (usage)) ((> argc 3) (message "%s\n" "Too many arguments.") (usage)) (t (apply 'roklein/tangle-with-include command-line-args-left))))) (main)