[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: read-epub.el
From: |
Bob Newell |
Subject: |
Re: read-epub.el |
Date: |
Tue, 17 Jun 2014 14:41:41 -0700 (PDT) |
User-agent: |
G2/1.0 |
Here's a much improved version, which I now consider a viable alternative to
epubmode.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; begin read-epub.el
;; read-epub
;; Read an epub in Emacs.
;; Requires that Calibre is installed, to provide the command-line
;; utility ebook-convert.
;; Probably only works under Linux. Untested with Windows or Mac or
;; anything else. Tested under Linux Mint 13 and 17, with Emacs 23.3
;; and 24.3.
;; M-x read-epub and give the path to the epub you want to
;; read. Tabbing after entering a partial path / filename will, as
;; usual, bring up a buffer with choices.
;; Inspired by epub-mode, but much simpler because it
;; depends on the Calibre ebook-convert utility to do
;; the hard stuff.
;; There is reasonable error checking, but see the *Messages* buffer
;; if you run into problems.
;; 2014-06-17 Foist upon newsgroup.
;; 2014-06-09 Deal with filenames properly instead of
;; lazily. This means not just tacking .txt
;; on the end of the epub name, and messing
;; with embedded blanks.
;; Do some error checking.
;; Add some documentary comments.
;; Option to reuse an already-converted file.
;; v.0.1.0.
;; 2014-06-08 Bob Newell (address@hidden),
;; Honolulu, Hawai`i.
;; Fast and dirty initial coding. v.0.01.
;; This software is released into the public domain.
(defun read-epub (epub-file)
"Read epub files in emacs"
(interactive "fname of epub: ")
;; I prefer the following to using 'let' because it makes debugging
;; easier.
(defvar epub-version "0.10")
(defvar epub-filebase)
(defvar epub-extension)
(defvar epub-oldname)
(defvar epub-newname)
(setq epub-filebase (file-name-sans-extension epub-file))
(setq epub-extension (file-name-extension epub-file))
(setq epub-oldname epub-file)
(setq epub-newname (concat epub-filebase ".txt"))
(if (not (file-exists-p epub-oldname))
(error (concat epub-file " not found")))
(if (not (string= "epub" epub-extension))
(error (concat epub-file " does not have an epub extension")))
(if (= 1 (shell-command "which ebook-convert"))
(error "Can't find the ebook-convert utility"))
;; If there is no existing text file with the 'conversion' name, or if
;; there is one and we don't want to reuse it, we do the
;; conversion. Otherwise we reuse the existing text file with no
;; further checking.
(if (or (not (file-exists-p epub-newname))
(not (yes-or-no-p (concat "Reuse existing " epub-newname "?"))))
(progn
(message "Converting %s, this can take a while" epub-file)
;; shell-quote-argument deals with embedded blanks in the filenames.
;; expand-file-name is needed because ebook-convert expands the tilde
;; incorrectly.
(shell-command
(concat "ebook-convert "
(shell-quote-argument (expand-file-name epub-oldname))
" "
(shell-quote-argument (expand-file-name epub-newname))))
(find-file epub-newname)
;; Comment out the next three lines if you don't want to fill
;; paragraphs. This might be the case if things like embedded tables
;; get rendered in a way that paragraph fill will mangle them. This
;; shouldn't be an issue for novels and most texts that are reasonable
;; to read in an emacs buffer.
(message "Realigning text, this may take some time")
(mark-whole-buffer)
(fill-paragraph nil 1)
(save-buffer)
)
)
;; Now open the output file (even though it may already be open), make
;; a single window, and go to the top of the file.
(find-file epub-newname)
(delete-other-windows)
(goto-char (point-min))
;; Put in view mode for easier reading.
(view-mode)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; end read-epub.el
- read-epub.el, Bob Newell, 2014/06/08
- Re: read-epub.el,
Bob Newell <=