Skip to content

Instantly share code, notes, and snippets.

@step-
Last active November 6, 2020 09:31
Show Gist options
  • Save step-/c4250b23477e9c93a5567f7195d0d5ee to your computer and use it in GitHub Desktop.
Save step-/c4250b23477e9c93a5567f7195d0d5ee to your computer and use it in GitHub Desktop.
A Linux shell wrapper for hastyScribe, which converts markdown to self-contained HTML with embedded images.

HastyScribe is a simple, cross-platform command-line program to convert markdown to self-contained HTML.

This script for Linux adds options --help and --no-clobber, and facilitates customizing CSS styles.

To install HastyScribe:

  • Download the latest release binary from the above repo
  • Extract the binary to a directory in your path
  • Rename the binary hastyscribe.bin
  • Copy hastyscribe.sh alongside the renamed binary
  • Make both files executable.

Type hastyscribe.sh for a help screen.

Note that HastyScribe requires tail options and a single input name, which can be a quoted shell glob.

#!/bin/sh
# A wrapper for hastyscribe v1.12.3 https://github.com/h3rald/hastyscribe
# This file: https://gist.github.com/step-/c4250b23477e9c93a5567f7195d0d5ee
SN=${0##*/}
if [ "$1" ]; then
filepath_or_glob=$1
shift
else
set -- -h
fi
### parse and delete out-of-tree options
unset opt_no_clobber
is_first=true
for p; do
[ "$is_first" ] && set --
case $p in
*-h*|*--help* ) printf >&2 '\t\t\t\033[7m Note tail OPTIONS \033[0m
Usage: hastyscribe.sh filename-or-glob-expression [ OPTIONS ]
filename-or-glob-expression is a valid file or glob expression that
generates the input file names to be compiled into HTML. Quote a glob
expression to avoid expansion by the shell. Output files are created
alongside input files with name extension ".htm". \033[31;7mExisting files are
overwritten\033[0m. See also --no-clobber.
OPTIONS:
-h, --help print usage and exit.
--no-clobber exit 100 if processing would overwrite existing .htm files.
From https://github.com/h3rald/hastyscribe:
--field/<field>=<value> causes HastyScribe to set a custom field to a specific value.
--notoc causes HastyScribe to output HTML documents without automatically generating a Table of Contents at the start.
--user-css=<file> causes HastyScribe to insert the contents of the specified local file as a CSS stylesheet.
--user-js=<file> causes HastyScribe to insert the contents of the specified local file as a Javascript script.
--output-file=<file> causes HastyScribe to write output to a local file (Use --output-file=- to output to standard output).
--watermark=<file> causes HastyScribe to embed and display an image as a watermark throughout the document.
--fragment causes HastyScribe to output just an HTML fragment instead of a full document, without embedding any image, font or stylesheet.
--dump=all|styles|fonts causes HastyScribe to dump all resources/stylesheets/fonts to the current directory.
'
exit;;
--no-clobber ) opt_no_clobber=true ;;
* ) set -- "$@" "$p" ;;
esac
unset is_first
done
# return 0 if $filepath_or_glob would overwrite an existing file
test_clobber() { # $@-filepaths
local p not_found=1
for p; do
p=${p%.*}.htm
if [ -e "$p" ]; then
not_found=0
echo "found $p" >&2
fi
done
return $not_found
}
if [ "$opt_no_clobber" ] && test_clobber $filepath_or_glob; then
echo "$SN: refusing to overwrite '$filepath_or_glob'." >&2
exit 100
fi
### set traps and create temporary folder
TMPD=$(mktemp -d -p "${TMPDIR:-/tmp}" "${0##*/}_XXXXXX")
chmod 700 "$TMPD" || exit 1
trap "rm -rf \"$TMPD\"/* \"$TMPD\"; exit \$status" HUP INT ALRM TERM 0 USR1 USR2
### CSS
(cd $TMPD; "${0%.sh}.bin" '' --dump=styles) # dumps to $SN.css
# edit below to add custom preferences
echo >> $TMPD/$SN.css '
@media print {
.headings h2 {
page-break-before: unset;
}
}'
### Run hastyscribe.bin
"${0%.sh}".bin "$filepath_or_glob" "$@" --user-css=$TMPD/$SN.css
status=$?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment