From 52e9c3aeea18d5750fea8704711a113adae93903 Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 12 Jun 2023 03:57:18 +0100 Subject: create example site where every page is executed to generate itself --- src/md2html.sh | 19 +++++++++++++++--- src/shblg | 33 +++++++++++++++++++++++++++---- src/test.md | 61 ---------------------------------------------------------- src/test.sh | 18 +++++++++++++++++ 4 files changed, 63 insertions(+), 68 deletions(-) mode change 100644 => 100755 src/shblg delete mode 100644 src/test.md create mode 100755 src/test.sh (limited to 'src') diff --git a/src/md2html.sh b/src/md2html.sh index 590cdaa..846498a 100755 --- a/src/md2html.sh +++ b/src/md2html.sh @@ -6,6 +6,19 @@ cat () { while IFS= read -r line; do printf "%s\n" "$line"; done < "$1" } +# remove a shebang from the start of the file +_remove_shebang () { + IFS= read -r line + case "$line" in + "#!"*) ;; + *) printf "%s\n" "$line" + esac + + while IFS= read -r line; do + printf "%s\n" "$line" + done +} + # remove traling whitespace from empty lines # _pre_strip () { @@ -396,7 +409,8 @@ _squash () { # convert the markdown from stdin into html # md2html () { - _pre_strip \ + _remove_shebang \ + | _pre_strip \ | _code \ | _pre_emph \ | _blockquote \ @@ -414,8 +428,7 @@ md2html () { | _h 3 \ | _h 2 \ | _h 1 \ - | _squash \ - | _html + | _squash } [ -z "$*" ] \ diff --git a/src/shblg b/src/shblg old mode 100644 new mode 100755 index ff10250..226216c --- a/src/shblg +++ b/src/shblg @@ -7,7 +7,9 @@ PAGE_TEMPLATE=blog/template.html while getopts ":o:i:t:" opt; do case "$opt" in o) - OUTPUT_DIR=$(realpath $OPTARG) + OUTPUT_DIR=$OPTARG/ + mkdir -p $OUTPUT_DIR + OUTPUT_DIR=$(realpath $OUTPUT_DIR) ;; i) INPUT_DIR=$(realpath $OPTARG) @@ -18,6 +20,29 @@ while getopts ":o:i:t:" opt; do esac done -for f in ${INPUT_DIR}/*.md; do - md2html "$f" > ${OUTPUT_DIR}/$f -done +# process a file to +process () { + path="${1#$INPUT_DIR}" + dirpath="${1%${1##*/}}" + out_file="${OUTPUT_DIR}${path}" + + [ -d "$1" ] && { + mkdir -p "$out_file" + for f in "$1"/*; do + process "$f" + done + return 0 + } || [ -x "$1" ] && { + # execute the file + cd $dirpath + "$1" > "${out_file%.*}.html" + cd - + return 0 + } || { + # just output the file as is + while IFS= read -r line; do printf "%s\n" "$line"; done < "$1" > "$out_file" + return 0 + } +} + +process "$INPUT_DIR" diff --git a/src/test.md b/src/test.md deleted file mode 100644 index 203d425..0000000 --- a/src/test.md +++ /dev/null @@ -1,61 +0,0 @@ -# This is a test md file hello - -This is *italics* this is **bold** this is ***both*** wow (this is in brackets ssh) and [this is in square brackets not a anchor lol] - -click [here](http) for stuff and [over here](http12 "my title") for more - -and [click here](http://this_has_stuffinside) too - -![this is an image](httpsomething) - -this is a paragraph -with many lines -that are joined together - -> this is a quote hi - -ok that was a quote - -> this quote has a list inside it -> - this is a list in a quote -> - it was quoted -> -> and also there is another quote; ->> Hi i am a quote - - -haha - -- this is a list - - of items - - please dont -- break - -ok - - int main() { - printf("hello %s\n", "world"); - } - - int func(int* a) { - return a; - } - -- list - - sublist - - subsublist - - subsubsublist - - -* star list -* this is a star - -+ plus now wow -+ plus wow so cool - -1. hello -2. world -3. lOL - - -> ok that worked? diff --git a/src/test.sh b/src/test.sh new file mode 100755 index 0000000..84c5405 --- /dev/null +++ b/src/test.sh @@ -0,0 +1,18 @@ +#!/bin/sh + + +line="hell o world" +# replace tabs with spaces +l="$line" +line= +while [ "$l" ]; do + c="${l%*${l#?}}" + case "$c" in + "\t") line="$line ";; + *) line="$line$c" ;; + esac + l="${l#?}" + printf "%s\n" "$c" +done +printf "%s\n" "$line" + -- cgit v1.2.1