;;; ox-htmlzip.el --- Zipped HTML Back-End for Org Export Engine
;; Copyright (C) 2015 Free Software Foundation, Inc.
;; Author: Robert Klein
;; Keywords: HTML, zip
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see .
;;; Commentary:
;; This library implements an zip-addon for the HTML back-end for the
;; Org generic exporter.
;;; Code:
;;; Dependencies
(require 'ox)
(require 'ox-html) ;; in turn requires ox-publish and ox.
;;; Define Derived Back-End
(org-export-define-derived-backend 'htmlzip 'html
:menu-entry
'(?h "Export to HTML"
((?z "To zipped file" org-htmlzip-export-to-htmlzip)
))
:options-alist
'()
:translate-alist
'())
;;; Internal variables
(defvar org-htmlzip--image-list nil
"Images to upload/change URL in buffer.")
;;; Internal Functions
(defun org-htmlzip--add-image (source attributes info)
"Add image file name org-htmlzip--image-list.
Used as an advice for org-html--image-format during htmlzip
export."
(add-to-list 'org-htmlzip--image-list source))
;;; helper functions for export
;;; End-user functions
;;;###autoload
(defun org-htmlzip-export-to-htmlzip
(&optional async subtreep visible-only body-only ext-plist)
"Export current buffer to a HTML 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.
When optional argument BODY-ONLY is non-nil, only write code
between \"\" and \"\" tags.
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's name."
(interactive)
(advice-add 'org-html--format-image :before #'org-htmlzip--add-image)
(setq org-htmlzip--image-list nil)
(let* ((extension (concat "." (or (plist-get ext-plist :html-extension)
org-html-extension
"html")))
(file (org-export-output-file-name extension subtreep))
(org-export-coding-system org-html-coding-system)
(zipfile (org-export-to-file 'html file
async subtreep visible-only body-only ext-plist
(lambda (file) (org-htmlzip--zip file)))))
(setq org-htmlzip--image-list nil)
(advice-remove 'org-html--format-image #'org-htmlzip--add-image)
zipfile))
(defun org-htmlzip--zip (htmlfile)
" Zip a HTML file.
HTMLFILE is the name of the exported HTML file being zipped.
Inline image file names are taken from the global variable
or-html--image-list.
Return ZIP file name."
(let* ((base-name (file-name-sans-extension (file-name-nondirectory htmlfile)))
(full-name (file-truename htmlfile))
(out-dir (file-name-directory htmlfile))
(zip-file (concat base-name ".zip"))
;; Properly set working directory for compilation.
(default-directory (if (file-name-absolute-p htmlfile)
(file-name-directory full-name)
default-directory)))
;; delete zip file if it exists
(when (file-exists-p zip-file)
(delete-file zip-file))
;; zip newly exported files
(shell-command (concat "zip "
(shell-quote-argument zip-file) " "
(shell-quote-argument htmlfile) " "
(mapconcat 'shell-quote-argument org-htmlzip--image-list
" ")))
zip-file))
;;;###autoload
(defun org-htmlzip-publish-to-htmlzip (plist filename pub-dir)
"Publish an org file to ZIP (via HTML).
FILENAME is the filename of the Org file to be published. PLIST
is the property list for the given project. PUB-DIR is the
publishing directory.
Return output file name."
;; Unlike to `org-html-publish-to-html', PDF file is generated
;; in working directory and then moved to publishing directory.
(advice-add 'org-html--format-image :before #'org-htmlzip--add-image)
(setq org-htmlzip--image-list nil)
(let ((outfile (org-publish-attachment
plist
(org-htmlzip--zip
(org-publish-org-to
'htmlzip filename ".html" plist (file-name-directory filename)))
pub-dir)))
(setq org-htmlzip--image-list nil)
(advice-remove 'org-html--format-image #'org-htmlzip--add-image)
outfile))
(provide 'ox-htmlzip)
;; Local variables:
;; generated-autoload-file: "org-loaddefs.el"
;; End:
;;; ox-htmlzip.el ends here