您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

138 行
3.2KB

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