eostre.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/sh
  2. # NEEDS sed,grep,zip,unzip
  3. findTemplate() {
  4. templateName=$1
  5. for d in /usr/share/asgard/templates ~/.local/share/asgard/templates templates .
  6. do
  7. if stat "$d/$templateName" >/dev/null 2>&1
  8. then
  9. echo "$d/$templateName"
  10. return
  11. fi
  12. done
  13. return 1
  14. }
  15. set -e
  16. # shellcheck disable=SC2010
  17. years=$(ls Current/ | grep -E '^[0-9]{4}' -o | sort -u)
  18. if [ -n "$years" ]
  19. then
  20. mkdir -p tmp
  21. unzip diary.epub -d tmp
  22. fi
  23. spine=""
  24. manifest=""
  25. for year in $years
  26. do
  27. echo year "$year"
  28. # shellcheck disable=SC2010
  29. files=$(ls | grep -E "^$year")
  30. # shellcheck disable=SC2086
  31. cat $files > "../tmp/$year.html"
  32. # todo hyphenate
  33. # https://github.com/hunspell/hyphen
  34. # https://github.com/hunspell/hyphen/blob/master/example.c
  35. # ./newscripts/hyph_en_GB.dic
  36. sed -i 's|<hr>|<hr/>|g' "tmp/$year.html"
  37. footer="</body></html>"
  38. if [ ! -e "tmp/OEBPS/Text/$year.xhtml" ]
  39. then
  40. header='<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><title>'"$year"'</title><link href="../Styles/Style.css" rel="stylesheet" type="text/css"/></head><body><h1><a id="navPoint-'"$year"'">'"$year"'</a></h1>'
  41. chapter=$(printf "%s\n%s\n%s" "$header" "$(cat "tmp/$year.html")" "$footer")
  42. else
  43. sed -i -E 's$</(body|html)>$$' "tmp/OEBPS/Text/$year.xhtml"
  44. chapter=$(printf "%s\n%s\n%s" "$(cat "tmp/OEBPS/Text/$year.xhtml")" "$(cat "tmp/$year.html")" "$footer")
  45. fi
  46. echo "$chapter" > "tmp/OEBPS/Text/$year.xhtml"
  47. done
  48. # shellcheck disable=SC2010
  49. years=$(ls tmp/OEBPS/Text/ | grep -E '^[0-9]{4}' -o | sort -u)
  50. for year in $years
  51. do
  52. manifest="$manifest\n\t\t<item href=\"Text/$year.xhtml\" id=\"$year.xhtml\" media-type=\"application/xhtml+xml\"/>"
  53. spine="$spine\n\t\t<itemref idref=\"$year.xhtml\"/>"
  54. cp "$(findTemplate content.opf.template)" tmp/OEBPS/content.opf
  55. sed -i "s|{{manifest}}|$manifest|" tmp/OEBPS/content.opf
  56. sed -i "s|{{spine}}|$spine|" tmp/OEBPS/content.opf
  57. toc="$toc\n\t\t<li><a href=\"$year.xhtml\">$year</a></li>"
  58. cp "$(findTemplate nav.xhtml.template)" tmp/OEBPS/Text/nav.xhtml
  59. sed -i "s|{{toc}}|$toc|" tmp/OEBPS/Text/nav.xhtml
  60. done
  61. rm diary.epub
  62. cd tmp
  63. zip -X ../diary.epub mimetype
  64. zip -rg ../diary.epub META-INF
  65. zip -rg ../diary.epub OEBPS
  66. cd ..
  67. rm -rf tmp
  68. rm ./*.html