-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtmltree.sh
executable file
·372 lines (274 loc) · 9.32 KB
/
htmltree.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env bash
# export all variables by default.
# Used by the perl replacement script in substitute_variables() function
set -a
printhelp()
{
printf "$0 flags [ PATH ]
-H | --head [ Header Template File ]
-F | --foot [ Footer Template File ]
-LF | --li-file [ LI Template for files ]
-LD | --li-directory [ LI Template for directories ]
-G | --git-ignore Look for a .gitignore file and hide files that match pattern.
-I | --hide-index Exclude the index.html file from the output listings.
--help\n"
}
# ┌─────────────────────────────────────┐
# │ Check Arguments │
# └─────────────────────────────────────┘
if [ "$#" -eq 0 ]; then
printhelp
exit 1
fi
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-H|--head)
HTML_HEAD="$2"
shift # past argument
shift # past value
;;
-F|--foot)
HTML_FOOT="$2"
shift # past argument
shift # past value
;;
-LF|--li-file)
HTML_LI_FILE="$2"
shift # past argument
shift # past value
;;
-LD|--li-directory)
HTML_LI_DIRECTORY="$2"
shift # past argument
shift # past value
;;
-G|--git-ignore)
GIT_IGNORE="yes"
shift # past argument
;;
-I|--hide-index)
HIDE_INDEX="yes"
shift # past argument
;;
--help)
printhelp
shift # past argument
exit 0
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
TARGET_PATH="$1"
shift # past argument
;;
esac
done
# restore positional parameters
set -- "${POSITIONAL_ARGS[@]}"
# ┌─────────────────────────────────────┐
# │ VARIABLES │
# └─────────────────────────────────────┘
ABSOLUTE_PATH="$(cd "$(dirname "$TARGET_PATH")"; pwd)/$(basename "$TARGET_PATH")"
CURRENT_PATH=$(pwd)
IGNORE=''
# ┌────────────────────────────────────┐
# │ │
# │ Substitute any {{moustaches}} │
# │ for variables of the same name. │
# │ │
# └────────────────────────────────────┘
substitute_variables()
{
CONTENTS=$(perl -pe 's,{{(.*?)}},$ENV{$1},g' <<< $CONTENTS)
}
# ┌─────────────────────────────────────┐
# │ HTML TEMPLATES │
# └─────────────────────────────────────┘
html_head()
{
DEFAULT_HTML_HEAD="
<html>
<head>
</head>
<body>
<h1>FILE LISTING: {{FOLDER}}</h1>
<ul>
"
# If HTML_HEAD (-H) flag is set
if [ -z ${HTML_HEAD+x} ]; then
CONTENTS="${DEFAULT_HTML_HEAD}"
else
CONTENTS=$(cat ${CURRENT_PATH}/${HTML_HEAD})
# Replace % with %% so that printf will work.
CONTENTS=$(echo "${CONTENTS}" | sed "s#%#%%#g")
fi
# Substitute any {{moustaches}}
substitute_variables
printf "${CONTENTS}\n" > index.html
}
html_li_directory()
{
DEFAULT_HTML_LI_DIRECTORY="<li class=\"item\"><a href=\"{{file_path}}\">{{file_name}}</a></li>\n"
if [ -z ${HTML_LI_DIRECTORY+x} ]; then
CONTENTS="${DEFAULT_HTML_LI_DIRECTORY}"
else
CONTENTS=$(cat ${CURRENT_PATH}/${HTML_LI_DIRECTORY})
# Replace % with %% so that printf will work.
CONTENTS=$(echo "${CONTENTS}" | sed "s#%#%%#g")
fi
# Substitute any {{moustaches}}
substitute_variables
printf "${CONTENTS}\n" >> index.html
}
html_li_file()
{
DEFAULT_HTML_LI_FILE="<li class=\"item\"><a href=\"{{file_path}}\">{{file_name}}</a></li>\n"
if [ -z ${HTML_LI_FILE+x} ]; then
CONTENTS="${DEFAULT_HTML_LI_FILE}"
else
CONTENTS=$(cat ${CURRENT_PATH}/${HTML_LI_FILE})
# printf "${CONTENTS}\n"
# Replace % with %% so that printf will work.
CONTENTS=$(echo "${CONTENTS}" | sed "s#%#%%#g")
fi
# Substitute any {{moustaches}}
substitute_variables
printf "${CONTENTS}\n" >> index.html
}
html_foot()
{
DEFAULT_HTML_FOOT="
</ul>
</body>
</html>
"
if [ -z ${HTML_FOOT+x} ]; then
CONTENTS="${DEFAULT_HTML_FOOT}"
else
CONTENTS=$(cat ${CURRENT_PATH}/${HTML_FOOT})
# Replace % with %% so that printf will work.
CONTENTS=$(echo "${CONTENTS}" | sed "s#%#%%#g")
fi
# Substitute any {{moustaches}}
substitute_variables
printf "${CONTENTS}\n" >> index.html
}
gitignore()
{
# unset IGNORE
cat ${CURRENT_PATH}/.gitignore | while IFS="" read -r IGNORE_RULE || [ -n "$IGNORE_RULE" ]
do
if [[ $file_name == $IGNORE_RULE ]]; then
echo "yes"
fi
done
}
# ┌─────────────────────────────────────┐
# │ REGEX ls -la │
# └─────────────────────────────────────┘
regex_fileline()
{
# Remove multiple spaces
FILELINE=$(echo $1 | tr -s ' ')
# Define REGEX
regex="^(.*)[[:space:]]+(.*)[[:space:]]+(.*)[[:space:]]+(.*)[[:space:]]+(.*)[[:space:]]+(.*)[[:space:]]+(.*)[[:space:]]+(.*)[[:space:]]+(.*)"
if [[ $FILELINE =~ $regex ]]
then
file_permissions="${BASH_REMATCH[1]}"
file_subdirectories="${BASH_REMATCH[2]}"
file_owner="${BASH_REMATCH[3]}"
file_group="${BASH_REMATCH[4]}"
file_size="${BASH_REMATCH[5]}"
file_day="${BASH_REMATCH[6]}"
file_month="${BASH_REMATCH[7]}"
file_time="${BASH_REMATCH[8]}"
file_name="${BASH_REMATCH[9]}"
file_path="./${BASH_REMATCH[9]}"
file_extension="${file_name##*.}"
file_type="file"
# Skip the . and .. folders.
if [[ "$file_name" == "." ]] || [[ "$file_name" == ".." ]] ; then
file_type="directory"
return
fi
# If the file is a directory, but not [.|..] output index.html on end.
if [[ $file_permissions == d* ]]; then
file_type="directory"
file_path="${file_path}/index.html"
fi
else
echo "$FILELINE doesn't match regex" >&2 # this could get noisy if there are a lot of non-matching files
fi
}
# ┌─────────────────────────────────────┐
# │ List Files in Folder │
# └─────────────────────────────────────┘
list_files()
{
IFS=$'\n'
# List all files in folder.
FILES=($(ls -lah . | grep -v "total"))
unset IFS
# Foreach file in the folder...
for FILELINE in "${FILES[@]}"
do
# Get each component of the line
regex_fileline "${FILELINE}"
# Skip the . folder.
if [[ "$file_name" == "." ]]; then
continue
fi
# Skip the root .. folder.
if [[ "$file_name" == ".." ]] && [[ "${ABSOLUTE_PATH}" == "$(pwd)" ]]; then
continue
fi
# Skip index.html if (HIDE_INDEX) is set.
if [[ "$file_name" == "index.html" ]] && [[ "${HIDE_INDEX}" == "yes" ]]; then
continue
fi
if [[ "${GIT_IGNORE}" == "yes" ]]; then
IGNORE="$(gitignore)"
if [[ "${IGNORE}" == "yes" ]]; then
continue
fi
fi
if [[ "$file_type" == "directory" ]]; then
html_li_directory
continue
fi
# Output the <LI>
html_li_file
done
unset FILES
}
# ┌─────────────────────────────────────┐
# │ Foreach folder │
# └─────────────────────────────────────┘
run()
{
IFS=$'\n'
# Find all folders in target directory and remove the target path from each result.
FOLDERS=($(find ${TARGET_PATH} -type d \( ! -name . \) | sed "s#${TARGET_PATH}#.#g"))
unset IFS
# Move into target directory.
cd $TARGET_PATH
for FOLDER in "${FOLDERS[@]}"
do
printf "FOLDER: %s\n" "${FOLDER}"
# Move into FOLDER
cd $FOLDER
# Output HTML head
html_head
# List file and create index
list_files
# Output HTML footer
html_foot
cd $ABSOLUTE_PATH
done
}
# Run script
run