working version

have to fix the footer
pull/1/head
Alessandro Mauri 4 years ago
parent e109425557
commit 46a09ad7e7
  1. 107
      rivet

107
rivet

@ -6,36 +6,21 @@ unset SKIP_HEADER
unset SKIP_LIST unset SKIP_LIST
unset VERBOSE unset VERBOSE
unset PRINT_HELP unset PRINT_HELP
unset SKIP_SITEMAP
usage() { usage() {
printf "Usage: rivet [-o destdir] [-hovelf] srcdir\n" printf "Usage: rivet [-o destdir] [-hovelf] srcdir domain\n"
printf "\t-h: prints this message\n" printf "\t-h: prints this message\n"
printf "\t-o [destdir]: specifies the output direcotory to be [destdir]\n" printf "\t-o [destdir]: specifies the output direcotory to be [destdir]\n"
printf "\t-v: makes the script verbose\n" printf "\t-v: makes the script verbose\n"
printf "\t-e: skips prepending _header.html to .html files\n" printf "\t-e: skips prepending _header.html to .html files\n"
printf "\t-f: skips appending _footer.html to .html files\n" printf "\t-f: skips appending _footer.html to .html files\n"
printf "\t-l: skips the generation of .html file list\n" printf "\t-l: skips the generation of .html file list\n"
printf "\t-s: skips sitemap.xml creation\n"
printf "\t-u: makes all references to the url 'http' instead of 'https'\n"
exit 2 exit 2
} }
apply_header() {
tmpfile="tmpheadfile.tmp"
cp "$1" "$tmpfile"
sed "/<head>/r $headerfile" "$tmpfile" > "$1"
rm -f "$tmpfile"
}
apply_footer() {
tmpfoot="tmpfooter.tmp"
tmpfile="tmpfootfile.tmp"
cp "$footerfile" "$tmpfoot"
sed -i '1s/^/<footer> /' "$tmpfoot"
echo '</footer>' >> "$tmpfoot"
cp "$1" "$tmpfile"
sed "/<\/body/r $tmpfoot" "$tmpfile" > "$1"
rm -f "$tmpfile" "$tmpfoot"
}
convert() { convert() {
tmpfile="tmpconvfile.tmp" tmpfile="tmpconvfile.tmp"
infile="$1" infile="$1"
@ -53,6 +38,7 @@ if ! command -v lowdown > /dev/null; then
fi fi
destdir='dst' destdir='dst'
prefix='https'
# Get arguments using getopts # Get arguments using getopts
# rivet [src] [-vhelf] [-o dir] # rivet [src] [-vhelf] [-o dir]
# -o [dir]: set the destination directory, defaults to "dst" # -o [dir]: set the destination directory, defaults to "dst"
@ -61,24 +47,40 @@ destdir='dst'
# -e: skip prepend header # -e: skip prepend header
# -l: skip article list # -l: skip article list
# -f: skip append footer # -f: skip append footer
while getopts 'o:vhelf' c # -s: skip sitemap creation
while getopts 'o:vhelfsu' c
do do
case "$c" in case "$c" in
o) destdir=$OPTARG ;; o) destdir=${OPTARG%%\/} ;;
v) VERBOSE=true ;; v) VERBOSE=true ;;
h) PRINT_HELP=true ;; h) PRINT_HELP=true ;;
e) SKIP_HEADER=true ;; e) SKIP_HEADER=true ;;
l) SKIP_LIST=true ;; l) SKIP_LIST=true ;;
f) SKIP_FOOTER=true ;; f) SKIP_FOOTER=true ;;
s) SKIP_SITEMAP=true ;;
u) prefix='http' ;;
*) ;; *) ;;
esac esac
done done
shift $((OPTIND - 1)) shift $((OPTIND - 1))
srcdir="$(basename "$1")" if ! [ "$1" ] || ! [ "$2" ]; then
echo "Not enough arguments"
usage
fi
src="$1"
srcdir=${src%%\/}
unset src
# Remove any junk from the domain eg. [https://]domain.com[/]
url="$(echo "$2" |
sed -e 's/^https*:\/\///' |
sed -e 's/\/$//' |
sed -e 's/[]\/$*.^[]/\\&/g')"
headerfile=$srcdir/_header.html headerfile=$srcdir/_header.html
footerfile=$srcdir/_footer.html footerfile=$srcdir/_footer.html
if [ "$PRINT_HELP" ]; then if [ "$PRINT_HELP" ]; then
usage usage
fi fi
@ -101,19 +103,70 @@ while IFS="" read -r file; do
convert "$file" convert "$file"
done done
# Prepend header # Prepare the header
if ! [ "$SKIP_HEADER" ]; then if ! [ "$SKIP_HEADER" ]; then
find "$destdir" -name "*.html" | find "$destdir" -name "*.html" |
while IFS="" read -r file; do while IFS="" read -r file; do
apply_header "$file" sed -i "/<head>/r $headerfile" "$file"
done done
fi fi
# Append footer # Prepate the footer
if ! [ "$SKIP_FOOTER" ]; then if ! [ "$SKIP_FOOTER" ]; then
find "$destdir" -name "*.html" | find "$destdir" -name "*.html" |
while IFS="" read -r file; do while IFS="" read -r file; do
apply_footer "$file" sed -i "/<footer>/r $footerfile" "$file"
done done
fi fi
# Prepare the sitemap & file list
if ! [ "$SKIP_SITEMAP" ] || ! [ "$SKIP_LIST" ]; then
linklist="linklist.tmp"
# echo "" > "$linklist"
rm -f "$linklist" "$destdir"/sitemap.xml
find "$destdir" -name "*.html" |
while IFS="" read -r file; do
echo "${file#$destdir/}" >> "$linklist"
done
if ! [ "$SKIP_LIST" ]; then
tmpfile="linkindex.tmp"
rm -f "$tmpfile"
cat << EOF >> "$tmpfile"
<div id="map">
<h2 id="Pages">Pages</h2>
EOF
while IFS="" read -r line; do
if echo "$line" | grep 'index\.html'; then
continue
fi
title="$(grep -e '^<title>.*<\/title>' "$destdir"/"$line" |
sed -e 's/<title>//' -e 's/<\/title>//')"
if ! [ "$title" ]; then
title=${line%?????}
fi
printf "<p><a href=\"%s\">%s</a></p>\n" "$line" "$title" >> "$tmpfile"
done < "$linklist"
echo '</div>' >> "$tmpfile"
sed -i '/<\/body>/i REPLACE' "$destdir"/index.html
sed -i "/^REPLACE/r $tmpfile" "$destdir"/index.html
sed -i 's/^REPLACE//' "$destdir"/index.html
rm -f "$tmpfile"
fi
if ! [ "$SKIP_SITEMAP" ]; then
sed -i -e "s/^/$prefix:\/\/$url\//" "$linklist"
cat << EOF >> "$destdir"/sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
</urlset>
EOF
while IFS="" read -r line; do
sed -i "/<\/urlset>/i \
<url><loc>$line<\/loc><\/url>" "$destdir"/sitemap.xml
done < "$linklist"
sed -i 's/^<url>/\t<url>/' "$destdir"/sitemap.xml
fi
rm -f "$linklist"
fi
exit exit

Loading…
Cancel
Save