emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Robert Klein <roklein@roklein.de>
To: Peter Davis <pfd@pfdstudio.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: HTML/images zipped?
Date: Mon, 29 Jun 2015 10:47:30 +0200	[thread overview]
Message-ID: <20150629104730.5d15ec42@pckr150.mpip-mainz.mpg.de> (raw)
In-Reply-To: <6sw6c7bng2haym.fsf@pfdstudio.com>

[-- Attachment #1: Type: text/plain, Size: 993 bytes --]

Hello,

please find an experimental htmlzip exporter attached.

Publishing doesn't work, yet (and would, as I began to write it,
export to individual zip files -- one per published org file --
instead of a single zip file).

It is a derived HTML exporter.  I took a lot of stuff from the
corresponding functions in the HTML and LaTeX exporter.

I'm putting an advice on org-html--format-image during "zip" export to
get a list of inline images, so I can put them together with the html
in the zip file.  (Another possibility would be to `rewrite' the
functions org-html-latex-environment, org-html-latex-fragment, and
org-html-link as translate functions for the new exporter (with the
only purpose to run a org-htmlzip--image-list instead of
org-html--image-list).

Best regards
Robert



On Fri, 26 Jun 2015 09:10:25 -0400
Peter Davis <pfd@pfdstudio.com> wrote:

> 
> Is there any way to export HTML with all references images,
> etc. packaged in a ZIP file?
> 
> Thank you.
> 
> -pd
> 
> 


[-- Attachment #2: ox-htmlzip.el --]
[-- Type: text/x-emacs-lisp, Size: 5432 bytes --]

;;; ox-htmlzip.el --- Zipped HTML Back-End for Org Export Engine

;; Copyright (C) 2015 Free Software Foundation, Inc.

;; Author: Robert Klein <roklein at roklein dot de>
;; 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 <http://www.gnu.org/licenses/>.

;;; 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.")

\f
;;; 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 \"<body>\" and \"</body>\" 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

  parent reply	other threads:[~2015-06-29  8:47 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-26 13:10 HTML/images zipped? Peter Davis
2015-06-26 13:15 ` Rasmus
2015-06-26 14:05   ` Peter Davis
2015-06-26 14:24   ` John Kitchin
2015-06-26 14:35     ` Peter Davis
2015-06-26 17:22     ` Peter Davis
2015-06-26 18:09       ` John Kitchin
2015-06-26 19:28         ` Peter Davis
2015-06-26 21:51     ` Nick Dokos
2015-06-26 23:34       ` John Kitchin
2015-06-27  4:52         ` Nick Dokos
2015-06-26 13:44 ` Phillip Lord
2015-06-26 14:08   ` Peter Davis
2015-06-26 14:30     ` John Kitchin
2015-06-29  8:47 ` Robert Klein [this message]
2015-06-29 11:13   ` Rasmus
2015-06-30 20:05     ` Robert Klein
2015-06-30  5:38 ` Vaidheeswaran C

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150629104730.5d15ec42@pckr150.mpip-mainz.mpg.de \
    --to=roklein@roklein.de \
    --cc=emacs-orgmode@gnu.org \
    --cc=pfd@pfdstudio.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).