You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 line
3.7KB

  1. #!/bin/bash
  2. for i in m4 pandoc pandoc-crossref pandoc-citeproc xelatex locale grep wc; do
  3. if [ -z "`which $i`" ]; then
  4. echo "error: $i is not installed"
  5. exit 1
  6. fi
  7. done
  8. # the document name is the current directory name
  9. name=`basename $PWD`-emf
  10. if [ ! -e ${name}.md ]; then
  11. echo "error: ${name}.md does not exist"
  12. exit 1
  13. fi
  14. # get the date right
  15. language=`grep lang: meta.yaml | awk -F: '{print $2}' | sed s/-/_/`
  16. LC=`locale -a | grep $language | head -n1`
  17. if [ -z "$LC" ]; then
  18. echo "error: locale for language $language is not installed, run"
  19. echo "dpkg-reconfigure locales"
  20. echo "or equivalent (or change the document's language)"
  21. exit 1
  22. fi
  23. # see how we are supposed to write the date, but
  24. # replace %m with %b to get a string for the month
  25. dateformat=`LC_ALL=${LC} locale d_fmt | sed s/m/b/`
  26. currentdate=`LC_ALL=${LC} date +${dateformat}`
  27. currentdateedtf=`LC_ALL=${LC} date +%Y-%m-%d`
  28. # read the template name from the md's yaml, otherwise default to light
  29. template=`grep template: meta.yaml | awk -F': ' '{print $2}'`
  30. if [ -z "$template" ]; then
  31. template=light
  32. fi
  33. # read the formats from the md's yaml
  34. if [ ! -z "$1" ]; then
  35. formats="$1"
  36. else
  37. formats=`grep formats: meta.yaml | awk -F': ' '{print $2}'`
  38. fi
  39. branch=`git rev-parse --abbrev-ref HEAD`
  40. dochash=`git rev-parse --short HEAD`
  41. if [ -e "baserev" ]; then
  42. basehash=`cat baserev`
  43. else
  44. basehash=`git rev-parse HEAD`
  45. fi
  46. headepoch=`git log -1 --format="%ct"`
  47. headdate=`LC_ALL=${DATELC} date -d@${headepoch} +${dateformat}`
  48. headdateedtf=`LC_ALL=${DATELC} date -d@${headepoch} +%Y-%m-%d`
  49. tag=`git tag --sort=-version:refname | head -n1`
  50. if [ -z "${tag}" ]; then
  51. revision="DRAFT"
  52. docver=`git rev-list ${basehash}..HEAD | wc -l`
  53. else
  54. revision=${tag}
  55. docver=`git rev-list ${tag}..HEAD | wc -l`
  56. fi
  57. if [ ${docver} -ne 0 ]; then
  58. revision="${revision}${docver}"
  59. fi
  60. hash=`git rev-parse --short HEAD`
  61. # hook-pre
  62. if [ -e hook-pre.sh ]; then
  63. ./hook-pre.sh
  64. if [ $? != 0 ]; then
  65. echo "hook-pre.sh failed!"
  66. exit 1
  67. fi
  68. fi
  69. # if the current working tree is not clean, we add a "+"
  70. # (should be $\epsilon$, but it would screw the file name),
  71. # set the date to today and change the author to the current user,
  72. # as it is the most probable scenario
  73. if [ -z "`git status --porcelain`" ]; then
  74. date=${headdate}
  75. dateedtf=${headdateedtf}
  76. else
  77. dochash="${hash}+$\epsilon$"
  78. revision="${revision}+"
  79. date=${currentdate}
  80. dateedtf=${currentdateedtf}
  81. fi
  82. cat << EOF > hash.yaml
  83. ---
  84. hash: ${dochash}
  85. rev: ${revision}
  86. date: ${date}
  87. ...
  88. EOF
  89. for format in ${formats}; do
  90. echo "md -> ${format}"
  91. flags="--toc --number-sections"
  92. if [[ "${format}" == "pdf" ]] || [[ "${format}" == "tex" ]]; then
  93. template_file="${template}.tex"
  94. flags="${flags} --listings --pdf-engine=xelatex"
  95. if [[ "${format}" == "tex" ]]; then
  96. flags="${flags} --biblatex"
  97. fi
  98. elif [[ "${format}" == "html" ]]; then
  99. template_file="${template}.html"
  100. flags="${flags} --katex"
  101. elif [[ "${format}" == "docx" ]]; then
  102. flags="${flags} --reference-doc=CS-Book.docx"
  103. else
  104. template_file="${template}.${format}"
  105. fi
  106. if [ -e "${template_file}" ]; then
  107. template_spec="--template $template_file"
  108. else
  109. template_spec=""
  110. fi
  111. m4 markuw.m4 meta.yaml ${name}.md hash.yaml |\
  112. pandoc -s ${flags} ${template_spec} --filter pandoc-crossref --filter pandoc-citeproc -o ${name}.${format} || exit 1
  113. if [ -z "`git check-ignore ${name}.${format}`" ]; then
  114. echo ${name}.${format} >> .gitignore
  115. fi
  116. # post-process
  117. if [[ "${format}" == "html" ]]; then
  118. sed -i 's/<table>/<table class="table table-responsive-md table-striped table-hover">/' ${name}.${format}
  119. sed -i 's/<blockquote>/<blockquote class="blockquote">/' ${name}.${format}
  120. fi
  121. done