#!/bin/sh DOCUMENTATION=$(cat <'. The meta block is deemed ended on the first line consisting of exactly ''. Each line of the meta block specifies an option, followed by an '=' and then the value. The value may contain whitespace, but there may not be whitespace between the option and the '='. Valid options are: language - Page language in ISO format. Eg. 'en' for English. title - Self explanatory. subtitle - Goes underneath the title. description - The description to be used for social media embeds. image - The image to be used for social media embeds. (Should be a URL.) date-published - Self explanatory. date-updated - Self explanatory. has - Optional components to be loaded. redirect-to - Optional, contains a path for a redirect. If present no page content will be shown. Sample meta block:
title=Why HTML is Better Than Markdown date-published=5 February 2020 date-updated=2 March 2021 has=code latex
USAGE: format.sh [OPTIONS] Where the OPTIONS are: --help Outputs this manual-style help message and immediately exits. --location LOCATION Set the location to LOCATION, which is assumed to be relative to the base url. Used mainly for meta tags. It should include a leading '/'. --version Outputs the script name along with its version number. The program will exit with one of four exit codes: 0 - if the program has been templated successfully, 1 - catastrophic failure, 2 - if no meta block has been found, and so this program effectively acted the same as the 'cat' command, 3 - if the meta block is badly formed (likely because the div was never ended, and 4 - if an invalid meta block option was encountered. Requirements: As far as I can tell, this script uses only features required in a POSIX compliant shell. Hacking hint: most of the modifications to this system will be done to this file, specifically in the 'Templates' section. SPDX-FileCopyrightText: Copyright (c) 2022, Theodore Preduta. SPDX-License-Identifier: BSD-2-Clause END_OF_DOCUMENTATION ) usage() { [ -n "$1" ] && echo "$1" && echo echo "Usage: format.sh [OPTIONS]" echo "Try running with --help for more information." exit 1 } location="/404/" while [ ! "$*" = "" ]; do case "$1" in --help) echo "$DOCUMENTATION" exit 0;; --location) location="$2" shift 2;; --version) echo "$DOCUMENTATION" | head -n 1 exit 0;; *) usage "Unkown argument: $1";; esac done has() { if test "${has_line#*$1}" != "$has_line" then return 0 else return 1 fi } ####################### # (Default) Variables # ####################### base_url="https://www.pta.gg" language="en" title="Unset Title" subtitle="" description="" image="" date_published="" date_updated="" has_line="" redirect_to="" ############# # Templates # ############# head() { cat < EOF has "code" && cat < EOF has "latex" && cat < EOF cat < $title EOF [ -n "$description" ] && cat < EOF # TODO do this only if image is actually provided as an absolute # link. [ -n "$image" ] && cat < EOF } header() { echo "

$title

" if [ -n "$subtitle" ]; then echo "

$subtitle

" fi cat < Skip Navigation Home About Projects EOF } prebody() { if [ -n "$date_published" ] || [ -n "$date_updated" ]; then printf "

" if [ -n "$date_published" ]; then printf "Published %s" "$date_published" if [ -n "$date_updated" ]; then printf "; " fi fi if [ -n "$date_updated" ]; then printf "Updated %s" "$date_updated" fi echo ".

" fi } postbody() { true # Unused } footer() { cat < Main Site Version Control Licenses Enable Chaos

© 2021-2024, Theodore Preduta. All rights reserved.

EOF } foot() { cat < EOF has "code" && cat < EOF has "latex" && cat < EOF } ############### # Main render # ############### read -r line # If the first line is not opening a meta block, we assume the # input is a standalone webpage and just vomit it back out. if [ ! "$line" = "
" ]; then echo "$line" cat exit 2 fi # Attempt to parse the meta block. while read -r line do if [ "$line" = "
" ]; then break; fi if [ "$line" = "" ]; then continue; fi var_part="${line%%=*}" val_part="${line#*=}" case "$var_part" in language) language="$val_part" ;; title) title="$val_part" ;; subtitle) subtitle="$val_part" ;; description) description="$val_part" ;; image) image="$val_part" ;; date-published) date_published="$val_part" ;; date-updated) date_updated="$val_part" ;; has) has_line="$val_part" ;; redirect-to) redirect_to="$val_part" ;; *) exit 3;; # Unknown option esac done # The meta block has not been fully read. if [ ! "$line" = "" ]; then exit 3; fi # Handle redirects. if [ ! "$redirect_to" = "" ]; then echo "" echo "" echo "" echo "" echo "" echo "" echo "Redirect" echo "" echo "" echo "

Redirecting here...

" echo "" echo "" exit 0 fi # Template the rest. Failures past this line needs to be debugged # with a browser. Hopefully(tm) there is an obvious visual error. echo "" echo "" echo "" head echo "" echo "" echo "
" header echo "
" echo "
" prebody cat # Body postbody echo "
" echo "" foot echo "" echo ""