aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-epub.el
blob: 9c3ad5922318aca92ea55012e88419adef63fb0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
;;; my-epub.el -- epub utils -*- lexical-binding: t -*-

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

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "30.1"))

;; This file is part of dotted.

;; dotted is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; dotted 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 Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with dotted.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; epub utils.

;;; Code:


(defun my-epub-content-file-name (file-name)
  (with-temp-buffer
    (if (eq 0 (call-process "unzip" nil t nil
                            "-p" file-name "META-INF/container.xml"))
        (let ((dom (libxml-parse-xml-region (point-min) (point-max))))
          (dom-attr
           (dom-by-tag
            (dom-by-tag (dom-by-tag dom 'container) 'rootfiles)
            'rootfile)
           'full-path))
      (message "Failed to extract container.xml: %s" (buffer-string))
      nil)))

(defun my-epub-metadata (file-name)
  "Get metadata of an epub file."
  (when-let ((content-file-name (my-epub-content-file-name file-name)))
    (with-temp-buffer
      (call-process "unzip" nil t nil "-p" file-name content-file-name)
      (let* ((dom (libxml-parse-xml-region (point-min) (point-max)))
             (metadata (dom-by-tag dom 'metadata))
             (title (dom-text (dom-by-tag metadata 'title)))
             (authors (dom-texts (dom-by-tag metadata 'creator) ", "))
             (identifier
              (replace-regexp-in-string
               "[^0-9,]" ""
               (dom-texts
                (seq-filter
                 (lambda (node)
                   (or (equal "ISBN" (dom-attr node 'scheme))
                       (string-match-p "^[0-9]+$" (dom-text node))))
                 (dom-by-tag metadata 'identifier))
                ",")))
             (date (replace-regexp-in-string
                    "[^0-9]" ""
                    (dom-text (dom-by-tag metadata 'date))))
             (year (substring date 0 (min 4 (length date)))))
        `((title . ,title)
          (authors . ,authors)
          (year . ,year)
          (identifier . ,identifier))
        ;; (pp metadata)
        ))
    ))

;; 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