scripts

My personal collection of scripts
git clone git://src.gearsix.net/scripts
Log | Files | Refs | Atom | README | LICENSE

commit eb56addd4b9de5473342ecbda72e2a2709b1e4dc
parent 98599365d688bffc3a18911b7256f35ed0cde7aa
Author: gearsix <gearsix@tuta.io>
Date:   Tue,  7 Mar 2023 10:35:12 +0000

md-preview: added option to specify converter (-c, --converter)

Diffstat:
Msrc/posix/preview-md.sh | 52+++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 43 insertions(+), 9 deletions(-)

diff --git a/src/posix/preview-md.sh b/src/posix/preview-md.sh @@ -1,24 +1,58 @@ #!/bin/sh # preview-md (preview markdown) # DESCRIPTION: convert a markdown file $1 to html, optional open in a browser (if $2 = --open). Set converter in src (cmark by default). -# dependencies: cmark +# dependencies: cmark or markdown # e.g.$ preview-md README.md --open if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then - echo "Usage: preview-md FILE [OPTIONS]" + echo "Usage: preview-md [OPTIONS] FILE" echo "" echo "convert a Markdown file to HTML using cmark" echo "the generated file will have the same filename as FILE, where the extension is replaced with .html" echo "" + echo "OPTIONS" + echo " -c, --converter Specify the tool to use for converting the file to HTML" + echo "" exit fi -mdconv="cmark" -if [ "$(command -v cmark)" = "" ]; then mdconv="markdown"; - if [ "$(command -v markdown)" = "" ]; then echo "'cmark' or 'markdown' required"; exit; fi; -fi -out=$(echo "$1" | sed -E "s/\.(md|txt)/\.html/") -if [ -e "$out" ]; then rm -i "$out"; fi +OPEN=0 +CONV=0 + +system="$(uname)" +mdconv="cmark"; if [ "$(command -v cmark)" = "" ]; then mdconv="markdown"; fi + +for arg in "$@"; do + if [ $CONV -eq 1 ]; then + mdconv="$1" + shift + CONV=0 + continue + fi + + + if [ "$arg" = "-o" ] || [ "$arg" = "--open" ]; then + OPEN=1 + shift + elif [ "$arg" = "-c" ] || [ "$arg" = "--converter" ]; then + CONV=1 + shift + else + if [ "$(command -v "$mdconv")" = "" ]; then + echo "failed to execute '$mdconv'" + exit + fi + + out=$(echo "$1" | sed -E "s/\.(md|txt)/\.html/") + if [ -e "$out" ]; then rm -i "$out"; fi + + $mdconv "$1" > "$out" -$mdconv "$1" > "$out" + if [ $OPEN ]; then + if [ "$system" = "Darwin" ]; then open "$out"; + elif [ "$system" = "Linux" ]; then xopen "$out"; + fi + fi + fi +done