Skip to content

Instantly share code, notes, and snippets.

@DocumentAlchemy
Last active April 19, 2016 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DocumentAlchemy/ae9611777ce1c9381c219b6e5453e857 to your computer and use it in GitHub Desktop.
Save DocumentAlchemy/ae9611777ce1c9381c219b6e5453e857 to your computer and use it in GitHub Desktop.
Convert Markdown to HTML, PDF or MS Word (DOCX) from the command line using https://documentalchemy.com/
#!/bin/bash
# Converts a Markdown file into an HTML, PDF or Microsoft Word document
# USAGE: md2x.sh <MARKDOWN-FILE> <FORMAT>
#
# EXAMPLE: md2x.sh README.md pdf
# This is your DocumentAlchemy API key. It may be set via the
# environment variable named `DA_API_KEY`.
# (See <https://documentalchemy.com/> to get one of your own.)
API_KEY=${DA_API_KEY:-"403l1zh3dkbakyb9"}
# This is a flag that can prevent this script from `echo`ing
# unnecessary information. It can be set via the environment
# variable `QUIET`.
# A clever person could make this into a command line parameter
# like `-q`.
QUIET=${QUIET:-FALSE}
# Note that you can set environment variables on a per-invocation
# basis by prefixing NAME=VALUE before the command. For example:
# API_KEY=my-key QUIET=true doc2docx *.doc
# utility function to show simple help
function usage {
echo "USE: $0 <MARKDOWN-FILE> <pdf|html|docx>";
}
md_file="$1"
format="$2"
if [ -z "$md_file" ] || [ -z "$format" ]; then
usage;
exit 1;
else
if ! [ -s "$md_file" ]; then
usage;
echo "File \"$md_file\" not found. Aborting.";
exit 2;
elif [ "$format" != "pdf" ] && [ "$format" != "html" ] && [ "$format" != "docx" ]; then
usage;
echo "Format must be one of 'pdf', 'html' or 'docx'. Found \"$format\". Aborting.";
exit 3;
else
outfile="`echo "${md_file}" | sed -E 's/\.[a-z]+$//'`.$format";
$QUIET || echo "Converting '$md_file' into '`basename "$outfile"`'...";
response=$(curl --silent \
--write-out %{http_code} -H "Authorization: da.key=$API_KEY" \
-X POST --form "document=@$md_file" \
https://documentalchemy.com/api/v1/document/-/rendition/$format \
-o "$outfile")
if ! [ "$response" -eq "200" ]; then
$QUIET || echo "WARNING: Expected a 200 response. Found $response instead.";
exit 4;
else
$QUIET || echo "...OK. File '$outfile' created."
fi
fi
fi
@DocumentAlchemy
Copy link
Author

See https://documentalchemy.com/ for information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment