From 1079f927a27db9d194c8e25eb3a188396fdf8eab Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 17 May 2021 21:02:55 +0100 Subject: refactor code --- packages/assert.source.sh | 30 +++++++++ packages/atomfeed.source.sh | 118 +++++++++++++++++++++++++++++++++++ packages/gemfeed.source.sh | 51 ++++++++++++++++ packages/generate.source.sh | 128 ++++++++++++++++++++++++++++++++++++++ packages/html.source.sh | 145 ++++++++++++++++++++++++++++++++++++++++++++ packages/log.source.sh | 26 ++++++++ packages/md.source.sh | 55 +++++++++++++++++ 7 files changed, 553 insertions(+) create mode 100644 packages/assert.source.sh create mode 100644 packages/atomfeed.source.sh create mode 100644 packages/gemfeed.source.sh create mode 100644 packages/generate.source.sh create mode 100644 packages/html.source.sh create mode 100644 packages/log.source.sh create mode 100644 packages/md.source.sh (limited to 'packages') diff --git a/packages/assert.source.sh b/packages/assert.source.sh new file mode 100644 index 0000000..551d162 --- /dev/null +++ b/packages/assert.source.sh @@ -0,0 +1,30 @@ +assert::equals () { + local -r result="$1"; shift + local -r expected="$1"; shift + local -r callee=${FUNCNAME[1]} + + if [[ "$result" != "$expected" ]]; then + cat < "$atom_file.tmp" + + + $now + $DOMAIN feed + $SUBTITLE + + + gemini://$DOMAIN/ +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")" + + assert::not_empty meta_title "$meta_title" + assert::not_empty meta_date "$meta_date" + assert::not_empty meta_author "$meta_author" + assert::not_empty meta_email "$meta_email" + assert::not_empty meta_summary "$meta_summary" + assert::not_empty content "$content" + + cat <> "$atom_file.tmp" + + $meta_title + + gemini://$DOMAIN/gemfeed/$gmi_file + $meta_date + + $meta_author + $meta_email + + $meta_summary + +
+ $content +
+
+
+ATOMENTRY + done < <(gemfeed::get_posts | head -n $ATOM_MAX_ENTRIES) + + cat <> "$atom_file.tmp" +
+ATOMFOOTER + + # Delete the 3rd line of the atom feeds (global feed update timestamp) + if ! diff -u <($SED 3d "$atom_file") <($SED 3d "$atom_file.tmp"); then + log INFO 'Feed got something new!' + mv "$atom_file.tmp" "$atom_file" + test "$ADD_GIT" == yes && git add "$atom_file" + else + log INFO 'Nothing really new in the feed' + rm "$atom_file.tmp" + fi +} diff --git a/packages/gemfeed.source.sh b/packages/gemfeed.source.sh new file mode 100644 index 0000000..c68c507 --- /dev/null +++ b/packages/gemfeed.source.sh @@ -0,0 +1,51 @@ +# Filters out blog posts from other files in the gemfeed dir. +gemfeed::get_posts () { + local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" + 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 +} + +# Adds the links from gemfeed/index.gmi to the main index site. +gemfeed::updatemainindex () { + local -r index_gmi="$CONTENT_DIR/gemtext/index.gmi" + local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" + + log VERBOSE "Updating $index_gmi with posts from $gemfeed_dir" + + # Remove old gemfeeds from main index + $SED '/^=> .\/gemfeed\/[0-9].* - .*/d;' "$index_gmi" > "$index_gmi.tmp" + # Add current gemfeeds to main index + $SED -n '/^=> / { s| ./| ./gemfeed/|; p; }' "$gemfeed_dir/index.gmi" >> "$index_gmi.tmp" + + mv "$index_gmi.tmp" "$index_gmi" + test "$ADD_GIT" == yes && git add "$index_gmi" +} + +# This generates a index.gmi in the ./gemfeed subdir. +gemfeed::generate () { + local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" + log INFO "Generating Gemfeed index for $gemfeed_dir" + +cat < "$gemfeed_dir/index.gmi.tmp" +# $DOMAIN's Gemfeed + +## $SUBTITLE + +GEMFEED + + gemfeed::get_posts | while read -r gmi_file; do + # Extract first heading as post title. + local title=$($SED -n '/^# / { s/# //; p; q; }' "$gemfeed_dir/$gmi_file" | tr '"' "'") + # Extract the date from the file name. + local filename_date=$(basename "$gemfeed_dir/$gmi_file" | cut -d- -f1,2,3) + + echo "=> ./$gmi_file $filename_date - $title" >> "$gemfeed_dir/index.gmi.tmp" + done + + mv "$gemfeed_dir/index.gmi.tmp" "$gemfeed_dir/index.gmi" + test "$ADD_GIT" == yes && git add "$gemfeed_dir/index.gmi" + + gemfeed::updatemainindex +} diff --git a/packages/generate.source.sh b/packages/generate.source.sh new file mode 100644 index 0000000..0f07af5 --- /dev/null +++ b/packages/generate.source.sh @@ -0,0 +1,128 @@ +generate::make_link () { + local -r what="$1"; shift + local -r line="${1/=> }"; shift + local link + local descr + + while read -r token; do + if [ -z "$link" ]; then + link="$token" + elif [ -z "$descr" ]; then + descr="$token" + else + descr="$descr $token" + fi + done < <(echo "$line" | tr ' ' '\n') + + if grep -E -q "$IMAGE_PATTERN" <<< "$link"; then + if [[ "$what" == md ]]; then + md::make_img "$link" "$descr" + else + html::make_img "$link" "$(html::special "$descr")" + fi + return + fi + + if [[ "$what" == md ]]; then + md::make_link "$link" "$descr" + else + html::make_link "$link" "$(html::special "$descr")" + fi +} + +generate::fromgmi_ () { + local -r src="$1"; shift + local -r format="$1"; shift + local dest=${src/gemtext/$format} + dest=${dest/.gmi/.$format} + local dest_dir=$(dirname "$dest") + + test ! -d "$dest_dir" && mkdir -p "$dest_dir" + if [[ "$format" == html ]]; then + cat header.html.part > "$dest.tmp" + html::fromgmi < "$src" >> "$dest.tmp" + cat footer.html.part >> "$dest.tmp" + elif [[ "$format" == md ]]; then + md::fromgmi < "$src" >> "$dest.tmp" + fi + + mv "$dest.tmp" "$dest" + test "$ADD_GIT" == yes && git add "$dest" +} + +generate::fromgmi_add_docs () { + local -r src="$1"; shift + local -r format="$1"; shift + local -r dest=${src/gemtext/$format} + local -r dest_dir=$(dirname "$dest") + + test ! -d "$dest_dir" && mkdir -p "$dest_dir" + cp "$src" "$dest" + test "$ADD_GIT" == yes && git add "$dest" +} + +generate::convert_gmi_atom_to_html_atom () { + local -r format="$1"; shift + test "$format" != html && return + + log INFO 'Converting Gemtext Atom feed to HTML Atom feed' + + $SED 's|.gmi|.html|g; s|gemini://|https://|g' \ + < $CONTENT_DIR/gemtext/gemfeed/atom.xml \ + > $CONTENT_DIR/html/gemfeed/atom.xml + + test "$ADD_GIT" == yes && git add "$CONTENT_DIR/html/gemfeed/atom.xml" +} + +generate::fromgmi_cleanup () { + local -r src="$1"; shift + local -r format="$1"; shift + local dest=${src/.$format/.gmi} + dest=${dest/$format/gemtext} + + test ! -f "$dest" && test "$ADD_GIT" == yes && git rm "$src" +} + +generate::fromgmi () { + local -i num_gmi_files=0 + local -i num_doc_files=0 + + log INFO "Generating $* from Gemtext" + + while read -r src; do + (( num_gmi_files++ )) + for format in "$@"; do + generate::fromgmi_ "$src" "$format" + done + done < <(find "$CONTENT_DIR/gemtext" -type f -name \*.gmi) + + log INFO "Converted $num_gmi_files Gemtext files" + + # Add non-.gmi files to html dir. + log VERBOSE "Adding other docs to $*" + + while read -r src; do + (( num_doc_files++ )) + 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)$') + + log INFO "Added $num_doc_files other documents to each of $*" + + # Add atom feed for HTML + for format in "$@"; do + generate::convert_gmi_atom_to_html_atom "$format" + done + + # Remove obsolete files from ./html/ + for format in "$@"; do + find "$CONTENT_DIR/$format" -type f | while read -r src; do + generate::fromgmi_cleanup "$src" "$format" + done + done + + for format in "$@"; do + log INFO "$format can be found in $CONTENT_DIR/$format now" + done +} diff --git a/packages/html.source.sh b/packages/html.source.sh new file mode 100644 index 0000000..d8d2fc6 --- /dev/null +++ b/packages/html.source.sh @@ -0,0 +1,145 @@ +html::special () { + $SED ' + s|\&|\&|g; + s|<|\<|g; + s|>|\>|g; + ' <<< "$@" +} + +html::make_paragraph () { + local -r text="$1"; shift + test -n "$text" && echo "

$(html::special "$text")

" +} + +html::make_heading () { + local -r text=$($SED -E 's/^#+ //' <<< "$1"); shift + local -r level="$1"; shift + echo "$(html::special "$text")" +} + +html::make_quote () { + local -r quote="${1/> }" + echo "

$(html::special "$quote")

" +} + +html::make_img () { + local link="$1"; shift + local descr="$1"; shift + + if [ -z "$descr" ]; then + echo -n "" + else + echo -n "$descr:" + echo -n "\"$descr\"" + fi + + echo "
" +} + +html::make_link () { + local link="$1"; shift + local descr="$1"; shift + + grep -F -q '://' <<< "$link" || link=${link/.gmi/.html} + test -z "$descr" && descr="$link" + echo "$descr
" +} + +html::fromgmi () { + local is_list=no + local is_plain=no + + while IFS='' read -r line; do + if [[ "$is_list" == yes ]]; then + if [[ "$line" == '* '* ]]; then + echo "
  • $(html::special "${line/\* /}")
  • " + else + is_list=no + echo "" + fi + continue + + elif [[ "$is_plain" == yes ]]; then + if [[ "$line" == '```'* ]]; then + echo "" + is_plain=no + else + html::special "$line" + fi + continue + fi + + case "$line" in + '* '*) + is_list=yes + echo "
      " + echo "
    • ${line/\* /}
    • " + ;; + '```'*) + is_plain=yes + echo "
      "
      +                ;;
      +            '# '*)
      +                html::make_heading "$line" 1
      +                ;;
      +            '## '*)
      +                html::make_heading "$line" 2
      +                ;;
      +            '### '*)
      +                html::make_heading "$line" 3
      +                ;;
      +            '> '*)
      +                html::make_quote "$line"
      +                ;;
      +            '=> '*)
      +                generate::make_link html "$line"
      +                ;;
      +            *)
      +                html::make_paragraph "$line"
      +                ;;
      +        esac
      +    done
      +}
      +
      +html::test () {
      +    local line='Hello world! This is a paragraph.'
      +    assert::equals "$(html::make_paragraph "$line")" '

      Hello world! This is a paragraph.

      ' + + line='' + assert::equals "$(html::make_paragraph "$line")" '' + + line='Foo &<>& Bar!' + assert::equals "$(html::make_paragraph "$line")" '

      Foo &<>& Bar!

      ' + + line='# Header 1' + assert::equals "$(html::make_heading "$line" 1)" '

      Header 1

      ' + + line='## Header 2' + assert::equals "$(html::make_heading "$line" 2)" '

      Header 2

      ' + + line='### Header 3' + assert::equals "$(html::make_heading "$line" 3)" '

      Header 3

      ' + + line='> This is a quote' + assert::equals "$(html::make_quote "$line")" '

      This is a quote

      ' + + line='=> https://example.org' + assert::equals "$(generate::make_link html "$line")" \ + 'https://example.org
      ' + + line='=> index.gmi' + assert::equals "$(generate::make_link html "$line")" \ + 'index.html
      ' + + line='=> http://example.org Description of the link' + assert::equals "$(generate::make_link html "$line")" \ + 'Description of the link
      ' + + line='=> http://example.org/image.png' + assert::equals "$(generate::make_link html "$line")" \ + '
      ' + + line='=> http://example.org/image.png Image description' + assert::equals "$(generate::make_link html "$line")" \ + 'Image description:Image description
      ' +} diff --git a/packages/log.source.sh b/packages/log.source.sh new file mode 100644 index 0000000..55d693e --- /dev/null +++ b/packages/log.source.sh @@ -0,0 +1,26 @@ +log () { + local -r level="$1"; shift + + for message in "$@"; do + echo "$message" + done | log::_pipe "$level" +} + +log::pipe () { + log::_pipe "$1" +} + +log::_pipe () { + local -r level="$1"; shift + + if [[ "$level" == VERBOSE && -z "$LOG_VERBOSE" ]]; then + return + fi + + local -r callee=${FUNCNAME[2]} + local -r stamp=$($DATE +%Y%m%d-%H%M%S) + + while read -r line; do + echo "$level|$stamp|$callee|$line" >&2 + done +} diff --git a/packages/md.source.sh b/packages/md.source.sh new file mode 100644 index 0000000..197bdcf --- /dev/null +++ b/packages/md.source.sh @@ -0,0 +1,55 @@ +md::make_img () { + local link="$1"; shift + local descr="$1"; shift + + if [ -z "$descr" ]; then + echo "[![$link]($link)]($link) " + else + echo "[![$descr]($link \"$descr\")]($link) " + fi +} + +md::make_link () { + local link="$1"; shift + local descr="$1"; shift + + grep -F -q '://' <<< "$link" || link=${link/.gmi/.md} + test -z "$descr" && descr="$link" + + echo "[$descr]($link) " +} + +md::test () { + local line='=> https://example.org' + assert::equals "$(generate::make_link md "$line")" \ + '[https://example.org](https://example.org) ' + + line='=> index.md' + assert::equals "$(generate::make_link md "$line")" \ + '[index.md](index.md) ' + + line='=> http://example.org Description of the link' + assert::equals "$(generate::make_link md "$line")" \ + '[Description of the link](http://example.org) ' + + line='=> http://example.org/image.png' + assert::equals "$(generate::make_link md "$line")" \ + '[![http://example.org/image.png](http://example.org/image.png)](http://example.org/image.png) ' + + line='=> http://example.org/image.png Image description' + assert::equals "$(generate::make_link md "$line")" \ + '[![Image description](http://example.org/image.png "Image description")](http://example.org/image.png) ' +} + +md::fromgmi () { + while IFS='' read -r line; do + case "$line" in + '=> '*) + generate::make_link md "$line" + ;; + *) + echo "$line" + ;; + esac + done +} -- cgit v1.2.3 From f6deb276c7c5e7b3617d9ecd35193b43ae32ccbf Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 17 May 2021 21:19:35 +0100 Subject: refactor --- packages/assert.source.sh | 2 ++ packages/atomfeed.source.sh | 3 +++ packages/generate.source.sh | 34 ++++++++++++++++++++-------------- packages/html.source.sh | 20 ++++++++++++++------ packages/log.source.sh | 3 +++ packages/md.source.sh | 30 +++++++++++++++++------------- 6 files changed, 59 insertions(+), 33 deletions(-) (limited to 'packages') diff --git a/packages/assert.source.sh b/packages/assert.source.sh index 551d162..c3ad0a6 100644 --- a/packages/assert.source.sh +++ b/packages/assert.source.sh @@ -1,3 +1,4 @@ +# Unit test for whether 2 given strings equal. assert::equals () { local -r result="$1"; shift local -r expected="$1"; shift @@ -16,6 +17,7 @@ ERROR log VERBOSE "Result in $callee as expected: '$expected'" } +# Unit test for whether a given string is not empty. assert::not_empty () { local -r name="$1"; shift local -r content="$1"; shift diff --git a/packages/atomfeed.source.sh b/packages/atomfeed.source.sh index feb074a..3a3e1e7 100644 --- a/packages/atomfeed.source.sh +++ b/packages/atomfeed.source.sh @@ -1,3 +1,4 @@ +# Retrieves meta data of a given blog post. Generate new meta info if not yet exists. atomfeed::meta () { local -r gmi_file_path="$1"; shift local -r meta_file=$($SED 's|gemtext|meta|; s|.gmi$|.meta|;' <<< "$gmi_file_path") @@ -36,6 +37,7 @@ META test $is_draft == yes && rm "$meta_file" } +# Retrieves the core content as XHTML of the blog post, to include to the feed. atomfeed::content () { local -r gmi_file_path="$1"; shift log VERBOSE "Retrieving feed content from $gmi_file_path" @@ -50,6 +52,7 @@ atomfeed::content () { " } +# Generate an atom.xml feed file. atomfeed::generate () { local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" local -r atom_file="$gemfeed_dir/atom.xml" diff --git a/packages/generate.source.sh b/packages/generate.source.sh index 0f07af5..67a9b7e 100644 --- a/packages/generate.source.sh +++ b/packages/generate.source.sh @@ -1,3 +1,4 @@ +# Generates a HTML or Markdown link from given Gemtext link. generate::make_link () { local -r what="$1"; shift local -r line="${1/=> }"; shift @@ -18,7 +19,7 @@ generate::make_link () { if [[ "$what" == md ]]; then md::make_img "$link" "$descr" else - html::make_img "$link" "$(html::special "$descr")" + html::make_img "$link" "$(html::encode "$descr")" fi return fi @@ -26,11 +27,12 @@ generate::make_link () { if [[ "$what" == md ]]; then md::make_link "$link" "$descr" else - html::make_link "$link" "$(html::special "$descr")" + html::make_link "$link" "$(html::encode "$descr")" fi } -generate::fromgmi_ () { +# Internal helper function for generate::fromgmi +generate::_fromgmi () { local -r src="$1"; shift local -r format="$1"; shift local dest=${src/gemtext/$format} @@ -50,6 +52,7 @@ generate::fromgmi_ () { test "$ADD_GIT" == yes && git add "$dest" } +# Adds other docs (e.g. images, videos) from Gemtext to output format. generate::fromgmi_add_docs () { local -r src="$1"; shift local -r format="$1"; shift @@ -61,6 +64,17 @@ generate::fromgmi_add_docs () { test "$ADD_GIT" == yes && git add "$dest" } +# Remove docs from output format which aren't present in Gemtext anymore. +generate::fromgmi_cleanup_docs () { + local -r src="$1"; shift + local -r format="$1"; shift + local dest=${src/.$format/.gmi} + dest=${dest/$format/gemtext} + + test ! -f "$dest" && test "$ADD_GIT" == yes && git rm "$src" +} + +# Converts the Gemtext Atom feed to a HTML Atom feed. generate::convert_gmi_atom_to_html_atom () { local -r format="$1"; shift test "$format" != html && return @@ -74,15 +88,7 @@ generate::convert_gmi_atom_to_html_atom () { test "$ADD_GIT" == yes && git add "$CONTENT_DIR/html/gemfeed/atom.xml" } -generate::fromgmi_cleanup () { - local -r src="$1"; shift - local -r format="$1"; shift - local dest=${src/.$format/.gmi} - dest=${dest/$format/gemtext} - - test ! -f "$dest" && test "$ADD_GIT" == yes && git rm "$src" -} - +# Generate a given output format from a Gemtext file. generate::fromgmi () { local -i num_gmi_files=0 local -i num_doc_files=0 @@ -92,7 +98,7 @@ generate::fromgmi () { while read -r src; do (( num_gmi_files++ )) for format in "$@"; do - generate::fromgmi_ "$src" "$format" + generate::_fromgmi "$src" "$format" done done < <(find "$CONTENT_DIR/gemtext" -type f -name \*.gmi) @@ -118,7 +124,7 @@ generate::fromgmi () { # Remove obsolete files from ./html/ for format in "$@"; do find "$CONTENT_DIR/$format" -type f | while read -r src; do - generate::fromgmi_cleanup "$src" "$format" + generate::fromgmi_cleanup_docs "$src" "$format" done done diff --git a/packages/html.source.sh b/packages/html.source.sh index d8d2fc6..d295c7a 100644 --- a/packages/html.source.sh +++ b/packages/html.source.sh @@ -1,4 +1,5 @@ -html::special () { +# Convert special characters to their HTML codes +html::encode () { $SED ' s|\&|\&|g; s|<|\<|g; @@ -6,22 +7,26 @@ html::special () { ' <<< "$@" } +# Make a HTML paragraph. html::make_paragraph () { local -r text="$1"; shift - test -n "$text" && echo "

      $(html::special "$text")

      " + test -n "$text" && echo "

      $(html::encode "$text")

      " } +# Make a HTML header. html::make_heading () { local -r text=$($SED -E 's/^#+ //' <<< "$1"); shift local -r level="$1"; shift - echo "$(html::special "$text")" + echo "$(html::encode "$text")" } +# Make a HTML quotation html::make_quote () { local -r quote="${1/> }" - echo "

      $(html::special "$quote")

      " + echo "

      $(html::encode "$quote")

      " } +# Make a HTML image html::make_img () { local link="$1"; shift local descr="$1"; shift @@ -36,6 +41,7 @@ html::make_img () { echo "
      " } +# Make a HTML hyperlink html::make_link () { local link="$1"; shift local descr="$1"; shift @@ -45,6 +51,7 @@ html::make_link () { echo "$descr
      " } +## Convert Gemtext to HTML html::fromgmi () { local is_list=no local is_plain=no @@ -52,7 +59,7 @@ html::fromgmi () { while IFS='' read -r line; do if [[ "$is_list" == yes ]]; then if [[ "$line" == '* '* ]]; then - echo "
    • $(html::special "${line/\* /}")
    • " + echo "
    • $(html::encode "${line/\* /}")
    • " else is_list=no echo "
    " @@ -64,7 +71,7 @@ html::fromgmi () { echo "" is_plain=no else - html::special "$line" + html::encode "$line" fi continue fi @@ -101,6 +108,7 @@ html::fromgmi () { done } +## Test HTML package. html::test () { local line='Hello world! This is a paragraph.' assert::equals "$(html::make_paragraph "$line")" '

    Hello world! This is a paragraph.

    ' diff --git a/packages/log.source.sh b/packages/log.source.sh index 55d693e..2c6e1b3 100644 --- a/packages/log.source.sh +++ b/packages/log.source.sh @@ -1,3 +1,4 @@ +# Log a message. log () { local -r level="$1"; shift @@ -6,10 +7,12 @@ log () { done | log::_pipe "$level" } +# Log a stream through a pipe. log::pipe () { log::_pipe "$1" } +# Internal log implementation. log::_pipe () { local -r level="$1"; shift diff --git a/packages/md.source.sh b/packages/md.source.sh index 197bdcf..2f85b3c 100644 --- a/packages/md.source.sh +++ b/packages/md.source.sh @@ -1,3 +1,4 @@ +# Make a Markdown image. md::make_img () { local link="$1"; shift local descr="$1"; shift @@ -9,6 +10,7 @@ md::make_img () { fi } +# Make a Markdown hyperlink. md::make_link () { local link="$1"; shift local descr="$1"; shift @@ -19,6 +21,21 @@ md::make_link () { echo "[$descr]($link) " } +# Convert Gemtext to Markdown. +md::fromgmi () { + while IFS='' read -r line; do + case "$line" in + '=> '*) + generate::make_link md "$line" + ;; + *) + echo "$line" + ;; + esac + done +} + +# Test the Markdown package. md::test () { local line='=> https://example.org' assert::equals "$(generate::make_link md "$line")" \ @@ -40,16 +57,3 @@ md::test () { assert::equals "$(generate::make_link md "$line")" \ '[![Image description](http://example.org/image.png "Image description")](http://example.org/image.png) ' } - -md::fromgmi () { - while IFS='' read -r line; do - case "$line" in - '=> '*) - generate::make_link md "$line" - ;; - *) - echo "$line" - ;; - esac - done -} -- cgit v1.2.3 From 6aee04354e443e2121bf6c558be227388a10e7b2 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 17 May 2021 21:23:06 +0100 Subject: reword comments --- packages/atomfeed.source.sh | 4 ++-- packages/gemfeed.source.sh | 6 +++--- packages/generate.source.sh | 48 ++++++++++++++++++++++----------------------- packages/html.source.sh | 4 ++-- 4 files changed, 31 insertions(+), 31 deletions(-) (limited to 'packages') diff --git a/packages/atomfeed.source.sh b/packages/atomfeed.source.sh index 3a3e1e7..6d5a8f4 100644 --- a/packages/atomfeed.source.sh +++ b/packages/atomfeed.source.sh @@ -1,4 +1,4 @@ -# Retrieves meta data of a given blog post. Generate new meta info if not yet exists. +# Retrieve meta data of a given blog post. Generate new meta info if not yet exists. atomfeed::meta () { local -r gmi_file_path="$1"; shift local -r meta_file=$($SED 's|gemtext|meta|; s|.gmi$|.meta|;' <<< "$gmi_file_path") @@ -37,7 +37,7 @@ META test $is_draft == yes && rm "$meta_file" } -# Retrieves the core content as XHTML of the blog post, to include to the feed. +# Retrieve the core content as XHTML of the blog post. atomfeed::content () { local -r gmi_file_path="$1"; shift log VERBOSE "Retrieving feed content from $gmi_file_path" diff --git a/packages/gemfeed.source.sh b/packages/gemfeed.source.sh index c68c507..182d6eb 100644 --- a/packages/gemfeed.source.sh +++ b/packages/gemfeed.source.sh @@ -1,4 +1,4 @@ -# Filters out blog posts from other files in the gemfeed dir. +# Filter out blog posts from other files in the gemfeed dir. gemfeed::get_posts () { local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" local -r gmi_pattern='^[0-9]{4}-[0-9]{2}-[0-9]{2}-.*\.gmi$' @@ -7,7 +7,7 @@ gemfeed::get_posts () { ls "$gemfeed_dir" | grep -E "$gmi_pattern" | grep -E -v "$draft_pattern" | sort -r } -# Adds the links from gemfeed/index.gmi to the main index site. +# Add the links from gemfeed/index.gmi to the main index site. gemfeed::updatemainindex () { local -r index_gmi="$CONTENT_DIR/gemtext/index.gmi" local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" @@ -23,7 +23,7 @@ gemfeed::updatemainindex () { test "$ADD_GIT" == yes && git add "$index_gmi" } -# This generates a index.gmi in the ./gemfeed subdir. +# Generate a index.gmi in the ./gemfeed subdir. gemfeed::generate () { local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" log INFO "Generating Gemfeed index for $gemfeed_dir" diff --git a/packages/generate.source.sh b/packages/generate.source.sh index 67a9b7e..e74d299 100644 --- a/packages/generate.source.sh +++ b/packages/generate.source.sh @@ -1,4 +1,4 @@ -# Generates a HTML or Markdown link from given Gemtext link. +# Generate a HTML or Markdown link from given Gemtext link. generate::make_link () { local -r what="$1"; shift local -r line="${1/=> }"; shift @@ -31,28 +31,7 @@ generate::make_link () { fi } -# Internal helper function for generate::fromgmi -generate::_fromgmi () { - local -r src="$1"; shift - local -r format="$1"; shift - local dest=${src/gemtext/$format} - dest=${dest/.gmi/.$format} - local dest_dir=$(dirname "$dest") - - test ! -d "$dest_dir" && mkdir -p "$dest_dir" - if [[ "$format" == html ]]; then - cat header.html.part > "$dest.tmp" - html::fromgmi < "$src" >> "$dest.tmp" - cat footer.html.part >> "$dest.tmp" - elif [[ "$format" == md ]]; then - md::fromgmi < "$src" >> "$dest.tmp" - fi - - mv "$dest.tmp" "$dest" - test "$ADD_GIT" == yes && git add "$dest" -} - -# Adds other docs (e.g. images, videos) from Gemtext to output format. +# Add other docs (e.g. images, videos) from Gemtext to output format. generate::fromgmi_add_docs () { local -r src="$1"; shift local -r format="$1"; shift @@ -74,7 +53,7 @@ generate::fromgmi_cleanup_docs () { test ! -f "$dest" && test "$ADD_GIT" == yes && git rm "$src" } -# Converts the Gemtext Atom feed to a HTML Atom feed. +# Convert the Gemtext Atom feed to a HTML Atom feed. generate::convert_gmi_atom_to_html_atom () { local -r format="$1"; shift test "$format" != html && return @@ -88,6 +67,27 @@ generate::convert_gmi_atom_to_html_atom () { test "$ADD_GIT" == yes && git add "$CONTENT_DIR/html/gemfeed/atom.xml" } +# Internal helper function for generate::fromgmi +generate::_fromgmi () { + local -r src="$1"; shift + local -r format="$1"; shift + local dest=${src/gemtext/$format} + dest=${dest/.gmi/.$format} + local dest_dir=$(dirname "$dest") + + test ! -d "$dest_dir" && mkdir -p "$dest_dir" + if [[ "$format" == html ]]; then + cat header.html.part > "$dest.tmp" + html::fromgmi < "$src" >> "$dest.tmp" + cat footer.html.part >> "$dest.tmp" + elif [[ "$format" == md ]]; then + md::fromgmi < "$src" >> "$dest.tmp" + fi + + mv "$dest.tmp" "$dest" + test "$ADD_GIT" == yes && git add "$dest" +} + # Generate a given output format from a Gemtext file. generate::fromgmi () { local -i num_gmi_files=0 diff --git a/packages/html.source.sh b/packages/html.source.sh index d295c7a..cc79e61 100644 --- a/packages/html.source.sh +++ b/packages/html.source.sh @@ -51,7 +51,7 @@ html::make_link () { echo "$descr
    " } -## Convert Gemtext to HTML +# Convert Gemtext to HTML html::fromgmi () { local is_list=no local is_plain=no @@ -108,7 +108,7 @@ html::fromgmi () { done } -## Test HTML package. +# Test HTML package. html::test () { local line='Hello world! This is a paragraph.' assert::equals "$(html::make_paragraph "$line")" '

    Hello world! This is a paragraph.

    ' -- cgit v1.2.3 From d371050f2fb79b6d66a810e60ff9c0ed2381f2cc Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 18 May 2021 08:50:24 +0100 Subject: dynamic html title --- packages/generate.source.sh | 3 +++ 1 file changed, 3 insertions(+) (limited to 'packages') diff --git a/packages/generate.source.sh b/packages/generate.source.sh index e74d299..248fe6c 100644 --- a/packages/generate.source.sh +++ b/packages/generate.source.sh @@ -84,6 +84,9 @@ generate::_fromgmi () { md::fromgmi < "$src" >> "$dest.tmp" fi + local title=$($SED -n '/^# / { s/# //; p; q; }' "$src" | tr '"' "'") + test -z "title" && title=$SUBTITLE + $SED -i "s|%%TITLE%%|$title|g" "$dest.tmp" mv "$dest.tmp" "$dest" test "$ADD_GIT" == yes && git add "$dest" } -- cgit v1.2.3 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) --- 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 +- 6 files changed, 25 insertions(+), 6 deletions(-) (limited to 'packages') diff --git a/packages/assert.source.sh b/packages/assert.source.sh index c3ad0a6..5d3a5c5 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 6d5a8f4..f17b00f 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 182d6eb..33ea0c8 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 248fe6c..83bf69e 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 cc79e61..5a292f1 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 2f85b3c..957b9cf 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 From afd9fdf7ca0800c457959b540c4378d20f802dc0 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 19 May 2021 19:46:20 +0100 Subject: add git package --- packages/atomfeed.source.sh | 6 +++--- packages/gemfeed.source.sh | 12 ++++++------ packages/generate.source.sh | 25 ++++++++++++++----------- packages/git.source.sh | 31 +++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 packages/git.source.sh (limited to 'packages') diff --git a/packages/atomfeed.source.sh b/packages/atomfeed.source.sh index f17b00f..4bfc60c 100644 --- a/packages/atomfeed.source.sh +++ b/packages/atomfeed.source.sh @@ -29,7 +29,7 @@ local meta_email="$EMAIL" local meta_title="$title" local meta_summary="$summary. .....to read on please visit my site." META - test $is_draft == no && git add "$meta_file" + test $is_draft == no && git::add meta "$meta_file" return fi @@ -54,7 +54,7 @@ atomfeed::content () { # Generate an atom.xml feed file. atomfeed::generate () { - local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" + local -r gemfeed_dir="$CONTENT_BASE_DIR/gemtext/gemfeed" local -r atom_file="$gemfeed_dir/atom.xml" local -r now=$($DATE --iso-8601=seconds) log INFO "Generating Atom feed to $atom_file" @@ -114,7 +114,7 @@ ATOMFOOTER if ! diff -u <($SED 3d "$atom_file") <($SED 3d "$atom_file.tmp"); then log INFO 'Feed got something new!' mv "$atom_file.tmp" "$atom_file" - test "$ADD_GIT" == yes && git add "$atom_file" + test "$USE_GIT" == yes && git::add gemtext "$atom_file" else log INFO 'Nothing really new in the feed' rm "$atom_file.tmp" diff --git a/packages/gemfeed.source.sh b/packages/gemfeed.source.sh index 33ea0c8..bc91eaf 100644 --- a/packages/gemfeed.source.sh +++ b/packages/gemfeed.source.sh @@ -1,6 +1,6 @@ # Filter out blog posts from other files in the gemfeed dir. gemfeed::get_posts () { - local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" + local -r gemfeed_dir="$CONTENT_BASE_DIR/gemtext/gemfeed" local -r gmi_pattern='^[0-9]{4}-[0-9]{2}-[0-9]{2}-.*\.gmi$' local -r draft_pattern='\.draft\.gmi$' @@ -12,8 +12,8 @@ gemfeed::get_posts () { # Add the links from gemfeed/index.gmi to the main index site. gemfeed::updatemainindex () { - local -r index_gmi="$CONTENT_DIR/gemtext/index.gmi" - local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" + local -r index_gmi="$CONTENT_BASE_DIR/gemtext/index.gmi" + local -r gemfeed_dir="$CONTENT_BASE_DIR/gemtext/gemfeed" log VERBOSE "Updating $index_gmi with posts from $gemfeed_dir" @@ -23,12 +23,12 @@ gemfeed::updatemainindex () { $SED -n '/^=> / { s| ./| ./gemfeed/|; p; }' "$gemfeed_dir/index.gmi" >> "$index_gmi.tmp" mv "$index_gmi.tmp" "$index_gmi" - test "$ADD_GIT" == yes && git add "$index_gmi" + test "$USE_GIT" == yes && git::add gemtext "$index_gmi" } # Generate a index.gmi in the ./gemfeed subdir. gemfeed::generate () { - local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" + local -r gemfeed_dir="$CONTENT_BASE_DIR/gemtext/gemfeed" log INFO "Generating Gemfeed index for $gemfeed_dir" cat < "$gemfeed_dir/index.gmi.tmp" @@ -48,7 +48,7 @@ GEMFEED done mv "$gemfeed_dir/index.gmi.tmp" "$gemfeed_dir/index.gmi" - test "$ADD_GIT" == yes && git add "$gemfeed_dir/index.gmi" + test "$USE_GIT" == yes && git::add gemtext "$gemfeed_dir/index.gmi" gemfeed::updatemainindex } diff --git a/packages/generate.source.sh b/packages/generate.source.sh index 83bf69e..25c292f 100644 --- a/packages/generate.source.sh +++ b/packages/generate.source.sh @@ -40,7 +40,7 @@ generate::fromgmi_add_docs () { test ! -d "$dest_dir" && mkdir -p "$dest_dir" cp "$src" "$dest" - test "$ADD_GIT" == yes && git add "$dest" + test "$USE_GIT" == yes && git::add "$format" "$dest" } # Remove docs from output format which aren't present in Gemtext anymore. @@ -50,7 +50,7 @@ generate::fromgmi_cleanup_docs () { local dest=${src/.$format/.gmi} dest=${dest/$format/gemtext} - test ! -f "$dest" && test "$ADD_GIT" == yes && git rm "$src" + test ! -f "$dest" && test "$USE_GIT" == yes && git::rm "$format" "$src" } # Convert the Gemtext Atom feed to a HTML Atom feed. @@ -61,10 +61,10 @@ generate::convert_gmi_atom_to_html_atom () { log INFO 'Converting Gemtext Atom feed to HTML Atom feed' $SED 's|.gmi|.html|g; s|gemini://|https://|g' \ - < $CONTENT_DIR/gemtext/gemfeed/atom.xml \ - > $CONTENT_DIR/html/gemfeed/atom.xml + < $CONTENT_BASE_DIR/gemtext/gemfeed/atom.xml \ + > $CONTENT_BASE_DIR/html/gemfeed/atom.xml - test "$ADD_GIT" == yes && git add "$CONTENT_DIR/html/gemfeed/atom.xml" + test "$USE_GIT" == yes && git::add "$format" "$CONTENT_BASE_DIR/html/gemfeed/atom.xml" } # Internal helper function for generate::fromgmi @@ -88,7 +88,7 @@ generate::_fromgmi () { test -z "title" && title=$SUBTITLE $SED -i "s|%%TITLE%%|$title|g" "$dest.tmp" mv "$dest.tmp" "$dest" - test "$ADD_GIT" == yes && git add "$dest" + test "$USE_GIT" == yes && git::add "$format" "$dest" } # Generate a given output format from a Gemtext file. @@ -103,7 +103,7 @@ generate::fromgmi () { for format in "$@"; do generate::_fromgmi "$src" "$format" done - done < <(find "$CONTENT_DIR/gemtext" -type f -name \*.gmi) + done < <(find "$CONTENT_BASE_DIR/gemtext" -type f -name \*.gmi) log INFO "Converted $num_gmi_files Gemtext files" @@ -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_BASE_DIR/gemtext" -type f | $GREP -E -v '(\.git.*|\.gmi|atom.xml|\.tmp)$') log INFO "Added $num_doc_files other documents to each of $*" @@ -124,14 +124,17 @@ generate::fromgmi () { generate::convert_gmi_atom_to_html_atom "$format" done - # Remove obsolete files from ./html/ + # Remove obsolete files from ./html/. + # Note: The _config.yml is the config file for GitHub pages (md format). for format in "$@"; do - find "$CONTENT_DIR/$format" -type f | while read -r src; do + find "$CONTENT_BASE_DIR/$format" -type f | + $GREP -E -v '(\.git.*|_config.yml)$'| + while read -r src; do generate::fromgmi_cleanup_docs "$src" "$format" done done for format in "$@"; do - log INFO "$format can be found in $CONTENT_DIR/$format now" + log INFO "$format can be found in $CONTENT_BASE_DIR/$format now" done } diff --git a/packages/git.source.sh b/packages/git.source.sh new file mode 100644 index 0000000..1484e93 --- /dev/null +++ b/packages/git.source.sh @@ -0,0 +1,31 @@ +# Add a static content file to git +git::add () { + local -r content_dir="$CONTENT_BASE_DIR/$1"; shift + local file="$1"; shift + file=${file/$content_dir/} + + cd $content_dir + echo git add $file + cd - +} + +# Remove a static content file from git +git::rm () { + local -r content_dir="$CONTENT_BASE_DIR/$1"; shift + local file="$1"; shift + file=${file/$content_dir/} + + cd $content_dir + echo git rm $file + cd - +} + +# Commit all changes +git::commit () { + local -r content_dir="$CONTENT_BASE_DIR/$1"; shift + local -r message="$1"; shift + + cd $content_dir + echo git commit -a -m "$message" + cd - +} -- cgit v1.2.3 From 14822623456972b0dbe9dc5b6c36b701bb6ec7f1 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 19 May 2021 20:01:47 +0100 Subject: Removal of Makefile and some fixes/refactorings --- packages/assert.source.sh | 12 ++++++++++++ packages/generate.source.sh | 7 +++++++ packages/git.source.sh | 22 +++++++++++----------- 3 files changed, 30 insertions(+), 11 deletions(-) (limited to 'packages') diff --git a/packages/assert.source.sh b/packages/assert.source.sh index 5d3a5c5..0c7157f 100644 --- a/packages/assert.source.sh +++ b/packages/assert.source.sh @@ -45,3 +45,15 @@ assert::matches () { log VERBOSE "Matching in $callee as expected" } + +# Checks if all the Bash scripts here are good. +assert::shellcheck () { + set -e + shellcheck \ + --norc \ + --external-sources \ + --check-sourced \ + --exclude=SC2155,SC2010,SC2154,SC1090,SC2012 \ + ./"$0" + set +e +} diff --git a/packages/generate.source.sh b/packages/generate.source.sh index 25c292f..b28d9e8 100644 --- a/packages/generate.source.sh +++ b/packages/generate.source.sh @@ -134,7 +134,14 @@ generate::fromgmi () { done done + if [[ -z "$GIT_COMMIT_MESSAGE" ]]; then + GIT_COMMIT_MESSAGE='Publishing new version' + fi for format in "$@"; do + git::commit "$format" "$GIT_COMMIT_MESSAGE" log INFO "$format can be found in $CONTENT_BASE_DIR/$format now" done + + git::commit gemtext "$GIT_COMMIT_MESSAGE" + git::commit meta "$GIT_COMMIT_MESSAGE" } diff --git a/packages/git.source.sh b/packages/git.source.sh index 1484e93..ec8e853 100644 --- a/packages/git.source.sh +++ b/packages/git.source.sh @@ -2,22 +2,22 @@ git::add () { local -r content_dir="$CONTENT_BASE_DIR/$1"; shift local file="$1"; shift - file=${file/$content_dir/} + file=${file/$content_dir/.\/} - cd $content_dir - echo git add $file - cd - + cd "$content_dir" &>/dev/null + git add "$file" + cd - &>/dev/null } # Remove a static content file from git git::rm () { local -r content_dir="$CONTENT_BASE_DIR/$1"; shift local file="$1"; shift - file=${file/$content_dir/} + file=${file/$content_dir/.\/} - cd $content_dir - echo git rm $file - cd - + cd "$content_dir" &>/dev/null + git rm "$file" + cd - &>/dev/null } # Commit all changes @@ -25,7 +25,7 @@ git::commit () { local -r content_dir="$CONTENT_BASE_DIR/$1"; shift local -r message="$1"; shift - cd $content_dir - echo git commit -a -m "$message" - cd - + cd "$content_dir" &>/dev/null + git commit -a -m "$message" + cd - &>/dev/null } -- cgit v1.2.3 From 202f4e03fb9519931142fcd3d76700aeb106a05d Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 19 May 2021 20:10:19 +0100 Subject: fix --- packages/generate.source.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'packages') diff --git a/packages/generate.source.sh b/packages/generate.source.sh index b28d9e8..910d250 100644 --- a/packages/generate.source.sh +++ b/packages/generate.source.sh @@ -134,14 +134,16 @@ generate::fromgmi () { done done - if [[ -z "$GIT_COMMIT_MESSAGE" ]]; then - GIT_COMMIT_MESSAGE='Publishing new version' + if [[ "$USE_GIT" == yes ]]; then + if [[ -z "$GIT_COMMIT_MESSAGE" ]]; then + GIT_COMMIT_MESSAGE='Publishing new version' + fi + git::commit gemtext "$GIT_COMMIT_MESSAGE" + git::commit meta "$GIT_COMMIT_MESSAGE" fi for format in "$@"; do - git::commit "$format" "$GIT_COMMIT_MESSAGE" + test "$USE_GIT" == yes && git::commit "$format" "$GIT_COMMIT_MESSAGE" log INFO "$format can be found in $CONTENT_BASE_DIR/$format now" done - git::commit gemtext "$GIT_COMMIT_MESSAGE" - git::commit meta "$GIT_COMMIT_MESSAGE" } -- cgit v1.2.3 From d354a3ffb999c27ece66f8a66dfb416e5d5d7b6c Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 19 May 2021 20:19:52 +0100 Subject: add GIT_PUSH --- packages/git.source.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'packages') diff --git a/packages/git.source.sh b/packages/git.source.sh index ec8e853..9ee238c 100644 --- a/packages/git.source.sh +++ b/packages/git.source.sh @@ -20,6 +20,20 @@ git::rm () { cd - &>/dev/null } +# Commit all changes +git::commit () { + local -r content_dir="$CONTENT_BASE_DIR/$1"; shift + local -r message="$1"; shift + + cd "$content_dir" &>/dev/null + git commit -a -m "$message" + if [[ "$GIT_PUSH" == yes ]]; then + git pull + git push + fi + cd - &>/dev/null +} + # Commit all changes git::commit () { local -r content_dir="$CONTENT_BASE_DIR/$1"; shift -- cgit v1.2.3 From d88be863b7dcabe42b8d5b47b40ed2cb620f287c Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 19 May 2021 20:21:25 +0100 Subject: retab --- packages/assert.source.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'packages') diff --git a/packages/assert.source.sh b/packages/assert.source.sh index 0c7157f..d53a728 100644 --- a/packages/assert.source.sh +++ b/packages/assert.source.sh @@ -49,11 +49,11 @@ assert::matches () { # Checks if all the Bash scripts here are good. assert::shellcheck () { set -e - shellcheck \ - --norc \ - --external-sources \ - --check-sourced \ - --exclude=SC2155,SC2010,SC2154,SC1090,SC2012 \ - ./"$0" - set +e + shellcheck \ + --norc \ + --external-sources \ + --check-sourced \ + --exclude=SC2155,SC2010,SC2154,SC1090,SC2012 \ + ./"$0" + set +e } -- cgit v1.2.3 From 6bdd5afc634199926df3467baf512f34688df55d Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 19 May 2021 21:59:38 +0100 Subject: better documentation --- packages/generate.source.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'packages') diff --git a/packages/generate.source.sh b/packages/generate.source.sh index 910d250..0e1f2e2 100644 --- a/packages/generate.source.sh +++ b/packages/generate.source.sh @@ -76,10 +76,12 @@ generate::_fromgmi () { local dest_dir=$(dirname "$dest") test ! -d "$dest_dir" && mkdir -p "$dest_dir" + if [[ "$format" == html ]]; then - cat header.html.part > "$dest.tmp" + cat "$HTML_HEADER" > "$dest.tmp" html::fromgmi < "$src" >> "$dest.tmp" - cat footer.html.part >> "$dest.tmp" + cat "$HTML_FOOTER" >> "$dest.tmp" + elif [[ "$format" == md ]]; then md::fromgmi < "$src" >> "$dest.tmp" fi @@ -88,6 +90,7 @@ generate::_fromgmi () { test -z "title" && title=$SUBTITLE $SED -i "s|%%TITLE%%|$title|g" "$dest.tmp" mv "$dest.tmp" "$dest" + test "$USE_GIT" == yes && git::add "$format" "$dest" } @@ -141,9 +144,9 @@ generate::fromgmi () { git::commit gemtext "$GIT_COMMIT_MESSAGE" git::commit meta "$GIT_COMMIT_MESSAGE" fi + for format in "$@"; do test "$USE_GIT" == yes && git::commit "$format" "$GIT_COMMIT_MESSAGE" log INFO "$format can be found in $CONTENT_BASE_DIR/$format now" done - } -- cgit v1.2.3 From 61aeed196b412ca0c57a8b7a52593215752f1e37 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 20 May 2021 08:40:59 +0100 Subject: some log bugfixing, also add another alternate config file path --- packages/git.source.sh | 13 +++---------- packages/log.source.sh | 1 + 2 files changed, 4 insertions(+), 10 deletions(-) (limited to 'packages') diff --git a/packages/git.source.sh b/packages/git.source.sh index 9ee238c..ca1f475 100644 --- a/packages/git.source.sh +++ b/packages/git.source.sh @@ -26,20 +26,13 @@ git::commit () { local -r message="$1"; shift cd "$content_dir" &>/dev/null + set +e git commit -a -m "$message" if [[ "$GIT_PUSH" == yes ]]; then + log INFO "Invoking git pull/push in $content_dir" git pull git push fi - cd - &>/dev/null -} - -# Commit all changes -git::commit () { - local -r content_dir="$CONTENT_BASE_DIR/$1"; shift - local -r message="$1"; shift - - cd "$content_dir" &>/dev/null - git commit -a -m "$message" + set -e cd - &>/dev/null } diff --git a/packages/log.source.sh b/packages/log.source.sh index 2c6e1b3..56c6587 100644 --- a/packages/log.source.sh +++ b/packages/log.source.sh @@ -1,6 +1,7 @@ # Log a message. log () { local -r level="$1"; shift + local message for message in "$@"; do echo "$message" -- cgit v1.2.3 From 65f8a2497bad58619d0cfdc76e4a0efeb15e0306 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 20 May 2021 17:51:07 +0100 Subject: initial macos support --- packages/atomfeed.source.sh | 14 ++++++++++---- packages/gemfeed.source.sh | 4 ++-- packages/generate.source.sh | 42 +++++++++++++++++++++++++----------------- packages/git.source.sh | 12 ++++++++++++ packages/html.source.sh | 14 +++++++++++--- packages/md.source.sh | 8 ++++++-- 6 files changed, 66 insertions(+), 28 deletions(-) (limited to 'packages') diff --git a/packages/atomfeed.source.sh b/packages/atomfeed.source.sh index 4bfc60c..e1ee406 100644 --- a/packages/atomfeed.source.sh +++ b/packages/atomfeed.source.sh @@ -11,7 +11,9 @@ atomfeed::meta () { fi local -r meta_dir=$(dirname "$meta_file") - test ! -d "$meta_dir" && mkdir -p "$meta_dir" + if [[ ! -d "$meta_dir" ]]; then + mkdir -p "$meta_dir" + fi if [ ! -f "$meta_file" ]; then # Extract first heading as post title. @@ -29,12 +31,16 @@ local meta_email="$EMAIL" local meta_title="$title" local meta_summary="$summary. .....to read on please visit my site." META - test $is_draft == no && git::add meta "$meta_file" + if [[ $is_draft == no ]]; then + git::add meta "$meta_file" + fi return fi cat "$meta_file" - test $is_draft == yes && rm "$meta_file" + if [[ $is_draft == yes ]]; then + rm "$meta_file" + fi } # Retrieve the core content as XHTML of the blog post. @@ -114,7 +120,7 @@ ATOMFOOTER if ! diff -u <($SED 3d "$atom_file") <($SED 3d "$atom_file.tmp"); then log INFO 'Feed got something new!' mv "$atom_file.tmp" "$atom_file" - test "$USE_GIT" == yes && git::add gemtext "$atom_file" + git::add gemtext "$atom_file" else log INFO 'Nothing really new in the feed' rm "$atom_file.tmp" diff --git a/packages/gemfeed.source.sh b/packages/gemfeed.source.sh index bc91eaf..c842bb1 100644 --- a/packages/gemfeed.source.sh +++ b/packages/gemfeed.source.sh @@ -23,7 +23,7 @@ gemfeed::updatemainindex () { $SED -n '/^=> / { s| ./| ./gemfeed/|; p; }' "$gemfeed_dir/index.gmi" >> "$index_gmi.tmp" mv "$index_gmi.tmp" "$index_gmi" - test "$USE_GIT" == yes && git::add gemtext "$index_gmi" + git::add gemtext "$index_gmi" } # Generate a index.gmi in the ./gemfeed subdir. @@ -48,7 +48,7 @@ GEMFEED done mv "$gemfeed_dir/index.gmi.tmp" "$gemfeed_dir/index.gmi" - test "$USE_GIT" == yes && git::add gemtext "$gemfeed_dir/index.gmi" + git::add gemtext "$gemfeed_dir/index.gmi" gemfeed::updatemainindex } diff --git a/packages/generate.source.sh b/packages/generate.source.sh index 0e1f2e2..98cd215 100644 --- a/packages/generate.source.sh +++ b/packages/generate.source.sh @@ -38,9 +38,11 @@ generate::fromgmi_add_docs () { local -r dest=${src/gemtext/$format} local -r dest_dir=$(dirname "$dest") - test ! -d "$dest_dir" && mkdir -p "$dest_dir" + if [[ ! -d "$dest_dir" ]]; then + mkdir -p "$dest_dir" + fi cp "$src" "$dest" - test "$USE_GIT" == yes && git::add "$format" "$dest" + git::add "$format" "$dest" } # Remove docs from output format which aren't present in Gemtext anymore. @@ -50,13 +52,17 @@ generate::fromgmi_cleanup_docs () { local dest=${src/.$format/.gmi} dest=${dest/$format/gemtext} - test ! -f "$dest" && test "$USE_GIT" == yes && git::rm "$format" "$src" + if [[ ! -f "$dest" ]]; then + git::rm "$format" "$src" + fi } # Convert the Gemtext Atom feed to a HTML Atom feed. generate::convert_gmi_atom_to_html_atom () { local -r format="$1"; shift - test "$format" != html && return + if [[ "$format" != html ]]; then + return + fi log INFO 'Converting Gemtext Atom feed to HTML Atom feed' @@ -64,7 +70,7 @@ generate::convert_gmi_atom_to_html_atom () { < $CONTENT_BASE_DIR/gemtext/gemfeed/atom.xml \ > $CONTENT_BASE_DIR/html/gemfeed/atom.xml - test "$USE_GIT" == yes && git::add "$format" "$CONTENT_BASE_DIR/html/gemfeed/atom.xml" + git::add "$format" "$CONTENT_BASE_DIR/html/gemfeed/atom.xml" } # Internal helper function for generate::fromgmi @@ -75,7 +81,9 @@ generate::_fromgmi () { dest=${dest/.gmi/.$format} local dest_dir=$(dirname "$dest") - test ! -d "$dest_dir" && mkdir -p "$dest_dir" + if [[ ! -d "$dest_dir" ]]; then + mkdir -p "$dest_dir" + fi if [[ "$format" == html ]]; then cat "$HTML_HEADER" > "$dest.tmp" @@ -87,11 +95,13 @@ generate::_fromgmi () { fi local title=$($SED -n '/^# / { s/# //; p; q; }' "$src" | tr '"' "'") - test -z "title" && title=$SUBTITLE + if [[ -z "$title" ]]; then + title=$SUBTITLE + fi $SED -i "s|%%TITLE%%|$title|g" "$dest.tmp" mv "$dest.tmp" "$dest" - test "$USE_GIT" == yes && git::add "$format" "$dest" + git::add "$format" "$dest" } # Generate a given output format from a Gemtext file. @@ -102,7 +112,7 @@ generate::fromgmi () { log INFO "Generating $* from Gemtext" while read -r src; do - (( num_gmi_files++ )) + num_gmi_files=$(( num_gmi_files + 1 )) for format in "$@"; do generate::_fromgmi "$src" "$format" done @@ -114,7 +124,7 @@ generate::fromgmi () { log VERBOSE "Adding other docs to $*" while read -r src; do - (( num_doc_files++ )) + num_doc_files=$(( num_doc_files + 1 )) for format in "$@"; do generate::fromgmi_add_docs "$src" "$format" done @@ -137,16 +147,14 @@ generate::fromgmi () { done done - if [[ "$USE_GIT" == yes ]]; then - if [[ -z "$GIT_COMMIT_MESSAGE" ]]; then - GIT_COMMIT_MESSAGE='Publishing new version' - fi - git::commit gemtext "$GIT_COMMIT_MESSAGE" - git::commit meta "$GIT_COMMIT_MESSAGE" + if [[ -z "$GIT_COMMIT_MESSAGE" ]]; then + GIT_COMMIT_MESSAGE='Publishing new version' fi + git::commit gemtext "$GIT_COMMIT_MESSAGE" + git::commit meta "$GIT_COMMIT_MESSAGE" for format in "$@"; do - test "$USE_GIT" == yes && git::commit "$format" "$GIT_COMMIT_MESSAGE" + git::commit "$format" "$GIT_COMMIT_MESSAGE" log INFO "$format can be found in $CONTENT_BASE_DIR/$format now" done } diff --git a/packages/git.source.sh b/packages/git.source.sh index ca1f475..c502b00 100644 --- a/packages/git.source.sh +++ b/packages/git.source.sh @@ -1,5 +1,9 @@ # Add a static content file to git git::add () { + if [[ "$USE_GIT" != yes ]]; then + return + fi + local -r content_dir="$CONTENT_BASE_DIR/$1"; shift local file="$1"; shift file=${file/$content_dir/.\/} @@ -11,6 +15,10 @@ git::add () { # Remove a static content file from git git::rm () { + if [[ "$USE_GIT" != yes ]]; then + return + fi + local -r content_dir="$CONTENT_BASE_DIR/$1"; shift local file="$1"; shift file=${file/$content_dir/.\/} @@ -22,6 +30,10 @@ git::rm () { # Commit all changes git::commit () { + if [[ "$USE_GIT" != yes ]]; then + return + fi + local -r content_dir="$CONTENT_BASE_DIR/$1"; shift local -r message="$1"; shift diff --git a/packages/html.source.sh b/packages/html.source.sh index 5a292f1..a258781 100644 --- a/packages/html.source.sh +++ b/packages/html.source.sh @@ -10,7 +10,10 @@ html::encode () { # Make a HTML paragraph. html::make_paragraph () { local -r text="$1"; shift - test -n "$text" && echo "

    $(html::encode "$text")

    " + + if [[ -n "$text" ]]; then + echo "

    $(html::encode "$text")

    " + fi } # Make a HTML header. @@ -46,8 +49,13 @@ html::make_link () { local link="$1"; shift local descr="$1"; shift - $GREP -F -q '://' <<< "$link" || link=${link/.gmi/.html} - test -z "$descr" && descr="$link" + if $GREP -F -q '://' <<< "$link"; then + link=${link/.gmi/.html} + fi + if [[ -z "$descr" ]]; then + descr="$link" + fi + echo "$descr
    " } diff --git a/packages/md.source.sh b/packages/md.source.sh index 957b9cf..ce190dd 100644 --- a/packages/md.source.sh +++ b/packages/md.source.sh @@ -15,8 +15,12 @@ md::make_link () { local link="$1"; shift local descr="$1"; shift - $GREP -F -q '://' <<< "$link" || link=${link/.gmi/.md} - test -z "$descr" && descr="$link" + if $GREP -F -q '://' <<< "$link"; then + link=${link/.gmi/.md} + fi + if [[ -z "$descr" ]]; then + descr="$link" + fi echo "[$descr]($link) " } -- cgit v1.2.3 From 5eba1760c0db9b01f7498a1efbfb4d969b6f3051 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 20 May 2021 18:08:36 +0100 Subject: works now on macOS --- packages/html.source.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages') diff --git a/packages/html.source.sh b/packages/html.source.sh index a258781..efb9bf6 100644 --- a/packages/html.source.sh +++ b/packages/html.source.sh @@ -143,7 +143,7 @@ html::test () { assert::equals "$(generate::make_link html "$line")" \ 'https://example.org
    ' - line='=> index.gmi' + li