Skip to content

Instantly share code, notes, and snippets.

@maticrivo
Forked from bmaupin/epub.css
Created January 10, 2018 08:30
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save maticrivo/8a2b2845df5081232814b91360ded097 to your computer and use it in GitHub Desktop.
Save maticrivo/8a2b2845df5081232814b91360ded097 to your computer and use it in GitHub Desktop.
You Don't Know JS Ebooks
body {
text-align: justify;
}
code, pre {
font-family: "Hack", monospace;
}
h1, h2, h3, h4, h5, h6 {
text-align: left;
}
pre {
/* Don't make it too dark or it will be harder to read on e-ink devices */
background-color: #f5f5f5;
border-radius: 3px;
font-size: 0.85em;
padding: 10px;
text-align: left;
/* Wrap the text of pre sections */
white-space: pre-wrap;
}
@font-face {
font-family: "Hack";
src: url(Hack-Regular.ttf) format("truetype");
}
# make sure brew is installed, if not please install it first from https://brew.sh
if [ -z "$(which brew)" ]; then
echo 'brew not found.'
echo 'make sure it is installed and configured correctly in your PATH.'
exit 1;
fi
brew install gnu-sed pandoc wget
brew cask install font-hack
git clone https://github.com/getify/Functional-Light-JS.git
wget -O Functional-Light-JS/epub.css https://gist.githubusercontent.com/maticrivo/8a2b2845df5081232814b91360ded097/raw/86b62ad3843fa038111021abe2cd16335297eb7f/epub.css
cd Functional-Light-JS
# Clean up redundant headings that end up getting split into separate chapters by themselves
find . -iname "*.md" -exec sed -i '/# Functional-Light JavaScript/d' {} \;
# Fix <br> tags which pandoc won't convert to <br />
find . -iname "*.md" -exec sed -i 's#<br>#<br />#' {} \;
# Close img tags
find . -iname "*.md" -exec sed -i -r 's#(<img.*[^/])>#\1 />#' {} \;
cd manuscript
pandoc -f markdown+smart -o ../"Functional-Light JavaScript.epub" \
--epub-cover-image=images/marketing/front-cover-hd.png \
--epub-embed-font=$HOME/Library/Fonts/Hack-Regular.ttf \
--css=../epub.css \
-M author="Kyle Simpson" \
-M title="Functional-Light JavaScript" \
foreword.md \
preface.md \
ch1.md \
ch2.md \
ch3.md \
ch4.md \
ch5.md \
ch6.md \
ch7.md \
ch8.md \
ch9.md \
ch10.md \
ch11.md \
apA.md \
apB.md \
apC.md
cd ..
# make sure brew is installed, if not please install it first from https://brew.sh
if [ -z "$(which brew)" ]; then
echo 'brew not found.'
echo 'make sure it is installed and configured correctly in your PATH.'
exit 1;
fi
brew install gnu-sed pandoc wget
brew cask install font-hack
git clone https://github.com/getify/You-Dont-Know-JS.git
wget -O You-Dont-Know-JS/epub.css https://gist.githubusercontent.com/maticrivo/8a2b2845df5081232814b91360ded097/raw/86b62ad3843fa038111021abe2cd16335297eb7f/epub.css
cd You-Dont-Know-JS
# Clean up redundant headings that end up getting split into separate chapters by themselves
find . -iname "*.md" -exec sed -i '/# You Don'\''t Know JS.*/d' {} \;
# Fix <br> tags which pandoc won't convert to <br />
find . -iname "*.md" -exec sed -i 's#<br>#<br />#' {} \;
# Close img tags
find . -iname "*.md" -exec sed -i -r 's#(<img.*[^/])>#\1 />#' {} \;
cd up\ \&\ going
# --no-highlight is used because syntax highlighting can make reading more difficult on e-ink devices
pandoc -f markdown+smart -o ../"You Don't Know JS: Up & Going.epub" \
--epub-cover-image=cover.jpg \
--epub-embed-font=$HOME/Library/Fonts/Hack-Regular.ttf \
--css=../epub.css \
-M author="Kyle Simpson" \
-M title="You Don't Know JS: Up & Going" \
foreword.md \
../preface.md \
ch1.md \
ch2.md \
ch3.md \
apA.md
cd ..
cd scope\ \&\ closures
wget -O foreword.html https://shanehudson.net/2014/06/03/foreword-dont-know-js/
pandoc -f markdown+smart -o foreword.md foreword.html
pandoc -f markdown+smart -o ../"You Don't Know JS: Scope & Closures.epub" \
--epub-cover-image=cover.jpg \
--epub-embed-font=$HOME/Library/Fonts/Hack-Regular.ttf \
--css=../epub.css \
-M author="Kyle Simpson" \
-M title="You Don't Know JS: Scope & Closures" \
foreword.md \
../preface.md \
ch1.md \
ch2.md \
ch3.md \
ch4.md \
ch5.md \
apA.md \
apB.md \
apC.md \
apD.md
cd ..
cd this\ \&\ object\ prototypes
pandoc -f markdown+smart -o ../"You Don't Know JS: this & Object Prototypes.epub" \
--epub-cover-image=cover.jpg \
--epub-embed-font=$HOME/Library/Fonts/Hack-Regular.ttf \
--css=../epub.css \
-M author="Kyle Simpson" \
-M title="You Don't Know JS: this & Object Prototypes" \
foreword.md \
../preface.md \
ch1.md \
ch2.md \
ch3.md \
ch4.md \
ch5.md \
ch6.md \
apA.md \
apB.md
cd ..
cd types\ \&\ grammar
pandoc -f markdown+smart -o ../"You Don't Know JS: Types & Grammar.epub" \
--epub-cover-image=cover.jpg \
--epub-embed-font=$HOME/Library/Fonts/Hack-Regular.ttf \
--css=../epub.css \
-M author="Kyle Simpson" \
-M title="You Don't Know JS: Types & Grammar" \
foreword.md \
../preface.md \
ch1.md \
ch2.md \
ch3.md \
ch4.md \
ch5.md \
apA.md \
apB.md
cd ..
cd async\ \&\ performance
pandoc -f markdown+smart -o ../"You Don't Know JS: Async & Performance.epub" \
--epub-cover-image=cover.jpg \
--epub-embed-font=$HOME/Library/Fonts/Hack-Regular.ttf \
--css=../epub.css \
-M author="Kyle Simpson" \
-M title="You Don't Know JS: Async & Performance" \
foreword.md \
../preface.md \
ch1.md \
ch2.md \
ch3.md \
ch4.md \
ch5.md \
ch6.md \
apA.md \
apB.md \
apC.md
cd ..
cd es6\ \&\ beyond
pandoc -f markdown+smart -o ../"You Don't Know JS: ES6 & Beyond.epub" \
--epub-cover-image=cover.jpg \
--epub-embed-font=$HOME/Library/Fonts/Hack-Regular.ttf \
--css=../epub.css \
-M author="Kyle Simpson" \
-M title="You Don't Know JS: ES6 & Beyond" \
foreword.md \
../preface.md \
ch1.md \
ch2.md \
ch3.md \
ch4.md \
ch5.md \
ch6.md \
ch7.md \
ch8.md \
apA.md
cd ..
@gidj
Copy link

gidj commented Jan 24, 2018

In order for me to get it working correctly, I had to change the options that get passed to sed in lines 16, 18, and 20. I added '-E' to enable 'extended' regular expressions, gave an empty argument to the '-'i option, and added '-e' (the last two due the an explanation here: https://stackoverflow.com/questions/19456518/invalid-command-code-despite-escaping-periods-using-sed)

Taken together, these worked for me:
find . -iname "*.md" -exec sed -E -i '' -e '/# You Don'\''t Know JS.*/d' {} \;
find . -iname "*.md" -exec sed -E -i '' -e 's#<br>#<br />#' {} \;
find . -iname "*.md" -exec sed -E -i -r 's#(<img.*[^/])>#\1 />#' {} \;

@nmaxcom
Copy link

nmaxcom commented Jan 30, 2018

Hi, thanks for the effort. I had a few issues. For example, with the functional programming book, I had to modify the brew part, otherwise it would fatal error. So that part ended like this:

[...]
brew install gnu-sed pandoc wget

# Following two lines changed as per https://github.com/source-foundry/Hack/issues/230#issuecomment-266764801
brew tap caskroom/fonts
brew cask install font-hack

git clone https://github.com/getify/Functional-Light-JS.git
[...]

Then the script could kept going, but:

[...]

Functional-Light-JS/epub.css     100%[========================================================>]     475  --.-KB/s    in 0s

2018-01-30 02:37:01 (26.6 MB/s) - 'Functional-Light-JS/epub.css' saved [475/475]

sed: 1: "./CONTRIBUTING.md": invalid command code .
sed: 1: "./ISSUE_TEMPLATE.md": invalid command code .
sed: 1: "./manuscript/apA.md": invalid command code .
sed: 1: "./manuscript/apB.md": invalid command code .
sed: 1: "./manuscript/apC.md": invalid command code .
sed: 1: "./manuscript/ch1.md": invalid command code .
sed: 1: "./manuscript/ch10.md": invalid command code .
sed: 1: "./manuscript/ch11.md": invalid command code .
sed: 1: "./manuscript/ch2.md": invalid command code .
sed: 1: "./manuscript/ch3.md": invalid command code .
sed: 1: "./manuscript/ch4.md": invalid command code .
sed: 1: "./manuscript/ch5.md": invalid command code .
sed: 1: "./manuscript/ch6.md": invalid command code .
sed: 1: "./manuscript/ch7.md": invalid command code .
sed: 1: "./manuscript/ch8.md": invalid command code .
sed: 1: "./manuscript/ch9.md": invalid command code .
sed: 1: "./manuscript/foreword.md": invalid command code .
sed: 1: "./manuscript/preface.md": invalid command code .
sed: 1: "./manuscript/README.md": invalid command code .
sed: 1: "./PULL_REQUEST_TEMPLATE.md": invalid command code .
sed: 1: "./README.md": invalid command code .
sed: 1: "./CONTRIBUTING.md": invalid command code .
sed: 1: "./ISSUE_TEMPLATE.md": invalid command code .
sed: 1: "./manuscript/apA.md": invalid command code .
sed: 1: "./manuscript/apB.md": invalid command code .
sed: 1: "./manuscript/apC.md": invalid command code .
sed: 1: "./manuscript/ch1.md": invalid command code .
sed: 1: "./manuscript/ch10.md": invalid command code .
sed: 1: "./manuscript/ch11.md": invalid command code .
sed: 1: "./manuscript/ch2.md": invalid command code .
sed: 1: "./manuscript/ch3.md": invalid command code .
sed: 1: "./manuscript/ch4.md": invalid command code .
sed: 1: "./manuscript/ch5.md": invalid command code .
sed: 1: "./manuscript/ch6.md": invalid command code .
sed: 1: "./manuscript/ch7.md": invalid command code .
sed: 1: "./manuscript/ch8.md": invalid command code .
sed: 1: "./manuscript/ch9.md": invalid command code .
sed: 1: "./manuscript/foreword.md": invalid command code .
sed: 1: "./manuscript/preface.md": invalid command code .
sed: 1: "./manuscript/README.md": invalid command code .
sed: 1: "./PULL_REQUEST_TEMPLATE.md": invalid command code .
sed: 1: "./README.md": invalid command code .
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE
sed: 1: "s#(<img.*[^/])>#\1 />#": \1 not defined in the RE

It produced an epub nevertheless, but with errors showing like so:

image

I'm sure it's an easy fix, but right now it's going over my head as I've never used these tools.

@nmaxcom
Copy link

nmaxcom commented Jan 30, 2018

I've tried @gidj adjustments and they got rid of all the script errors. Sadly though, the results seem to be the same.

In iBooks I think I can see the entire book, with those errors like in the capture from time to time, but in my preferred reader, ClearView, it only renders the first 4 pages. Which makes me think that trying in mobile or other platform would be a lottery.

@disdyakis
Copy link

disdyakis commented Mar 23, 2018

Looks like changing lines 16-20 to the following got rid of those error blocks, which I was getting too. -r isn't the right flag for extended regular expressions on macos but -E works, I guess? And also changing -i to -i '' is necessary for macos sed. And then also putting a -e flag before the script.

find . -iname "*.md" -exec sed -i '' -e '/# You Don'\''t Know JS.*/d' {} \;
# Fix <br> tags which pandoc won't convert to <br />
find . -iname "*.md" -exec sed -i '' -e 's#<br>#<br />#' {} \;
# Close img tags
find . -iname "*.md" -exec sed -i '' -E -e 's#(<img.*[^/])>#\1 />#' {} \;

Given that, I'm assuming this will work for Functional Light JS, but I haven't tried it yet:

# Clean up redundant headings that end up getting split into separate chapters by themselves
find . -iname "*.md" -exec sed -i '' -e '/# Functional-Light JavaScript/d' {} \;
# Fix <br> tags which pandoc won't convert to <br />
find . -iname "*.md" -exec sed -i '' -e 's#<br>#<br />#' {} \;
# Close img tags
find . -iname "*.md" -exec sed -i '' -E -e 's#(<img.*[^/])>#\1 />#' {} \;

@ZanderOlidan
Copy link

I managed to get rid of the error in the beginning with a different pandoc command. Change line 40 with this

pandoc -f html -t markdown_github foreword.html -o foreword.md

Everything seems to be working, along with the changes by @disdyakis

@jkevingutierrez
Copy link

jkevingutierrez commented Jun 7, 2018

There is a bug with some special characters: https://groups.google.com/forum/#!topic/pandoc-discuss/MFui4JJFY1s
I had to remove all the & characters on line 29, 46, 66, 85, 103 and 123. For example, instead of:

-M title="You Don't Know JS: Scope & Closures" \

I put:

-M title="You Don't Know JS: Scope Closures" \

Everything is ok, using the changes by @disdyakis and @kaivatar. I just modified a little the line 40:

pandoc -f html -t gfm foreword.html -o foreword.md

@Serlych
Copy link

Serlych commented Jan 20, 2019

Hi! I forked and fixed the script to fix the previous errors stated by @jkevingutierrez, @kaivatar and @gidj, also I noticed the Hack font install wasn't working, so I made sure script's users have brew and brew cask installed. Here it is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment