commit b2da39990730d139c43edc3850bed5044cf2a5ea
parent 3e841adac8c53a01d3fd4c1293929429ea2c18c6
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Fri, 19 Aug 2022 17:32:26 -0400
Add ability to build pages from any subdirectory
These changes allow one to build a page without having to go back up to
the root directory of the website. This should reduce deployment
friction when quickly iterating on a page or series of pages.
Diffstat:
M | sbs | | | 85 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------ |
M | sbs.1 | | | 8 | ++++---- |
2 files changed, 77 insertions(+), 16 deletions(-)
diff --git a/sbs b/sbs
@@ -9,12 +9,23 @@ set -o errexit # Halt processing if an error is encountered
set -o nounset # Do not allow the use of variables that haven't been set
if [ ! -x "$(command -v lowdown)" ]; then
- echo "The program 'lowdown' is needed but was not found."; exit 1
+ printf "Error: The program 'lowdown' is needed but was not found.\n"
+ exit 1
fi
# Create a new page, post, or site with the following skeleton content
new()
{
+ if [ "$#" -lt 2 ]; then
+ printf "Please provide a subcommand. See sbs(1) for documentation.\n"
+ exit 1
+ fi
+
+ if [ "$#" -lt 3 ]; then
+ printf "Please provide a path. See sbs(1) for documentation.\n"
+ exit 1
+ fi
+
if [ "$2" = "page" ]; then
{ printf "Title: \nSummary: \n\n"
printf "# [%%title]\n\n"
@@ -61,7 +72,7 @@ new()
> "$3/static/style.css"
printf "Created: %s\n" "$3"
else
- printf "Command '%s' not recognized.\n" "$2"
+ printf "Subcommand '%s' not recognized. See sbs(1) for documentation.\n" "$2"
exit 1
fi
exit 0
@@ -75,7 +86,7 @@ parse_configuration()
if [ -n "$value" ]; then
eval "$key='$value'"
else
- printf "Error: %s not configured.\n" "$key"
+ printf "Error: %s is not configured.\n" "$key"
exit 1
fi
done
@@ -138,17 +149,23 @@ genfeed() {
build()
{
for file in "$@"; do
- if [ -d "$file" ]; then
- build "$file"/*
- continue
- elif [ ! -f "$file" ]; then
- printf "ERROR: %s does not exist. " "$file"
+ # Stop the filename from being prepended with the path multiple
+ # times as build() recurses
+ if ! echo "$file" | grep -q "$cwd"; then
+ file="$cwd"/"$file"
+ fi
+ if [ ! -f "$file" ]; then
+ if [ -d "$file" ]; then
+ build "$file"/*
+ continue
+ fi
+ printf "Error: %s does not exist. " "$file"
printf "Are you sure you're in the right directory?\n"
- continue
+ exit 1
fi
fileName=$(basename "$file" .md)
- subDir=$(dirname "$file" | sed "s/^content//")
+ subDir=$(dirname "$file" | sed "s/.*\/content//")
mkdir -p "static/$subDir"
# Convert Gemtext files to Markdown
@@ -159,7 +176,7 @@ build()
# The third sed expression fixes gemini links that only had a
# URL so that the URL will be displayed as the link text in HTML
if [ "$(echo "$file" | awk -F\. '{print $NF}' )" = "gmi" ]; then
- printf "Converting: %s to markdown...\n" "$file"
+ printf "Converting: content%s/%s to markdown...\n" "$subDir" "$fileName"
fileName=$(basename "$file" .gmi)
sed -e 's/\(=>[ ]*\)\(.*\)\(.gmi\)\(.*\)/\1\2.html\4/g' \
-e 's/=>[ ]*\([^ ]*\)\( \|\)\(.*\)/[\3](\1)\n/g' \
@@ -195,20 +212,63 @@ push()
sh -c "$pushcmd"
}
+# Walks up the filesystem to the root of the website so it can be built from
+# within any subdirectory. Has the side effect of making path-parsing more
+# resilient.
+walk_back()
+{
+ # config.ini should be in the root of the website's folder structure
+ while [ ! -f config.ini ]; do
+ cd ..
+ if [ $(pwd) = "/" ]; then
+ printf "Error: Not inside of an sbs site directory.\n"
+ exit 1
+ fi
+ done
+
+ # Parse the config just for the siteName variable to ensure we're in the
+ # right place (in the root of the website's folder structure)
+ value=$(grep "siteName" config.ini | cut -d'=' -f2 | xargs)
+ if [ -n "$value" ]; then
+ eval "siteName='$value'"
+ else
+ printf "Error: siteName is not configured.\n"
+ exit 1
+ fi
+
+ # Check that we are in the root of the website's folder structure
+ if [ "$(basename $(pwd))" = "$siteName" ]; then
+ return 0
+ else
+ printf "Error: config.ini found but %s is not the root of the site.\n" "$(pwd)"
+ exit 1
+ fi
+}
+
+if [ "$#" -lt 1 ]; then
+ echo "Please provide a command. See sbs(1) for documentation."
+ exit 1
+fi
+
case "$1" in
"build")
shift
+ # Store the current directory so we know where we started
+ cwd="$(pwd)"
+ walk_back
parse_configuration
mkdir -p /tmp/sbs/
# Allows simply running "sbs build" without path(s)
if [ $# -eq 0 ]; then
- build content/*
+ cwd=""
+ build ./content/*
else
build "$@"
fi
rm -rf /tmp/sbs/
;;
"genfeed")
+ walk_back
parse_configuration
genfeed
;;
@@ -216,6 +276,7 @@ case "$1" in
new "$@"
;;
"push")
+ walk_back
parse_configuration
push
;;
diff --git a/sbs.1 b/sbs.1
@@ -68,7 +68,9 @@ options are as follows:
.It siteURL
The URL of the website's root page (e.g. https://www.example.com/)
.It siteName
-The name of the website (e.g. John Smith's Blog).
+The name of the website (e.g. John Smith's Blog). This must be the same as the
+directory of the website (i.e. what you specified when using the 'sbs new site'
+command).
.It blogDir
The directory under which all blog posts will be.
.It languageCode
@@ -93,9 +95,7 @@ To create a new site called "example.com":
.Pp
.Dl sbs new site example.com
.Pp
-All of the following commands must be run from within the root of a given site's
-directory (i.e. the example.com/ directory, not any subdirectory). The examples
-below assume the webmaster is currently inside of the newly created
+The examples below assume the webmaster is currently inside of the newly created
"example.com" directory and has edited the files and configuration generated by
sbs to their liking.
.Pp