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