Browse Source

org-export: Implement function to locally override translation table

* contrib/lisp/org-export.el (org-export-data-with-translations,
  org-export-data-with-backend): New functions.
* testing/lisp/test-org-export.el: Add tests.
Nicolas Goaziou 12 years ago
parent
commit
27f06c104e
2 changed files with 64 additions and 0 deletions
  1. 36 0
      contrib/lisp/org-export.el
  2. 28 0
      testing/lisp/test-org-export.el

+ 36 - 0
contrib/lisp/org-export.el

@@ -1925,6 +1925,11 @@ tag."
 ;; lines and white space are preserved.  The function memoizes its
 ;; results, so it is cheap to call it within translators.
 ;;
+;; It is possible to modify locally the back-end used by
+;; `org-export-data' or even use a temporary back-end by using
+;; `org-export-data-with-translations' and
+;; `org-export-data-with-backend'.
+;;
 ;; Internally, three functions handle the filtering of objects and
 ;; elements during the export.  In particular,
 ;; `org-export-ignore-element' marks an element or object so future
@@ -2046,6 +2051,37 @@ Return transcoded string."
 	     results)))
 	 (plist-get info :exported-data))))))
 
+(defun org-export-data-with-translations (data translations info)
+  "Convert DATA into another format using a given translation table.
+DATA is an element, an object, a secondary string or a string.
+TRANSLATIONS is an alist between element or object types and
+a functions handling them.  See `org-export-define-backend' for
+more information.  INFO is a plist used as a communication
+channel."
+  (org-export-data
+   data
+   ;; Set-up a new communication channel with TRANSLATIONS as the
+   ;; translate table and a new hash table for memoization.
+   (org-combine-plists
+    info
+    (list :translate-alist translations
+	  ;; Size of the hash table is reduced since this function
+	  ;; will probably be used on short trees.
+	  :exported-data (make-hash-table :test 'eq :size 401)))))
+
+(defun org-export-data-with-backend (data backend info)
+  "Convert DATA into BACKEND format.
+
+DATA is an element, an object, a secondary string or a string.
+BACKEND is a symbol.  INFO is a plist used as a communication
+channel.
+
+Unlike to `org-export-with-backend', this function will
+recursively convert DATA using BACKEND translation table."
+  (org-export-barf-if-invalid-backend backend)
+  (org-export-data-with-translations
+   data (org-export-backend-translate-table backend) info))
+
 (defun org-export--interpret-p (blob info)
   "Non-nil if element or object BLOB should be interpreted as Org syntax.
 Check is done according to export options INFO, stored as

+ 28 - 0
testing/lisp/test-org-export.el

@@ -724,6 +724,34 @@ body\n")))
 	      ((plain-text . (lambda (text contents info) "Success"))))
 	    (org-export-with-backend 'test2 "Test")))))
 
+(ert-deftest test-org-export/data-with-translations ()
+  "Test `org-export-data-with-translations' specifications."
+  (should
+   (equal
+    "Success!"
+    (org-export-data-with-translations
+     '(bold nil "Test")
+     '((plain-text . (lambda (text info) "Success"))
+       (bold . (lambda (bold contents info) (concat contents "!"))))
+     '(:with-emphasize t)))))
+
+(ert-deftest test-org-export/data-with-backend ()
+  "Test `org-export-data-with-backend' specifications."
+  ;; Error when calling an undefined back-end.
+  (should-error
+   (let (org-export-registered-backends)
+     (org-export-data-with-backend 'test "Test" nil)))
+  ;; Otherwise, export data recursively, using correct back-end.
+  (should
+   (equal
+    "Success!"
+    (let (org-export-registered-backends)
+      (org-export-define-backend test
+	((plain-text . (lambda (text info) "Success"))
+	 (bold . (lambda (bold contents info) (concat contents "!")))))
+      (org-export-data-with-backend
+       '(bold nil "Test") 'test '(:with-emphasize t))))))
+
 
 
 ;;; Export Snippets