Created
September 22, 2011 10:43
-
-
Save elmimmo/1234517 to your computer and use it in GitHub Desktop.
Preview (multi)markdown in preferred browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Preview (multi)markdown documents in the preferred browser. | |
function mmdp() { | |
if [ -z "$1" ]; then | |
echo "Usage: mmdp FILE" | |
return 1 | |
fi | |
# Determine how to open a file based on OS, and what MMD binary to use: | |
local OS=$(uname -s) | |
case $OS in | |
# on Mac OS X: | |
"Darwin" ) | |
local OPEN="open" | |
local MMD="$(type -P multimarkdown)" | |
;; | |
# on Linux: | |
"Linux" ) | |
local OPEN="xdg-open" | |
local MMD="/usr/bin/MultiMarkdown.pl" | |
;; | |
# on other OSs: | |
* ) | |
# Exit before making who knows what mess. | |
echo "Failed. OS not supported." 1>&2 | |
return 1 | |
;; | |
esac | |
if [ ! -x "$MMD" ]; then | |
echo "Failed. MultiMarkdown required." 1>&2 | |
return 1 | |
fi | |
# Add metadata in order to get full HTML with html, head & body tags. | |
# You should change "format:complete" for "css:path" for custom CSS. | |
echo "format:complete" > "/tmp/multimarkdown-preview.md" | |
# If MMD doc had no metadata, add a newline. | |
if [[ "`head -n1 \"$1\"`" != *:* ]]; then | |
echo "" >> "/tmp/multimarkdown-preview.md" | |
fi | |
cat "$1" >> "/tmp/multimarkdown-preview.md" | |
# Convert from MMD to HTML. | |
"$MMD" "/tmp/multimarkdown-preview.md" > \ | |
"/tmp/multimarkdown-preview.html" | |
# Add UTF-8 encoding declaration | |
sed -i'' -e "s/<head>/<head>\\`echo -e '\r'` <meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\" \/>/;" /tmp/multimarkdown-preview.html | |
"$OPEN" "/tmp/multimarkdown-preview.html" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment