Browse Source

Publishing: Allow to publish the Org source file

There is now a new export function, `org-export-as-org', which
basically creates a copy of the Org file with things like archived
trees, commented trees, and trees deselected by export tags,
stripped.
Carsten Dominik 16 years ago
parent
commit
2e9202362f
6 changed files with 134 additions and 3 deletions
  1. 6 0
      doc/ChangeLog
  2. 16 3
      doc/org.texi
  3. 7 0
      lisp/ChangeLog
  4. 86 0
      lisp/org-exp.el
  5. 14 0
      lisp/org-html.el
  6. 5 0
      lisp/org-publish.el

+ 6 - 0
doc/ChangeLog

@@ -1,3 +1,9 @@
+2009-05-13  Carsten Dominik  <carsten.dominik@gmail.com>
+
+	* org.texi (Publishing action): Mention the new publishing
+	function, to publish an Org source file.
+	(Publishing links): Mention how to link to an Org source file.
+
 2009-05-11  Carsten Dominik  <carsten.dominik@gmail.com>
 
 	* org.texi (Macro replacement): Document new macros.

+ 16 - 3
doc/org.texi

@@ -9279,8 +9279,14 @@ possibly transformed in the process.  The default transformation is to export
 Org files as HTML files, and this is done by the function
 @code{org-publish-org-to-html} which calls the HTML exporter (@pxref{HTML
 export}).  But you also can publish your as PDF files using
-@code{org-publish-org-to-pdf}.  Other files like images only need to be
-copied to the publishing destination, for this you may use
+@code{org-publish-org-to-pdf}.  If you want to publish the Org file itself,
+but with @i{archived}, @i{commented}, and @i{tag-excluded} trees removed, use
+@code{org-publish-org-to-org} and set the parameters @code{:plain-source}
+and/or @code{:htmlized-source}.  This will produce @file{file.org} and
+@file{file.org.html} in the publishing
+directory@footnote{@file{file-source.org} and @file{file-source.org.html} if
+source and publishing directories are equal.}.  Other files like images only
+need to be copied to the publishing destination, for this you may use
 @code{org-publish-attachment}.  For non-Org files, you always need to provide
 specify the publishing function:
 
@@ -9288,6 +9294,10 @@ specify the publishing function:
 @item @code{:publishing-function}
 @tab Function executing the publication of a file.  This may also be a
 list of functions, which will all be called in turn.
+@item @code{:plain-source}
+@tab Non-nil means, publish plain source.
+@item @code{:htmlized-source}
+@tab Non-nil means, publish htmlized source.
 @end multitable
 
 The function must accept two arguments: a property list containing at least a
@@ -9420,7 +9430,10 @@ something like @samp{[[file:foo.org][The foo]]} or simply
 @samp{file:foo.org.} (@pxref{Hyperlinks}).  Upon publishing this link
 becomes a link to @file{foo.html}.  In this way, you can interlink the
 pages of your "org web" project and the links will work as expected when
-you publish them to HTML.
+you publish them to HTML.  If you also publish the Org source file and want
+to link to that, use an @code{http:} link instead of an @code{file:} link,
+because @code{file:} links are converted to link to the corresponding
+@file{html} file. 
 
 You may also link to related files, such as images. Provided you are careful
 with relative pathnames, and provided you have also configured Org to upload

+ 7 - 0
lisp/ChangeLog

@@ -1,3 +1,10 @@
+2009-05-13  Carsten Dominik  <carsten.dominik@gmail.com>
+
+	* org-exp.el (org-export-as-org): New command.
+	(org-export-as-org): New command.
+
+	* org-publish.el (org-publish-org-to-org): New function.
+
 2009-05-12  Carsten Dominik  <carsten.dominik@gmail.com>
 
 	* org.el (org-yank): Just call `org-yank-generic'.

+ 86 - 0
lisp/org-exp.el

@@ -2396,6 +2396,92 @@ command."
 		(not (get-char-property s 'invisible))))
     s))
 
+;;;###autoload
+(defun org-export-as-org (arg &optional hidden ext-plist
+			      to-buffer body-only pub-dir)
+  "Make a copy wiht not-exporting stuff removed.
+The purpose of this function is to provide a way to export the source
+Org file of a webpage in Org format, but with sensitive and/or irrelevant
+stuff removed.  This command will remove the following:
+
+- archived trees (if the variable `org-export-with-archived-trees' is nil)
+- comment blocks and trees starting with the COMMENT keyword
+- only trees that are consistent with `org-export-select-tags'
+  and `org-export-exclude-tags'.
+
+The only arguments that will be used are EXT-PLIST and PUB-DIR,
+all the others will be ignored (but are present so that the general
+mechanism to call publishing functions will work).
+
+EXT-PLIST is a property list with external parameters overriding
+org-mode's default settings, but still inferior to file-local
+settings.  When PUB-DIR is set, use this as the publishing
+directory."
+  (interactive "P")
+  (let* ((opt-plist (org-combine-plists (org-default-export-plist)
+					ext-plist
+					(org-infile-export-plist)))
+	 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
+	 (filename (concat (file-name-as-directory
+			    (or pub-dir
+				(org-export-directory :org opt-plist)))
+			   (file-name-sans-extension
+			    (file-name-nondirectory bfname))
+			     ".org"))
+	 (filename (and filename
+			(if (equal (file-truename filename)
+				   (file-truename bfname))
+			    (concat filename "-source")
+			  filename)))
+	 (backup-inhibited t)
+	 (buffer (find-file-noselect filename))
+	 (region (buffer-string)))
+    (save-excursion
+      (switch-to-buffer buffer)
+      (erase-buffer)
+      (insert region)
+      (let ((org-inhibit-startup t)) (org-mode))
+      
+      ;; Get rid of archived trees
+      (org-export-remove-archived-trees (plist-get opt-plist :archived-trees))
+      
+      ;; Remove comment environment and comment subtrees
+      (org-export-remove-comment-blocks-and-subtrees)
+      
+      ;; Get rid of excluded trees
+      (org-export-handle-export-tags (plist-get opt-plist :select-tags)
+				     (plist-get opt-plist :exclude-tags))
+      
+      (when (or (plist-get opt-plist :plain-source)
+		(not (or (plist-get opt-plist :plain-source)
+			 (plist-get opt-plist :htmlized-source))))
+	;; Either nothing special is requested (default call)
+	;; or the plain source is explicitly requested
+	;; so: save it
+	(save-buffer))
+      (when (plist-get opt-plist :htmlized-source)
+	;; Make the htmlized version
+	(require 'htmlize)
+	(require 'org-html)
+	(font-lock-fontify-buffer)
+	(let* ((htmlize-output-type 'css)
+	       (newbuf (htmlize-buffer)))
+	  (with-current-buffer newbuf
+	    (when org-export-htmlized-org-css-url
+	      (goto-char (point-min))
+	      (and (re-search-forward
+		    "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*"
+		    nil t)
+		   (replace-match
+		    (format
+		     "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
+		     org-export-htmlized-org-css-url)
+		    t t)))
+	    (write-file (concat filename ".html")))
+	  (kill-buffer newbuf)))
+      (set-buffer-modified-p nil)
+      (kill-buffer (current-buffer)))))
+
 (defvar org-archive-location)  ;; gets loaded with the org-archive require.
 (defun org-get-current-options ()
   "Return a string with current options as keyword options.

+ 14 - 0
lisp/org-html.el

@@ -324,6 +324,20 @@ in all modes you want.  Then, use the command
   :group 'org-export-htmlize
   :type 'string)
 
+(defcustom org-export-htmlized-org-css-url nil
+  "URL pointing to a CSS file defining text colors for htmlized Emacs buffers.
+Normally when creating an htmlized version of an Org buffer, htmlize will
+create CSS to define the font colors.  However, this does not work when
+converting in batch mode, and it also can look bad if different people
+with different fontification setup work on the same website.
+When this variable is non-nil, creating an htmlized version of an Org buffer
+using `org-export-as-org' will remove the internal CSS section and replace it
+with a link to this URL."
+  :group
+  :type '(choice
+	  (const :tag "Keep internal css" nil)
+	  (string :tag "URL or local href")))
+
 ;;; Variables, constants, and parameter plists
 
 (defvar org-export-html-preamble nil

+ 5 - 0
lisp/org-publish.el

@@ -430,6 +430,11 @@ See `org-publish-org-to' to the list of arguments."
 See `org-publish-org-to' to the list of arguments."
   (org-publish-org-to "html" plist filename pub-dir))
 
+(defun org-publish-org-to-org (plist filename pub-dir)
+  "Publish an org file to HTML.
+See `org-publish-org-to' to the list of arguments."
+  (org-publish-org-to "org" plist filename pub-dir))
+
 (defun org-publish-attachment (plist filename pub-dir)
   "Publish a file with no transformation of any kind.
 See `org-publish-org-to' to the list of arguments."