")
+
+ s = re.sub(r"\]\s*", "] ", s, flags=re.DOTALL)
+
+ s = s.replace("\n", "")
+ # multiple spaces
+ s = re.sub(r" +", r" ", s)
+ return s
+
+
+def convert_chapter(s: str) -> str:
+ global counter_chapter
+ counter_chapter += 1
+ # chapter class is used in calibre to detect chapters
+ out = f'
{counter_chapter}. {s}
'
+ return out
+
+
+def convert_parsel(s: str) -> str:
+ s = s.replace("ss", "ß").replace("s", "ss").replace("ß", "sss")
+ out = f'{s}'
+ return out
+
+
+# def convert_footnotes(s: str, authorsnote: bool = False) -> str:
+#
+# epub:type="noteref" only works for EPUB version 3.
+# at https://manual.calibre-ebook.com/generated/en/ebook-convert.html#epub-output-options
+# it is suggested
+# "EPUB 2 is the most widely compatible, only use EPUB 3 if you know you actually need it."
+# so I decided to uses inline author comments instead
+#
+# # \authorsnotetext{I do this in my own home.}
+# # ->
+# # 1
+# #
+# # if authorsnote = False ->
I do this in my own home.
+# global counter_footnotes
+# counter_footnotes += 1
+# s_authorsnote = ""
+# if authorsnote:
+# s_authorsnote = "Author’s note: "
+# out = f""" {counter_footnotes}
+# """
+# return out
+
+
+def find_tex_commands(s: str) -> list:
+ l = []
+
+ myMatches = re.findall(r"\\[a-zA-Z0-9]+", s)
+ for myMatch in myMatches:
+ l.append(str(myMatch))
+
+ return l
+
+
+fhAll = open(f"{dir_out}/hpmor.html", mode="w", encoding="utf-8", newline="\n")
+fhAll.write(html_start)
+fhAll.write(html_preamble)
+
+
+l_tex_commands_unhandled = []
+
+for fileIn in sorted(glob.glob(f"{dir_tex_source}/hpmor-chapter-*.tex")):
+ # for fileIn in sorted(glob.glob(f"../chapters/hpmor-chapter-100.tex")):
+ (filePath, fileName) = os.path.split(fileIn)
+ (fileBaseName, fileExtension) = os.path.splitext(fileName)
+ fileOut = f"{dir_tmp}/{fileBaseName}.html"
+ with open(fileIn, mode="r", encoding="utf-8", newline="\n") as fh:
+ cont = fh.read()
+ cont = simplify_tex(cont)
+ cont = tex2html(cont)
+
+ l = find_tex_commands(cont)
+ l_tex_commands_unhandled.extend(l)
+ if len(l) > 0:
+ print(
+ f"WARN: there are leftover LaTeX commands in file {fileOut}:\n"
+ + ", ".join(l)
+ )
+
+ with open(fileOut, mode="w", encoding="utf-8", newline="\n") as fh:
+ fh.write(html_start + cont + html_end)
+
+ if fileBaseName == "hpmor-chapter-001":
+ cont = (
+ "
Book 1: Harry James Potter-Evans-Verres and the Methods of Rationality
Book 6: Harry James Potter-Evans-Verres and the Philosopher's Stone
\n"
+ + cont
+ )
+
+ fhAll.write(cont)
+
+fhAll.write(html_end)
+fhAll.close()
+
+d_tex_commands_unhandled = {}
+for item in l_tex_commands_unhandled:
+ if item in d_tex_commands_unhandled:
+ d_tex_commands_unhandled[item] += 1
+ else:
+ d_tex_commands_unhandled[item] = 1
+# sort values reversed
+for key, value in sorted(
+ d_tex_commands_unhandled.items(), key=lambda item: item[1], reverse=True
+):
+ print(f"{value}\t{key}")
+
+assert (
+ len(d_tex_commands_unhandled) == 0
+), "Error: unhandled LaTeX commands found, see above"
diff --git a/ebook/2_html2epub.sh b/ebook/2_html2epub.sh
new file mode 100755
index 000000000..50ac7a1ec
--- /dev/null
+++ b/ebook/2_html2epub.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+# by Torben Menke https://entorb.net
+
+# run from within ebook dir via
+# ./2_html2epub.sh
+
+mkdir -p tmp
+
+echo 1. extract titlepage from PDF
+# there are 2 possible sources: fetch PDF from web or alternatively use locally generated file
+# 1.1a download PDF from web
+# TODO: ensure to use latest version of PDF
+if [ ! -f tmp/hpmor.pdf ] ; then
+ echo 1.1 obtain PDF file
+ wget -O tmp/hpmor.pdf https://github.com/rjl20/hpmor/releases/download/v1.1.3/hpmor-1.1.3.pdf
+fi
+
+# 1.1b use locally generated file
+# cp ../hpmor.pdf tmp/
+
+# 1.2 extract title page from PDF and convert to jpeg
+# 1.2a via imagemagick
+# sudo apt install imagemagick
+# convert -density 150 tmp/hpmor.pdf[0] -quality 75 tmp/title-en.jpg
+# image magick complains:
+# attempt to perform an operation not allowed by the security policy
+
+# 1.2b let's use ghostscript instead
+gs -dSAFER -r600 -sDEVICE=pngalpha -dFirstPage=1 -dLastPage=1 -o tmp/title-en.png tmp/hpmor.pdf
+# now imagemagick can be used for converting to the proper size
+convert -density 150 tmp/title-en.png -resize 1186x1186\> -quality 75 tmp/title-en.jpg
+
+echo 2. convert html to epub
+# use calibre instead of pandoc, as pandoc loses the css style
+# see https://manual.calibre-ebook.com/generated/en/ebook-convert.html
+# linux: sudo apt install calibre
+# windows: obtain from https://calibre-ebook.com/download_windows
+echo 2.1 calibre: html to epub
+ebook-convert output/hpmor.html output/hpmor.epub --no-default-epub-cover --cover tmp/title-en.jpg --authors "Eliezer Yudkowsky" --title "Harry Potter and the Methods of Rationality" --book-producer "Torben Menke" --pubdate 2015-03-14 --language en-US
+
+echo 2.2 calibre: epub to mobi
+ebook-convert output/hpmor.epub output/hpmor.mobi
From 315fa3bbf7e0b0d63e47901803d5ef5b0e9d2cf5 Mon Sep 17 00:00:00 2001
From: Reuben Thomas
Date: Tue, 14 Dec 2021 20:56:15 +0000
Subject: [PATCH 2/3] GNUmakefile: add ebooks
---
GNUmakefile | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/GNUmakefile b/GNUmakefile
index 6a53603d6..278a36e43 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -4,19 +4,27 @@ PROJECT=hpmor
TAG := $(shell git describe --tags)
VERSION := $(shell echo $(TAG) | sed -e 's/^v//')
+EBOOKS = ebook/output/$(PROJECT).epub ebook/output/$(PROJECT).mobi
ZIPFILE = $(PROJECT)-$(VERSION).zip
-all:
+all: ebooks pdf
+
+pdf:
latexmk
-zip:
- latexmk -g && \
+ebooks: pdf
+ cd ebook && ./1_latex2html.py && ./2_html2epub.sh
+
+zip: pdf ebooks
rm -f $(ZIPFILE) && \
- zip $(ZIPFILE) *.pdf
+ zip $(ZIPFILE) *.pdf $(EBOOKS)
# To make a release: git tag vx.y && git push --tags && make release
# Needs woger from https://github.com/rrthomas/woger/
release: zip
git diff --exit-code && \
- woger github package=$(PROJECT) version=$(VERSION) dist_type=zip
- hub release edit $(TAG) --attach $(PROJECT).pdf#$(PROJECT)-$(VERSION).pdf
+ woger github package=$(PROJECT) version=$(VERSION) dist_type=zip && \
+ for file in $(PROJECT).pdf $(EBOOKS); do \
+ suffix=$${file##*.}; \
+ hub release edit $(TAG) --attach $$file#$(PROJECT)-$(VERSION).$$suffix; \
+ done
From 2a74f791d6884587f7eb2ba636eae42c87237313 Mon Sep 17 00:00:00 2001
From: Reuben Thomas
Date: Tue, 14 Dec 2021 20:56:23 +0000
Subject: [PATCH 3/3] ebook/2_html2epub.sh: use locally-generated file
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
It’s less confusing to generate ebook from local sources. Since the ebooks
are released, it’s not necessary to be able to make them without building
the LaTeX version.
---
ebook/2_html2epub.sh | 15 +++------------
1 file changed, 3 insertions(+), 12 deletions(-)
diff --git a/ebook/2_html2epub.sh b/ebook/2_html2epub.sh
index 50ac7a1ec..cdb710280 100755
--- a/ebook/2_html2epub.sh
+++ b/ebook/2_html2epub.sh
@@ -8,25 +8,16 @@
mkdir -p tmp
echo 1. extract titlepage from PDF
-# there are 2 possible sources: fetch PDF from web or alternatively use locally generated file
-# 1.1a download PDF from web
-# TODO: ensure to use latest version of PDF
-if [ ! -f tmp/hpmor.pdf ] ; then
- echo 1.1 obtain PDF file
- wget -O tmp/hpmor.pdf https://github.com/rjl20/hpmor/releases/download/v1.1.3/hpmor-1.1.3.pdf
-fi
-
-# 1.1b use locally generated file
-# cp ../hpmor.pdf tmp/
+cp ../hpmor.pdf tmp/
# 1.2 extract title page from PDF and convert to jpeg
# 1.2a via imagemagick
# sudo apt install imagemagick
# convert -density 150 tmp/hpmor.pdf[0] -quality 75 tmp/title-en.jpg
-# image magick complains:
+# imagemagick complains:
# attempt to perform an operation not allowed by the security policy
-# 1.2b let's use ghostscript instead
+# 1.2b via ghostscript
gs -dSAFER -r600 -sDEVICE=pngalpha -dFirstPage=1 -dLastPage=1 -o tmp/title-en.png tmp/hpmor.pdf
# now imagemagick can be used for converting to the proper size
convert -density 150 tmp/title-en.png -resize 1186x1186\> -quality 75 tmp/title-en.jpg