Browse Source

ox-org: Add interactive functions to back-end

* lisp/ox-org.el (org): Add a menu entry for the back-end.
(org-org-export-as-org, org-org-export-to-org): New functions.
* lisp/org.el (org-export-backends): Accept `org' as a loadable
  back-end.
Nicolas Goaziou 8 years ago
parent
commit
3d65b46c0c
2 changed files with 98 additions and 4 deletions
  1. 1 0
      lisp/org.el
  2. 97 4
      lisp/ox-org.el

+ 1 - 0
lisp/org.el

@@ -495,6 +495,7 @@ depends on, if any."
 	      (const :tag "   man         Export buffer to MAN format" man)
 	      (const :tag "   md          Export buffer to Markdown format" md)
 	      (const :tag "   odt         Export buffer to ODT format" odt)
+	      (const :tag "   org         Export buffer to Org format" org)
 	      (const :tag "   texinfo     Export buffer to Texinfo format" texinfo)
 	      (const :tag "C  confluence  Export buffer to Confluence Wiki format" confluence)
 	      (const :tag "C  deck        Export buffer to deck.js presentations" deck)

+ 97 - 4
lisp/ox-org.el

@@ -20,9 +20,13 @@
 
 ;;; Commentary:
 
-;; This library implements an Org back-end for Org exporter.  Since
-;; its usage is mainly internal, it doesn't provide any interactive
-;; function.
+;; This library implements an Org back-end for Org exporter.
+;;
+;; It introduces two interactive functions, `org-org-export-as-org'
+;; and `org-org-export-to-org', which export, respectively, to
+;; a temporary buffer and to a file.
+;;
+;; A publishing function is also provided: `org-org-publish-to-org'.
 
 ;;; Code:
 (require 'ox)
@@ -102,7 +106,15 @@ setting of `org-html-htmlize-output-type' is 'css."
     (timestamp . org-org-identity)
     (underline . org-org-identity)
     (verbatim . org-org-identity)
-    (verse-block . org-org-identity)))
+    (verse-block . org-org-identity))
+  :menu-entry
+  '(?O "Export to Org"
+       ((?O "As Org buffer" org-org-export-as-org)
+	(?o "As Org file" org-org-export-to-org)
+	(?v "As Org file and open"
+	    (lambda (a s v b)
+	      (if a (org-org-export-to-org t s v b)
+		(org-open-file (org-org-export-to-org nil s v b))))))))
 
 (defun org-org-identity (blob contents info)
   "Transcode BLOB element or object back into Org syntax."
@@ -131,6 +143,86 @@ Ignore keywords targeted at other export back-ends."
 		   org-element-block-name-alist))
     (org-element-keyword-interpreter keyword nil)))
 
+;;;###autoload
+(defun org-org-export-as-org (&optional async subtreep visible-only ext-plist)
+  "Export current buffer to an Org buffer.
+
+If narrowing is active in the current buffer, only export its
+narrowed part.
+
+If a region is active, export that region.
+
+A non-nil optional argument ASYNC means the process should happen
+asynchronously.  The resulting buffer should be accessible
+through the `org-export-stack' interface.
+
+When optional argument SUBTREEP is non-nil, export the sub-tree
+at point, extracting information from the headline properties
+first.
+
+When optional argument VISIBLE-ONLY is non-nil, don't export
+contents of hidden elements.
+
+EXT-PLIST, when provided, is a property list with external
+parameters overriding Org default settings, but still inferior to
+file-local settings.
+
+Export is done in a buffer named \"*Org ORG Export*\", which will
+be displayed when `org-export-show-temporary-export-buffer' is
+non-nil."
+  (interactive)
+  (if async
+      (org-export-async-start
+	  (lambda (output)
+	    (with-current-buffer (get-buffer-create "*Org ORG Export*")
+	      (erase-buffer)
+	      (insert output)
+	      (goto-char (point-min))
+	      (org-mode)
+	      (org-export-add-to-stack (current-buffer) 'org)))
+	`(org-export-as 'org ,subtreep ,visible-only nil ',ext-plist))
+    (let ((outbuf
+	   (org-export-to-buffer
+	    'org "*Org ORG Export*" subtreep visible-only nil ext-plist)))
+      (with-current-buffer outbuf (org-mode))
+      (when org-export-show-temporary-export-buffer
+	(switch-to-buffer-other-window outbuf)))))
+
+;;;###autoload
+(defun org-org-export-to-org (&optional async subtreep visible-only ext-plist)
+  "Export current buffer to an org file.
+
+If narrowing is active in the current buffer, only export its
+narrowed part.
+
+If a region is active, export that region.
+
+A non-nil optional argument ASYNC means the process should happen
+asynchronously.  The resulting file should be accessible through
+the `org-export-stack' interface.
+
+When optional argument SUBTREEP is non-nil, export the sub-tree
+at point, extracting information from the headline properties
+first.
+
+When optional argument VISIBLE-ONLY is non-nil, don't export
+contents of hidden elements.
+
+EXT-PLIST, when provided, is a property list with external
+parameters overriding Org default settings, but still inferior to
+file-local settings.
+
+Return output file name."
+  (interactive)
+  (let ((outfile (org-export-output-file-name ".org" subtreep)))
+    (if async
+	(org-export-async-start
+	    (lambda (f) (org-export-add-to-stack f 'org))
+	  `(expand-file-name
+	    (org-export-to-file
+	     'org ,outfile ,subtreep ,visible-only nil ',ext-plist)))
+      (org-export-to-file 'org outfile subtreep visible-only nil ext-plist))))
+
 ;;;###autoload
 (defun org-org-publish-to-org (plist filename pub-dir)
   "Publish an org file to org.
@@ -167,6 +259,7 @@ Return output file name."
       (unless visitingp (kill-buffer work-buffer)))
     (set-buffer-modified-p nil)))
 
+
 (provide 'ox-org)
 
 ;; Local variables: