@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/sh -e
# Copyright (c) 2020 Alessandro Mauri
# Copyright (c) 2020 Alessandro Mauri
#
#
@ -27,29 +27,37 @@ unset SKIP_LIST
unset VERBOSE
unset VERBOSE
unset PRINT_HELP
unset PRINT_HELP
unset SKIP_SITEMAP
unset SKIP_SITEMAP
unset SKIP_FEED
usage( ) {
usage( ) {
printf "Usage: rivet [-hovelfsu] [-p string] [-o destdir] srcdir domain\n"
printf "Usage: rivet [-hovelfsu] [-p string] [-o destdir] 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-p: [string]: Rename the \"Pages\" section to [string]\n"
printf "\t-p [string]: Rename the \"Pages\" section to [string]\n"
printf "\t-n [string]: Set the title of the atom feed"
printf "\t-d [string]: Set the description of the atom feed"
printf "\t-m [number]: Set the max number of elements in the atom feed"
printf "\t-v: Makes the script verbose\n"
printf "\t-v: Makes the script verbose\n"
printf "\t-e: Do not prepend _header.html to .html files\n"
printf "\t-e: Do not prepend _header.html to .html files\n"
printf "\t-f: Do not prepend _footer.html to .html files\n"
printf "\t-f: Do not prepend _footer.html to .html files\n"
printf "\t-l: Do not generate \"Pages\" section in index.html\n"
printf "\t-l: Do not generate \"Pages\" section in index.html\n"
printf "\t-s: Do not generate sitemap.xml\n"
printf "\t-s: Do not generate sitemap.xml\n"
printf "\t-r: Do not generate an atom feed\n"
printf "\t-u: Makes all references to the url 'http' instead of 'https'\n"
printf "\t-u: Makes all references to the url 'http' instead of 'https'\n"
exit 2
exit 2
}
}
convert( ) {
convert( ) {
tmpfile = "tmpconvfile.tmp"
infile = " $1 "
infile = " $1 "
outfile = " ${ infile %??? } .html "
extension = " ${ infile ##*. } "
if [ " $extension " = 'md' ] ; then
tmpfile = "tmpconvfile.tmp"
outfile = " ${ infile %md } html "
cp " $infile " " $tmpfile "
cp " $infile " " $tmpfile "
# TODO: convert links to .md to .html
lowdown -s -Thtml -o " $outfile " " $tmpfile "
lowdown -s -Thtml -o " $outfile " " $tmpfile "
rm -f " $tmpfile " " $infile "
rm -f " $tmpfile " " $infile "
fi
# TODO: convert links to .md to .html
}
}
# Check dependencies
# Check dependencies
@ -61,7 +69,10 @@ fi
destdir = 'dst'
destdir = 'dst'
prefix = 'https'
prefix = 'https'
linksec = 'Pages'
linksec = 'Pages'
while getopts 'o:vhelfsup:' c
blog_title = 'Atom feed'
blog_desc = ''
blog_nmax = '3'
while getopts 'o:vhelfsrup:n:d:m:' c
do
do
case " $c " in
case " $c " in
o) destdir = ${ OPTARG %% \/ } ; ;
o) destdir = ${ OPTARG %% \/ } ; ;
@ -71,8 +82,12 @@ do
l) SKIP_LIST = true ; ;
l) SKIP_LIST = true ; ;
f) SKIP_FOOTER = true ; ;
f) SKIP_FOOTER = true ; ;
s) SKIP_SITEMAP = true ; ;
s) SKIP_SITEMAP = true ; ;
r) SKIP_FEED = true ; ;
u) prefix = 'http' ; ;
u) prefix = 'http' ; ;
p) linksec = " $OPTARG " ; ;
p) linksec = " $OPTARG " ; ;
n) blog_title = " $OPTARG " ; ;
d) blog_desc = " $OPTARG " ; ;
m) blog_nmax = " $OPTARG " ; ;
*) ; ;
*) ; ;
esac
esac
done
done
@ -87,7 +102,7 @@ srcdir=${src%%\/}
unset src
unset src
headerfile = $srcdir /_header.html
headerfile = $srcdir /_header.html
footerfile = $srcdir /_footer.html
footerfile = $srcdir /_footer.html
filelist = file list.tmp
objlist = obj list.tmp
# Check if index.md is present
# Check if index.md is present
if ! [ -e " $srcdir " /index.md ] ; then
if ! [ -e " $srcdir " /index.md ] ; then
@ -111,10 +126,10 @@ sed -i 's/<footer.*>//' "$footerfile"
sed -i 's/<\/footer>//' " $footerfile "
sed -i 's/<\/footer>//' " $footerfile "
# Remove any junk from the domain eg. [https://]domain.com[/]
# Remove any junk from the domain eg. [https://]domain.com[/]
url = " $( echo " $2 " |
domain = " $( echo " $2 " | sed -e 's/^https*:\/\///' -e 's/\/$//' ) "
sed -e 's/^https*:\/\///' |
sed -e 's/\/$//' |
# Save the real url
sed -e 's/[]\/$*.^[]/\\&/g' ) "
url = " $prefix " '://' " $domain "
if [ " $PRINT_HELP " ] ; then
if [ " $PRINT_HELP " ] ; then
usage
usage
@ -134,14 +149,22 @@ cp -r "$srcdir"/* "$destdir"
rm -f " $destdir " /_header.html " $destdir " /_footer.html
rm -f " $destdir " /_header.html " $destdir " /_footer.html
# Generate an ordered (by open time) file list
# Generate an ordered (by open time) file list
find " $srcdir " -type f -name "*.md" -exec ls -1t { } + > " $filelist "
find " $srcdir " -type f -regex '.*\/[^_].+\..*' -exec ls -1t { } + |
sed -i " s,^\/*[^\/]*\/, $destdir /, " " $filelist "
awk '/.*\.(md|html)$/' > " $objlist "
sed -i -e " s,^\/*[^\/]*\/, $destdir /, " " $objlist "
# Convert markdown files
# Convert markdown files
while IFS = "" read -r file; do
while IFS = "" read -r file; do
convert " $file "
convert " $file "
done < " $filelist "
done < " $objlist "
sed -i 's/\.md$/\.html/' " $filelist "
# Convert the file list to a list that contains the path of all the html files
sed -i -e 's/\.md$/\.html/' " $objlist "
# Create a list that contains the links to all html files
linklist = linklist.tmp
cp -f " $objlist " " $linklist "
sed -i -e " s/^ $destdir // " -e " s/^/ $prefix :\/\/ $domain / " " $linklist "
# Prepare the header
# Prepare the header
if ! [ " $SKIP_HEADER " ] ; then
if ! [ " $SKIP_HEADER " ] ; then
@ -152,40 +175,35 @@ if ! [ "$SKIP_HEADER" ]; then
fi
fi
# Prepate the footer
# Prepate the footer
if ! [ " $SKIP_FOOTER " ] ; then
if ! [ " $SKIP_FOOTER " ] ; then
tmpfoot = "tmpfootfile.tmp"
tmpfoot = "tmpfootfile.tmp"
cp " $footerfile " " $tmpfoot "
cp " $footerfile " " $tmpfoot "
sed -i '1s/^/<footer>/' " $tmpfoot "
sed -i '1s/^/<footer>/' " $tmpfoot "
echo '</footer>' >> " $tmpfoot "
echo '</footer>' >> " $tmpfoot "
find " $destdir " -name "*.html" |
find " $destdir " -name "*.html" |
while IFS = "" read -r file; do
while IFS = "" read -r file; do
sed -i " /<\/body>/r $tmpfoot " " $file "
sed -i " /<\/body>/r $tmpfoot " " $file "
done
done
rm -f " $tmpfoot "
rm -f " $tmpfoot "
fi
fi
# Prepare the sitemap & file list
# Prepare index file list
if ! [ " $SKIP_SITEMAP " ] || ! [ " $SKIP_LIST " ] ; then
if ! [ " $SKIP_LIST " ] ; then
linklist = "linklist.tmp"
rm -f " $linklist " " $destdir " /sitemap.xml
while IFS = "" read -r file; do
echo " ${ file # $destdir / } " >> " $linklist "
done < " $filelist "
if ! [ " $SKIP_LIST " ] ; then
tmpfile = "linkindex.tmp"
tmpfile = "linkindex.tmp"
rm -f " $tmpfile "
rm -f " $tmpfile "
cat << EOF >> " $tmpfile "
cat << EOF >> " $tmpfile "
<div id = "map" >
<div id = "map" >
<h2 id = "Pages" >$linksec </h2>
<h2 id = "Pages" >$linksec </h2>
EOF
EOF
count = '0'
while IFS = "" read -r line; do
while IFS = "" read -r line; do
count = $(( count + 1 ))
if echo " $line " | grep -q 'index\.html' ; then
if echo " $line " | grep -q 'index\.html' ; then
continue
continue
fi
fi
tit le = " $( grep -e '^<title>.*<\/title>' " $destdir " /" $line " |
tf ile = " $( awk " NR== $count " " $objlist " ) "
sed -e 's/<title>//' -e 's/<\/title>//' ) "
title = " $( sed -E -n 's/.*<title>\s*(.+)\s*<\/title>.*/\1/p' " $tfile " ) "
if ! [ " $title " ] || echo " $title " | grep -q 'Untitled article' ; then
if [ -z " $title " ] ; then
title = ${ line %????? }
title = " ${ tfile ##*/ } "
fi
fi
printf "<p><a href=\"%s\">%s</a></p>\n" " $line " " $title " >> " $tmpfile "
printf "<p><a href=\"%s\">%s</a></p>\n" " $line " " $title " >> " $tmpfile "
done < " $linklist "
done < " $linklist "
@ -194,10 +212,10 @@ EOF
sed -i " /^REPLACE/r $tmpfile " " $destdir " /index.html
sed -i " /^REPLACE/r $tmpfile " " $destdir " /index.html
sed -i 's/^REPLACE//' " $destdir " /index.html
sed -i 's/^REPLACE//' " $destdir " /index.html
rm -f " $tmpfile "
rm -f " $tmpfile "
fi
fi
if ! [ " $SKIP_SITEMAP " ] ; then
# Generate sitemap
sed -i -e " s/^/ $prefix :\/\/ $url \// " " $linklist "
if ! [ " $SKIP_SITEMAP " ] ; then
cat << EOF >> " $destdir " /sitemap.xml
cat << EOF >> " $destdir " /sitemap.xml
<?xml version = "1.0" encoding = "UTF-8" ?>
<?xml version = "1.0" encoding = "UTF-8" ?>
<urlset xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9" >
<urlset xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9" >
@ -208,8 +226,45 @@ EOF
<url><loc>$line <\/ loc><\/ url>" " $destdir " /sitemap.xml
<url><loc>$line <\/ loc><\/ url>" " $destdir " /sitemap.xml
done < " $linklist "
done < " $linklist "
sed -i 's/^<url>/\t<url>/' " $destdir " /sitemap.xml
sed -i 's/^<url>/\t<url>/' " $destdir " /sitemap.xml
fi
# Generate atom feed
if ! [ " $SKIP_FEED " ] ; then
feed = " $destdir " /atom.xml
cat << EOF >> " $feed "
<?xml version = "1.0" encoding = "utf-8" ?>
<feed xmlns = "http://www.w3.org/2005/Atom" >
<title>$blog_title </title>
<subtitle>$blog_desc </subtitle>
<link href = " $url /atom.xml/ " rel = "self" />
<link href = " $url / " />
<id>$url </id>
<updated>$( date -Is) </updated>
EOF
count = '0'
while IFS = "" read -r line; do
count = $(( count + 1 ))
if [ $count -gt " $blog_nmax " ] ; then
break
fi
tfile = " $( awk " NR== $count " " $objlist " ) "
title = " $( sed -E -n 's/.*<title>\s*(.+)\s*<\/title>.*/\1/p' " $tfile " ) "
if [ -z " $title " ] ; then
title = " ${ tfile ##*/ } "
fi
fi
rm -f " $linklist " " $filelist "
ldate = " $( stat -c '%y' " $tfile " ) "
{
printf '\t<entry>\n'
printf '\t\t<title>%s</title>\n' " $title "
printf '\t\t<link href="%s" />\n' " $line "
printf '\t\t<updated>%s</updated>\n' " $ldate "
printf '\t</entry>\n'
} >> " $feed "
done < " $linklist "
printf '</feed>\n' >> " $feed "
fi
fi
rm -f " $objlist " " $linklist "
exit
exit