Browse Source

ob-tangle: new hook for pre-processing code block bodies on tangling

* lisp/ob-tangle.el (org-babel-tangle-body-hook): Hook for changing
  the contents of a code block body on export.
  (org-babel-tangle-collect-blocks): Apply
  `org-babel-tangle-body-hook' to the collected bodies of code blocks.
Eric Schulte 10 years ago
parent
commit
a793b77625
1 changed files with 26 additions and 16 deletions
  1. 26 16
      lisp/ob-tangle.el

+ 26 - 16
lisp/ob-tangle.el

@@ -63,6 +63,11 @@ then the name of the language is used."
   :group 'org-babel
   :type 'hook)
 
+(defcustom org-babel-tangle-body-hook nil
+  "Hook run over the contents of each code block body."
+  :group 'org-babel
+  :type 'hook)
+
 (defcustom org-babel-tangle-pad-newline t
   "Switch indicating whether to pad tangled code with newlines."
   :group 'org-babel
@@ -307,22 +312,27 @@ code blocks by language."
 		   (assignments-cmd
 		    (intern (concat "org-babel-variable-assignments:" src-lang)))
 		   (body
-		    ((lambda (body)
-		       (if (assoc :no-expand params)
-			   body
-			 (if (fboundp expand-cmd)
-			     (funcall expand-cmd body params)
-			   (org-babel-expand-body:generic
-			    body params
-			    (and (fboundp assignments-cmd)
-				 (funcall assignments-cmd params))))))
-		     (if (and (cdr (assoc :noweb params))
-			      (let ((nowebs (split-string
-					     (cdr (assoc :noweb params)))))
-				(or (member "yes" nowebs)
-				    (member "tangle" nowebs))))
-			 (org-babel-expand-noweb-references info)
-		       (nth 1 info))))
+		    ((lambda (body) ;; run the tangle-body-hook
+		       (with-temp-buffer
+			 (insert body)
+			 (run-hooks 'org-babel-tangle-body-hook)
+			 (buffer-string)))
+		     ((lambda (body) ;; expand the body in language specific manner
+			(if (assoc :no-expand params)
+			    body
+			  (if (fboundp expand-cmd)
+			      (funcall expand-cmd body params)
+			    (org-babel-expand-body:generic
+			     body params
+			     (and (fboundp assignments-cmd)
+				  (funcall assignments-cmd params))))))
+		      (if (and (cdr (assoc :noweb params)) ;; expand noweb refs
+			       (let ((nowebs (split-string
+					      (cdr (assoc :noweb params)))))
+				 (or (member "yes" nowebs)
+				     (member "tangle" nowebs))))
+			  (org-babel-expand-noweb-references info)
+			(nth 1 info)))))
 		   (comment
 		    (when (or (string= "both" (cdr (assoc :comments params)))
 			      (string= "org" (cdr (assoc :comments params))))