aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-epub.el
diff options
context:
space:
mode:
Diffstat (limited to 'emacs/.emacs.d/lisp/my/my-epub.el')
-rw-r--r--emacs/.emacs.d/lisp/my/my-epub.el101
1 files changed, 101 insertions, 0 deletions
diff --git a/emacs/.emacs.d/lisp/my/my-epub.el b/emacs/.emacs.d/lisp/my/my-epub.el
index 4a3dfca..9c3ad59 100644
--- a/emacs/.emacs.d/lisp/my/my-epub.el
+++ b/emacs/.emacs.d/lisp/my/my-epub.el
@@ -71,5 +71,106 @@
))
))
+;; generate epub
+(defun my-epub-create (dir title author)
+ "Create an epub by concatenating htmls in DIR."
+ (let* ((name
+ (file-name-nondirectory (directory-file-name dir)))
+ (tmpdir
+ (make-temp-file (format "/tmp/%s.epub." name) t))
+ (files (directory-files dir nil directory-files-no-dot-files-regexp)))
+ (my-epub--create-mimetype tmpdir)
+ (my-epub--create-container tmpdir)
+ (my-epub--create-opf tmpdir files title author)
+ (my-epub--create-toc tmpdir files title)
+ (my-epub--add-html
+ tmpdir
+ (directory-files dir t directory-files-no-dot-files-regexp))
+ (my-epub--zip tmpdir name)
+ ))
+
+(defun my-epub--create-mimetype (dir)
+ (with-temp-buffer
+ (insert "application/epub+zip")
+ (write-file (file-name-concat dir "mimetype"))))
+
+(defun my-epub--create-container (dir)
+ (with-temp-buffer
+ (insert
+ "<?xml version=\"1.0\"?>
+<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">
+ <rootfiles>
+ <rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/>
+ </rootfiles>
+</container>")
+ (make-directory
+ (file-name-concat dir "META-INF"))
+ (write-file (file-name-concat dir "META-INF/container.xml"))))
+
+(defun my-epub--create-opf (dir files title author)
+ (with-temp-buffer
+ (insert
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "\n")
+ (dom-print
+ `(package
+ ((unique-identifier . "BookID")
+ (version . "2.0"))
+ (metadata nil
+ (title nil ,title)
+ (creator nil ,author)
+ (language nil "en")
+ (identifier
+ ((id . "BookID"))
+ "Hello ID"))
+ (manifest
+ nil
+ (item ((id . "ncx")
+ (href . "toc.ncx")
+ (media-type . "application/x-dtbncx+xml")))
+ ,@(seq-map
+ (lambda (file)
+ `(item ((id . ,file)
+ (href . ,file)
+ (media-type . "application/xhtml+xml"))))
+ files))
+ (spine
+ ((toc . "ncx"))
+ ,@(seq-map
+ (lambda (file)
+ `(itemref ((idref . ,file))))
+ files)))
+ t t)
+ (make-directory
+ (file-name-concat dir "OEBPS"))
+ (write-file (file-name-concat dir "OEBPS/content.opf"))
+ (message (buffer-string))))
+
+(defun my-epub--create-toc (dir files title)
+ (with-temp-buffer
+ (insert "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "\n")
+ (dom-print
+ `(ncx nil
+ (docTitle nil
+ (text nil ,title))
+ (navMap
+ nil
+ ,@(seq-map
+ (lambda (file)
+ `(navPoint nil
+ (content ((src . ,file)))))
+ files)))
+ t t)
+ (write-file (file-name-concat dir "OEBPS/toc.ncx"))))
+
+(defun my-epub--add-html (dir files)
+ (seq-do
+ (lambda (file)
+ (copy-file file (file-name-concat dir "OEBPS/")))
+ files))
+
+(defun my-epub--zip (dir name)
+ (let ((default-directory dir))
+ (call-process "zip" nil nil nil "-r" (format "/tmp/%s.epub" name) ".")))
+
(provide 'my-epub)
;;; my-epub.el ends here