Skip to content

Instantly share code, notes, and snippets.

@milouse
Created June 8, 2017 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milouse/d6bb69dbf8623228a510e2290c3e020c to your computer and use it in GitHub Desktop.
Save milouse/d6bb69dbf8623228a510e2290c3e020c to your computer and use it in GitHub Desktop.
The script I use to build an epub version of https://coffeescript-cookbook.github.io/
#!/usr/bin/env bash
cat <<EOF > list_input.rb
require 'yaml'
cur_dir = File.expand_path(File.dirname(__FILE__))
chapters = YAML.safe_load(IO.read(cur_dir + '/_data/chapters.yml'))
puts "#{cur_dir}/license.md\n#{cur_dir}/authors.md"
chapters.each do |c|
dirname = c.tr(' ', '_').downcase
Dir.glob("#{cur_dir}/chapters/#{dirname}/*.md") do |filename|
puts filename
end
end
["#{cur_dir}/contributing.md",
"#{cur_dir}/authors-guide.md",
"#{cur_dir}/developers-guide.md",
"#{cur_dir}/designers-guide.md"].each do |filename|
puts filename
end
EOF
files=$(ruby list_input.rb)
truncate -s 0 chapters_list
for f in $files; do
echo '' >> "$f"
echo '' >> "$f"
sed -i 's/
//' "$f"
cur_layout=$(sed -n '1,5s/^layout: \(.*\)$/\1/p' "$f")
cur_title=$(sed -n '1,5s/^title: \(.*\)$/\1/p' "$f")
if [ "$cur_layout" != 'default' -o "$cur_title" = 'License' -o "$cur_title" = 'Authors' ]; then
cur_chapter=$(sed -n '1,5s/^chapter:\( *\)\(.*\)$/\2/p' "$f")
truncate -s 0 cur_temp
sed -r 's/^(#+)\s+(.*)$/#\1 \2/' "$f" > cur_main
if [ x"${cur_chapter}" != x ]; then
if ! grep -iq "$cur_chapter" chapters_list ; then
echo "$cur_chapter" >> chapters_list
printf "# %s\n\n" "$cur_chapter" >> cur_temp
fi
fi
[ "$cur_layout" != 'default' -a x"${cur_title}" != x ] && \
printf "## %s\n\n" "$cur_title" >> cur_temp
cat cur_temp cur_main > "$f"
fi
sed -i -e '
1,10 {
s/^chapter: .*$//
s/^title: .*$//
s/^layout: .*$//
s/^---$//
}' "$f"
loc_dir=$(dirname "$f")
sed -i "s|img src=\"images/|img src=\"$loc_dir/images/|" "$f"
done
cat $files > doc.md
sed -i -e 's/^{% endhighlight %}$/```/' \
-e 's/^{% highlight \([^ ]*\).* %}$/```\1/' doc.md
sed -i -r 's/\s+$//' doc.md
cat <<EOF > cover.md
---
title: CoffeeScript Cookbook
...
# About this book
EOF
pandoc -S --normalize -o coffeescript_cookbook.epub cover.md doc.md
for lineinfo in $(git status -sb | sed -e "/^##/d" -e "s/^ \(A\|C\|D\|M\|R\) \(.*\)$/\1\1\1\2/" -e "s/^?? \(.*\)$/???\1/"); do
linestatus=${lineinfo:0:3}
linefile=${lineinfo:3}
case "$linestatus" in
MMM|AAA|DDD|RRR|CCC) git checkout $linefile ;;
'???')
[ "$linefile" != 'make_epub.sh' -a "$linefile" != 'coffeescript_cookbook.epub' ] && \
rm -r $linefile ;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment