PageRenderTime 62ms CodeModel.GetById 52ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/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
 46PROGRAM=txt2tags
 47#
 48#   If you use another extension for your txt2tags files, change here
 49IN_EXT=.t2t
 50#
 51#   If you want to use this script for other formats than HTML
 52OUT_EXT=.html
 53TARGET=html
 54#
 55#   The default message for --interative question
 56QUESTION="   Convert? [yn] "
 57#
 58#   The --verbose messages
 59VERB_DIR="Will scan this dir:"
 60VERB_FILE="Scanning this file:"
 61#
 62#####################################################################
 63
 64# Atention: program code starts here, do not edit from this point
 65
 66PROGNAME=${0##*/}
 67USAGE="
 68$PROGNAME - part of the txt2tags project
 69
 70Usage: $PROGNAME [-c] [-f] [-m] [-i] [directory]
 71
 72  -c, --convert       Perform the conversion of the marked files
 73  -f, --force         Mark all .t2t files, even if up-to-date
 74  -m, --missing       Mark .t2t files that has no .html
 75  -i, --interative    Ask before convert
 76  -e, --in-ext        Set other source extension than .t2t
 77  -E, --out-ext       Set other target extension than .html
 78  -t, --target        Force a target type when converting (default: html)
 79  -p, --program       Set an alternative way to call txt2tags
 80  -v, --verbose       Show all scanned file names
 81  -h, --help          Show this help screen
 82
 83Common use: $PROGNAME -c /path/to/dir
 84Stress use: $PROGNAME -c -f -m -i /path/to/dir
 85"
 86
 87# Flags reset status
 88f_help=0
 89f_force=0
 90f_convert=0
 91f_missing=0
 92f_verbose=0
 93f_interative=0
 94
 95# Command line options parsing
 96while [ "$1" ] ; do
 97	case "$1" in
 98		-h|--help      ) f_help=1 ;;
 99		-f|--force     ) f_force=1 ;;
100		-m|--missing   ) f_missing=1 ;;
101		-c|--convert   ) f_convert=1 ;;
102		-v|--verbose   ) f_verbose=1 ;;
103		-i|--interative) f_interative=1 ;;
104		-e|--in-ext    ) IN_EXT="$2" ; shift;;
105		-E|--out-ext   ) OUT_EXT="$2"; shift;;
106		-t|--target    ) TARGET="$2" ; shift;;
107		-p|--program   ) PROGRAM="$2"; shift;;
108		*) break ;;
109	esac
110	shift
111done
112
113# Show help screen and exit
114if [ "$f_help" = 1 ]; then
115	echo "$USAGE"
116	exit 0
117fi
118
119# Set directory to user passed parameter or current one
120udir="${1:-.}"
121
122# The directory is valid (does exist)?
123if [ ! -d "$udir" ] ; then
124	echo "$PROGNAME: Sorry, '$udir' is not a valid directory"
125	exit 1
126fi
127
128# The default message showed before the filename
129MESSAGE="$TARGET needs update:"
130
131# Show scanned directory name
132[ "$f_verbose" = 1 ] && echo "$VERB_DIR $udir"
133
134# IFS+for trick try to avoid "find | while read" that mess -i
135IFS="
136"
137
138# Search for all .t2t files on the specified (or current) dir
139for t2t in $(find "$udir" -name "*$IN_EXT"); do
140
141	# Show scanned file name
142	[ "$f_verbose" = 1 ] && echo "$VERB_FILE $t2t"
143	
144	# Set the out file name as <file>.TARGET
145	out=${t2t%$IN_EXT}$OUT_EXT
146
147	# Flag that will tell if the conversion should be done
148	f_ok=0
149
150	# Force flag
151	if [ -f "$out" -a "$f_force" = 1 ]; then
152		f_ok=1
153
154	# Missing flag
155	elif [ ! -f "$out" -a "$f_missing" = 1 ] ; then
156		f_ok=1
157	
158	# If the .t2t is newer than the .TARGET...
159	elif [ -f "$out" -a "$t2t" -nt "$out" ] ; then
160		f_ok=1
161	fi
162
163	# Okay, the conversion may be done
164	if [ "$f_ok" = 1 ]; then
165		echo -n "$MESSAGE $t2t"
166		
167		# Last chance to give up
168		if [ "$f_convert" = 1 -a "$f_interative" = 1 ]; then
169			echo -n "$QUESTION"
170			read -n 1 ans
171			[ "$ans" = y -o "$ans" = Y ] || f_ok=0
172		fi	
173		echo
174
175		# Yes, we will
176		if [ "$f_convert" = 1 -a "$f_ok" = 1 ]; then
177			$PROGRAM -t $TARGET $t2t
178		fi
179	fi	
180done