選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

152 行
3.8KB

  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`
  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. if [ $? != 0 ]; then
  41. echo "error: cannot find HEAD in current directory, is there at least one commit"?
  42. exit 1
  43. fi
  44. dochash=`git rev-parse --short HEAD`
  45. if [ -e "baserev" ]; then
  46. basehash=`cat baserev`
  47. else
  48. basehash=`git rev-parse HEAD`
  49. fi
  50. headepoch=`git log -1 --format="%ct"`
  51. headdate=`LC_ALL=${DATELC} date -d@${headepoch} +${dateformat}`
  52. headdateedtf=`LC_ALL=${DATELC} date -d@${headepoch} +%Y-%m-%d`
  53. tag=`git tag --sort=-version:refname | head -n1`
  54. if [ -z "${tag}" ]; then
  55. revision="DRAFT"
  56. docver=`git rev-list ${basehash}..HEAD | wc -l`
  57. else
  58. revision=${tag}
  59. docver=`git rev-list ${tag}..HEAD | wc -l`
  60. fi
  61. if [ ${docver} -ne 0 ]; then
  62. revision="${revision}${docver}"
  63. fi
  64. hash=`git rev-parse --short HEAD`
  65. # hook-pre
  66. if [ -e hook-pre.sh ]; then
  67. ./hook-pre.sh
  68. if [ $? != 0 ]; then
  69. echo "hook-pre.sh failed!"
  70. exit 1
  71. fi
  72. fi
  73. # if the current working tree is not clean, we add a "+"
  74. # (should be $\epsilon$, but it would screw the file name),
  75. # set the date to today and change the author to the current user,
  76. # as it is the most probable scenario
  77. if [ -z "`git status --porcelain`" ]; then
  78. date=${headdate}
  79. dateedtf=${headdateedtf}
  80. else
  81. dochash="${hash}+$\epsilon$"
  82. revision="${revision}+"
  83. date=${currentdate}
  84. dateedtf=${currentdateedtf}
  85. fi
  86. cat << EOF > hash.yaml
  87. ---
  88. hash: ${dochash}
  89. rev: ${revision}
  90. date: ${date}
  91. ...
  92. EOF
  93. for format in ${formats}; do
  94. echo "md -> ${format}"
  95. flags="--number-sections --toc --syntax-definition=wasora.xml --syntax-definition=fino.xml"
  96. if [[ "${format}" == "pdf" ]] || [[ "${format}" == "tex" ]]; then
  97. template_file="${template}.tex"
  98. flags="${flags} --listings --pdf-engine=xelatex"
  99. if [[ "${format}" == "tex" ]]; then
  100. flags="${flags} --biblatex"
  101. fi
  102. elif [[ "${format}" == "html" ]]; then
  103. template_file="${template}.html"
  104. flags="${flags} --mathjax"
  105. else
  106. template_file="${template}.${format}"
  107. fi
  108. if [ -e "${template_file}" ]; then
  109. template_spec="--template $template_file"
  110. fi
  111. m4 -Doutput_format=${format} 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. # if [[ "${format}" == "pdf" ]]; then
  117. # docnumber=`grep docnumber: meta.yaml | awk -F: '{print $2}'`
  118. # if [[ ! -z "${docnumber}" ]] && [[ ! -e "${docnumber}.pdf" ]]; then
  119. # ln -sf ${name}.pdf ${docnumber}.pdf
  120. # fi
  121. # fi
  122. done