preview-md.sh (1363B)
1 #!/bin/sh 2 # preview-md (preview markdown) 3 # DESCRIPTION: convert a markdown file $1 to html, optional open in a browser (if $2 = --open). Set converter in src (cmark by default). 4 # dependencies: cmark or markdown 5 # e.g.$ preview-md README.md --open 6 7 if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then 8 echo "Usage: preview-md [OPTIONS] FILE" 9 echo "" 10 echo "convert a Markdown file to HTML using cmark" 11 echo "the generated file will have the same filename as FILE, where the extension is replaced with .html" 12 echo "" 13 echo "OPTIONS" 14 echo " -c, --converter Specify the tool to use for converting the file to HTML" 15 echo "" 16 exit 17 fi 18 19 20 OPEN=0 21 CONV=0 22 23 system="$(uname)" 24 mdconv="cmark"; if [ "$(command -v cmark)" = "" ]; then mdconv="markdown"; fi 25 26 for arg in "$@"; do 27 if [ $CONV -eq 1 ]; then 28 mdconv="$1" 29 shift 30 CONV=0 31 continue 32 fi 33 34 35 if [ "$arg" = "-o" ] || [ "$arg" = "--open" ]; then 36 OPEN=1 37 shift 38 elif [ "$arg" = "-c" ] || [ "$arg" = "--converter" ]; then 39 CONV=1 40 shift 41 else 42 if [ "$(command -v "$mdconv")" = "" ]; then 43 echo "failed to execute '$mdconv'" 44 exit 45 fi 46 47 out=$(echo "$1" | sed -E "s/\.(md|txt)/\.html/") 48 if [ -e "$out" ]; then rm -i "$out"; fi 49 50 $mdconv "$1" > "$out" 51 52 if [ $OPEN ]; then 53 if [ "$system" = "Darwin" ]; then open "$out"; 54 elif [ "$system" = "Linux" ]; then xdg-open "$out"; 55 fi 56 fi 57 fi 58 done