From 52e9c3aeea18d5750fea8704711a113adae93903 Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 12 Jun 2023 03:57:18 +0100 Subject: [PATCH 1/2] create example site where every page is executed to generate itself --- example/entries/entries.sh | 45 +++++++++++++++++++++++++++++++++ example/entries/entry1.md | 13 ++++++++++ example/entries/entry2.md | 9 +++++++ src/test.md => example/index.md | 2 ++ src/md2html.sh | 19 +++++++++++--- src/shblg | 33 +++++++++++++++++++++--- src/test.sh | 18 +++++++++++++ 7 files changed, 132 insertions(+), 7 deletions(-) create mode 100755 example/entries/entries.sh create mode 100755 example/entries/entry1.md create mode 100755 example/entries/entry2.md rename src/test.md => example/index.md (97%) mode change 100644 => 100755 mode change 100644 => 100755 src/shblg create mode 100755 src/test.sh diff --git a/example/entries/entries.sh b/example/entries/entries.sh new file mode 100755 index 0000000..0479d96 --- /dev/null +++ b/example/entries/entries.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +# add a special header to all entries +cat << EOF + + + + + $1 + + +

my blog

+EOF + +# make this page be the index if it is called with no arguments +[ -z "$1" ] && { + cat << EOF +

blog entries

+ +EOF + +} || { + # convert the markdown page to html text + md2html $1 + + # add a back button + cat << EOF +go back to list +EOF +} + +# and a footer +cat << EOF + + +EOF + diff --git a/example/entries/entry1.md b/example/entries/entry1.md new file mode 100755 index 0000000..82917c7 --- /dev/null +++ b/example/entries/entry1.md @@ -0,0 +1,13 @@ +#!./entries.sh + +# first blog entry + +hello welcome to my test blog. here i will be talking about blah + +## something else + +i probably have something else to say here + +## conclusion + +this is the conclusion, thank you diff --git a/example/entries/entry2.md b/example/entries/entry2.md new file mode 100755 index 0000000..54b978d --- /dev/null +++ b/example/entries/entry2.md @@ -0,0 +1,9 @@ +#!./entries.sh + +# day 2 + +this is a second entry to my blog + +i learned how to make text **bold** and *italic* + +wow isnt that ***cool???*** diff --git a/src/test.md b/example/index.md old mode 100644 new mode 100755 similarity index 97% rename from src/test.md rename to example/index.md index 203d425..1be4e1f --- a/src/test.md +++ b/example/index.md @@ -1,3 +1,5 @@ +#!/usr/bin/env md2html + # 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] 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.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" + From 50074253d03d2f8568d44fd931b84ce212156be9 Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 12 Jun 2023 04:02:07 +0100 Subject: [PATCH 2/2] add install script to install --- install.sh | 8 ++++++++ src/{shblg => shblg.sh} | 0 src/test.sh | 18 ------------------ 3 files changed, 8 insertions(+), 18 deletions(-) create mode 100755 install.sh rename src/{shblg => shblg.sh} (100%) delete mode 100755 src/test.sh diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..9c8a653 --- /dev/null +++ b/install.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +# installs m2html and shblg to the system path + +PREFIX=${PREFIX:-/usr} + +install -m755 src/md2html.sh ${PREFIX}/bin/md2html +install -m755 src/shblg.sh ${PREFIX}/bin/shblg diff --git a/src/shblg b/src/shblg.sh similarity index 100% rename from src/shblg rename to src/shblg.sh diff --git a/src/test.sh b/src/test.sh deleted file mode 100755 index 84c5405..0000000 --- a/src/test.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/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" -