/extras/html-update.sh

http://txt2tags.googlecode.com/ · Shell · 180 lines · 83 code · 23 blank · 74 comment · 12 complexity · 40f5654ed5a97e74e6d84db73c046333 MD5 · raw file

  1. #!/bin/bash
  2. # html-update.sh - converts to HTML all .t2t files that has changed
  3. #
  4. # Author: Aurelio Jargas
  5. # Requires: bash, find, txt2tags
  6. # Debut: June, 2004
  7. # Last Update: 27 Sep 2005
  8. #
  9. # Based on Guaracy Monteiro's t2tmake.rb program.
  10. # Implemented some ideas from Mauricio Teixeira (netmask).
  11. #
  12. # This file is part of the txt2tags (http://txt2tags.org) project.
  13. # Its license is the same as the program: GPL. See txt2tags COPYING
  14. # file for a detailed explanation of the GPL license.
  15. #
  16. # INSTRUCTIONS
  17. #
  18. # With no options, it searches recursively the current directory and
  19. # prints on the screen all the filenames that needs to be converted.
  20. #
  21. # With the '-c' option, it performs the conversion of the found
  22. # files.
  23. #
  24. # With a directory path as the last argument, it searches that
  25. # directory instead the current dir.
  26. #
  27. # Use the --help option for more information.
  28. #
  29. # NOTES
  30. #
  31. # The condition to a file be considered 'needed' to be converted is
  32. # that the .t2t file is newer than its respective .html. In other
  33. # words, you edited the .t2t after the last conversion, so it needs
  34. # to be converted again. You can ignore this checking using the
  35. # --force option.
  36. #
  37. # It assumes that the .html file uses the same basename as the .t2t
  38. # file. If you used the txt2tags' --outfile option to change the
  39. # default output filename ou extension, it will not be detected by
  40. # this script. You can force this detection using the --missing
  41. # option.
  42. #
  43. # CONFIG
  44. #
  45. # If you call txt2tags any other way, change here
  46. PROGRAM=txt2tags
  47. #
  48. # If you use another extension for your txt2tags files, change here
  49. IN_EXT=.t2t
  50. #
  51. # If you want to use this script for other formats than HTML
  52. OUT_EXT=.html
  53. TARGET=html
  54. #
  55. # The default message for --interative question
  56. QUESTION=" Convert? [yn] "
  57. #
  58. # The --verbose messages
  59. VERB_DIR="Will scan this dir:"
  60. VERB_FILE="Scanning this file:"
  61. #
  62. #####################################################################
  63. # Atention: program code starts here, do not edit from this point
  64. PROGNAME=${0##*/}
  65. USAGE="
  66. $PROGNAME - part of the txt2tags project
  67. Usage: $PROGNAME [-c] [-f] [-m] [-i] [directory]
  68. -c, --convert Perform the conversion of the marked files
  69. -f, --force Mark all .t2t files, even if up-to-date
  70. -m, --missing Mark .t2t files that has no .html
  71. -i, --interative Ask before convert
  72. -e, --in-ext Set other source extension than .t2t
  73. -E, --out-ext Set other target extension than .html
  74. -t, --target Force a target type when converting (default: html)
  75. -p, --program Set an alternative way to call txt2tags
  76. -v, --verbose Show all scanned file names
  77. -h, --help Show this help screen
  78. Common use: $PROGNAME -c /path/to/dir
  79. Stress use: $PROGNAME -c -f -m -i /path/to/dir
  80. "
  81. # Flags reset status
  82. f_help=0
  83. f_force=0
  84. f_convert=0
  85. f_missing=0
  86. f_verbose=0
  87. f_interative=0
  88. # Command line options parsing
  89. while [ "$1" ] ; do
  90. case "$1" in
  91. -h|--help ) f_help=1 ;;
  92. -f|--force ) f_force=1 ;;
  93. -m|--missing ) f_missing=1 ;;
  94. -c|--convert ) f_convert=1 ;;
  95. -v|--verbose ) f_verbose=1 ;;
  96. -i|--interative) f_interative=1 ;;
  97. -e|--in-ext ) IN_EXT="$2" ; shift;;
  98. -E|--out-ext ) OUT_EXT="$2"; shift;;
  99. -t|--target ) TARGET="$2" ; shift;;
  100. -p|--program ) PROGRAM="$2"; shift;;
  101. *) break ;;
  102. esac
  103. shift
  104. done
  105. # Show help screen and exit
  106. if [ "$f_help" = 1 ]; then
  107. echo "$USAGE"
  108. exit 0
  109. fi
  110. # Set directory to user passed parameter or current one
  111. udir="${1:-.}"
  112. # The directory is valid (does exist)?
  113. if [ ! -d "$udir" ] ; then
  114. echo "$PROGNAME: Sorry, '$udir' is not a valid directory"
  115. exit 1
  116. fi
  117. # The default message showed before the filename
  118. MESSAGE="$TARGET needs update:"
  119. # Show scanned directory name
  120. [ "$f_verbose" = 1 ] && echo "$VERB_DIR $udir"
  121. # IFS+for trick try to avoid "find | while read" that mess -i
  122. IFS="
  123. "
  124. # Search for all .t2t files on the specified (or current) dir
  125. for t2t in $(find "$udir" -name "*$IN_EXT"); do
  126. # Show scanned file name
  127. [ "$f_verbose" = 1 ] && echo "$VERB_FILE $t2t"
  128. # Set the out file name as <file>.TARGET
  129. out=${t2t%$IN_EXT}$OUT_EXT
  130. # Flag that will tell if the conversion should be done
  131. f_ok=0
  132. # Force flag
  133. if [ -f "$out" -a "$f_force" = 1 ]; then
  134. f_ok=1
  135. # Missing flag
  136. elif [ ! -f "$out" -a "$f_missing" = 1 ] ; then
  137. f_ok=1
  138. # If the .t2t is newer than the .TARGET...
  139. elif [ -f "$out" -a "$t2t" -nt "$out" ] ; then
  140. f_ok=1
  141. fi
  142. # Okay, the conversion may be done
  143. if [ "$f_ok" = 1 ]; then
  144. echo -n "$MESSAGE $t2t"
  145. # Last chance to give up
  146. if [ "$f_convert" = 1 -a "$f_interative" = 1 ]; then
  147. echo -n "$QUESTION"
  148. read -n 1 ans
  149. [ "$ans" = y -o "$ans" = Y ] || f_ok=0
  150. fi
  151. echo
  152. # Yes, we will
  153. if [ "$f_convert" = 1 -a "$f_ok" = 1 ]; then
  154. $PROGRAM -t $TARGET $t2t
  155. fi
  156. fi
  157. done