From 796b66c3e078bdb7f1f7b2a3681cbc6b40942343 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Wed, 9 Sep 2020 00:37:08 +0200 Subject: [PATCH] first commit --- .gitignore | 2 + LICENSE | 21 ++++++++++ README.md | 12 ++++++ rivet | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100755 rivet diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a2a8fb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +tests/** +dst/** diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ee709b3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Alessandro Mauri + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..0e002e8 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# Rivet: static site genetator +River takes a source directory containing a the file sources and produces an +output directoru that can actually be uploaded to a server. + +## Dependencies +- [lowdown](https://kristaps.bsd.lv/lowdown/) +- POSIX sh + +## Similar tools + +- [saait](https://git.codemadness.org/saait/file/README.html) +- [ssg](https://www.romanzolotarev.com/ssg.html) diff --git a/rivet b/rivet new file mode 100755 index 0000000..97759a0 --- /dev/null +++ b/rivet @@ -0,0 +1,119 @@ +#!/bin/sh +set -e + +unset SKIP_FOOTER +unset SKIP_HEADER +unset SKIP_LIST +unset VERBOSE +unset PRINT_HELP + +usage() { + printf "Usage: rivet [-o destdir] [-hovelf] srcdir\n" + printf "\t-h: prints this message\n" + printf "\t-o [destdir]: specifies the output direcotory to be [destdir]\n" + printf "\t-v: makes the script verbose\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-l: skips the generation of .html file list\n" + exit 2 +} + +apply_header() { + tmpfile="tmpheadfile.tmp" + cp "$1" "$tmpfile" + sed "//r $headerfile" "$tmpfile" > "$1" + rm -f "$tmpfile" +} + +apply_footer() { + tmpfoot="tmpfooter.tmp" + tmpfile="tmpfootfile.tmp" + cp "$footerfile" "$tmpfoot" + sed -i '1s/^/' >> "$tmpfoot" + cp "$1" "$tmpfile" + sed "/<\/body/r $tmpfoot" "$tmpfile" > "$1" + rm -f "$tmpfile" "$tmpfoot" +} + +convert() { + tmpfile="tmpconvfile.tmp" + infile="$1" + outfile="${infile%???}.html" + cp "$1" "$tmpfile" + # TODO: convert links to .md to .html + lowdown -s -Thtml -o "$outfile" "$tmpfile" + rm -f "$tmpfile" "$1" +} + +# Check dependencies +if ! command -v lowdown > /dev/null; then + echo "lowdown is not installed" + exit +fi + +destdir='dst' +# Get arguments using getopts +# rivet [src] [-vhelf] [-o dir] +# -o [dir]: set the destination directory, defaults to "dst" +# -v: verbose, sets x so it prints every line +# -h: prints usage +# -e: skip prepend header +# -l: skip article list +# -f: skip append footer +while getopts 'o:vhelf' c +do + case "$c" in + o) destdir=$OPTARG ;; + v) VERBOSE=true ;; + h) PRINT_HELP=true ;; + e) SKIP_HEADER=true ;; + l) SKIP_LIST=true ;; + f) SKIP_FOOTER=true ;; + *) ;; + esac +done +shift $((OPTIND - 1)) +srcdir="$(basename "$1")" +headerfile=$srcdir/_header.html +footerfile=$srcdir/_footer.html + + +if [ "$PRINT_HELP" ]; then + usage +fi +if [ "$VERBOSE" ]; then + set -x +fi + +if ! [ -d "$srcdir" ]; then + echo "Error: missing source direcotry" + usage +fi + +mkdir -p "$destdir" +cp -r "$srcdir"/* "$destdir" +rm -f "$destdir"/_header.html "$destdir"/_footer.html + +# Convert markdown files +find "$destdir" -name "*.md" | +while IFS="" read -r file; do + convert "$file" +done + +# Prepend header +if ! [ "$SKIP_HEADER" ]; then + find "$destdir" -name "*.html" | + while IFS="" read -r file; do + apply_header "$file" + done +fi +# Append footer +if ! [ "$SKIP_FOOTER" ]; then + find "$destdir" -name "*.html" | + while IFS="" read -r file; do + apply_footer "$file" + done +fi + +exit