From 641a95de7bdd64963666cca6b96387ab5d9245e2 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 19 May 2021 10:06:02 +0100 Subject: some refactoring and also ensured that it works on macOS (given Bash 5 is installed) --- README.md | 5 +++-- buetow.org.sh | 54 +++++++++++++++++++++++++++------------------ content/md/index.md | 2 ++ packages/assert.source.sh | 15 +++++++++++++ packages/atomfeed.source.sh | 3 ++- packages/gemfeed.source.sh | 5 ++++- packages/generate.source.sh | 4 ++-- packages/html.source.sh | 2 +- packages/md.source.sh | 2 +- 9 files changed, 62 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 72bf9c43..fb8450b5 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ This is the source code of my Gemini capsule. You can reach the site(s) here: * Text editor: [Vim](https://www.vim.org) * Gemini server: [a-h/gemini](https://github.com/a-h/gemini) -* Some Bash scripting +* Some Bash scripting (GNU Bash probably 5.x or higher required) ## HTTP fallback software used @@ -23,6 +23,7 @@ This is the source code of my Gemini capsule. You can reach the site(s) here: These are things I want to do with this project in near future: -* Make it work on macOS, as it currently doesn't fully (Only ensure correct Bash version?) * Speed it up a bit: Make it so, that only changed .gmi files (or changed HTML templates) result into re-generating other formats. * Write a blog post about buetow.org.sh script +* Maybe put the ./content dirs into different git projects and then include them via submodules. This has the benefit that the GitHub page has the resulting index.md in the root folder. +* Custom alt.buetow.org domain for GitHub page. diff --git a/buetow.org.sh b/buetow.org.sh index fb5f45f8..57535b99 100755 --- a/buetow.org.sh +++ b/buetow.org.sh @@ -3,13 +3,16 @@ # The buetow.org.sh static site generator # by Paul Buetow 2021 -declare -r ARG=$1; shift +declare -r ARG="$1"; shift declare DATE=date declare SED=sed +declare GREP=grep which gdate &>/dev/null && DATE=gdate which gsed &>/dev/null && SED=gsed +which ggrep &>/dev/null && GREP=ggrep readonly DATE readonly SED +readonly GREP source buetow.org.conf source ./packages/assert.source.sh @@ -30,25 +33,32 @@ $0's possible arguments: HELPHERE } -case $ARG in - --test) - html::test - md::test - ;; - --feed) - gemfeed::generate - atomfeed::generate - ;; - --generate) - html::test - md::test - gemfeed::generate - atomfeed::generate - generate::fromgmi html md - ;; - --help|*) - help - ;; -esac +main () { + local -r arg="$1"; shift -exit 0 + case $ARG in + --test) + html::test + md::test + ;; + --feed) + gemfeed::generate + atomfeed::generate + ;; + --generate) + html::test + md::test + gemfeed::generate + atomfeed::generate + generate::fromgmi html md + ;; + --help|*) + help + ;; + esac + + return 0 +} + +main $ARG +exit $? diff --git a/content/md/index.md b/content/md/index.md index 90b90066..e32b5472 100644 --- a/content/md/index.md +++ b/content/md/index.md @@ -1,3 +1,5 @@ +# buetow.org + ``` ,---------------------------, | /---------------------\ | diff --git a/packages/assert.source.sh b/packages/assert.source.sh index c3ad0a6a..5d3a5c5d 100644 --- a/packages/assert.source.sh +++ b/packages/assert.source.sh @@ -30,3 +30,18 @@ assert::not_empty () { log VERBOSE "Result in $callee as expected not empty" } + +# Unit test for whether a given string matches a regex. +assert::matches () { + local -r name="$1"; shift + local -r content="$1"; shift + local -r regex="$1"; shift + local -r callee=${FUNCNAME[1]} + + if ! $GREP -q -E "$regex" <<< "$content"; then + log ERROR "In $callee expected '$name' to match '$regex'" + exit 2 + fi + + log VERBOSE "Matching in $callee as expected" +} diff --git a/packages/atomfeed.source.sh b/packages/atomfeed.source.sh index 6d5a8f47..f17b00fb 100644 --- a/packages/atomfeed.source.sh +++ b/packages/atomfeed.source.sh @@ -6,7 +6,7 @@ atomfeed::meta () { log VERBOSE "Generating meta info for post $gmi_file_path" local is_draft=no - if grep -E -q '\.draft\.meta$' <<< "$meta_file"; then + if $GREP -E -q '\.draft\.meta$' <<< "$meta_file"; then is_draft=yes fi @@ -75,6 +75,7 @@ ATOMHEADER while read -r gmi_file; do # Load cached meta information about the post. source <(atomfeed::meta "$gemfeed_dir/$gmi_file") + # Get HTML content for the feed local content="$(atomfeed::content "$gemfeed_dir/$gmi_file")" diff --git a/packages/gemfeed.source.sh b/packages/gemfeed.source.sh index 182d6eb0..33ea0c8d 100644 --- a/packages/gemfeed.source.sh +++ b/packages/gemfeed.source.sh @@ -4,7 +4,10 @@ gemfeed::get_posts () { local -r gmi_pattern='^[0-9]{4}-[0-9]{2}-[0-9]{2}-.*\.gmi$' local -r draft_pattern='\.draft\.gmi$' - ls "$gemfeed_dir" | grep -E "$gmi_pattern" | grep -E -v "$draft_pattern" | sort -r + ls "$gemfeed_dir" | + $GREP -E "$gmi_pattern" | + $GREP -E -v "$draft_pattern" | + sort -r } # Add the links from gemfeed/index.gmi to the main index site. diff --git a/packages/generate.source.sh b/packages/generate.source.sh index 248fe6c4..83bf69ee 100644 --- a/packages/generate.source.sh +++ b/packages/generate.source.sh @@ -15,7 +15,7 @@ generate::make_link () { fi done < <(echo "$line" | tr ' ' '\n') - if grep -E -q "$IMAGE_PATTERN" <<< "$link"; then + if $GREP -E -q "$IMAGE_PATTERN" <<< "$link"; then if [[ "$what" == md ]]; then md::make_img "$link" "$descr" else @@ -115,7 +115,7 @@ generate::fromgmi () { for format in "$@"; do generate::fromgmi_add_docs "$src" "$format" done - done < <(find "$CONTENT_DIR/gemtext" -type f | grep -E -v '(.gmi|atom.xml|.tmp)$') + done < <(find "$CONTENT_DIR/gemtext" -type f | $GREP -E -v '(.gmi|atom.xml|.tmp)$') log INFO "Added $num_doc_files other documents to each of $*" diff --git a/packages/html.source.sh b/packages/html.source.sh index cc79e612..5a292f1c 100644 --- a/packages/html.source.sh +++ b/packages/html.source.sh @@ -46,7 +46,7 @@ html::make_link () { local link="$1"; shift local descr="$1"; shift - grep -F -q '://' <<< "$link" || link=${link/.gmi/.html} + $GREP -F -q '://' <<< "$link" || link=${link/.gmi/.html} test -z "$descr" && descr="$link" echo "$descr
" } diff --git a/packages/md.source.sh b/packages/md.source.sh index 2f85b3c2..957b9cf3 100644 --- a/packages/md.source.sh +++ b/packages/md.source.sh @@ -15,7 +15,7 @@ md::make_link () { local link="$1"; shift local descr="$1"; shift - grep -F -q '://' <<< "$link" || link=${link/.gmi/.md} + $GREP -F -q '://' <<< "$link" || link=${link/.gmi/.md} test -z "$descr" && descr="$link" echo "[$descr]($link) " -- cgit v1.2.3