summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlshprung <lshprung@yahoo.com>2021-12-17 18:11:37 -0800
committerlshprung <lshprung@yahoo.com>2021-12-17 18:11:37 -0800
commit6f2f76f53efc73a3f9c6e8fa1e80e8618f79701b (patch)
treef185cd82a307e9dc669395d6ded6bc6f01140571
First commit
-rw-r--r--.gitignore2
-rwxr-xr-xmarkdown-to-github-html.sh87
2 files changed, 89 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..91e5bb9
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*
+!markdown-to-github-html.sh
diff --git a/markdown-to-github-html.sh b/markdown-to-github-html.sh
new file mode 100755
index 0000000..527d289
--- /dev/null
+++ b/markdown-to-github-html.sh
@@ -0,0 +1,87 @@
+#!/usr/bin/sh
+
+NAME="$0"
+CSS_LINK="https://raw.githubusercontent.com/sindresorhus/github-markdown-css/gh-pages/github-markdown.css"
+INPUT="/dev/stdin"
+CUSTOM_INPUT=0
+OUTPUT="/dev/stdout"
+CUSTOM_OUTPUT=0
+COMPILER="marked --gfm"
+
+print_help() {
+ echo "Usage: $NAME [OPTION]... [INPUT]"
+ echo "Compile INPUT markdown file to github-styled html"
+ echo ""
+ echo " --compiler [COMPILER] set the markdown compiler to use. Default is marked --gfm"
+ echo " -h, --help print this help message"
+ echo " -i, --input [INPUT] specify INPUT file"
+ echo " -o, --output [OUTPUT] output to specified OUTPUT file"
+ echo " -y, --yes automatic yes to prompts"
+}
+
+# Parse arguments
+while [ $# -gt 0 ]; do
+ case $1 in
+ --compiler)
+ COMPILER="$2"
+ shift 2
+ ;;
+ -h | --help)
+ print_help
+ exit 1
+ ;;
+ -i | --input)
+ INPUT="$2"
+ CUSTOM_INPUT=1
+ shift 2
+ ;;
+ -o | --output)
+ OUTPUT="$2"
+ CUSTOM_OUTPUT=1
+ shift 2
+ ;;
+ *)
+ INPUT="$1"
+ CUSTOM_INPUT=1
+ shift
+ ;;
+ esac
+done
+
+# Check if overwrite
+if [ $CUSTOM_OUTPUT -eq 1 ] && [ -e "$OUTPUT" ]; then
+ echo -n "File $OUTPUT already exists. Overwrite it? [y/N] "
+ read -r PROMPT
+ if [ "$PROMPT" = "y" ]; then
+ echo -n "" > "$OUTPUT"
+ else
+ exit 1
+ fi
+fi
+
+# Add meta, link, and style lines to OUTPUT
+echo "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" >> "$OUTPUT"
+echo "<link rel=\"stylesheet\" href=\"github-markdown.css\">" >> "$OUTPUT"
+echo "<style>" >> "$OUTPUT"
+echo " .markdown-body {" >> "$OUTPUT"
+echo " box-sizing: border-box;" >> "$OUTPUT"
+echo " min-width: 200px;" >> "$OUTPUT"
+echo " max-width: 980px;" >> "$OUTPUT"
+echo " margin: 0 auto;" >> "$OUTPUT"
+echo " padding: 45px;" >> "$OUTPUT"
+echo " }" >> "$OUTPUT"
+echo "" >> "$OUTPUT"
+echo " @media (max-width: 767px) {" >> "$OUTPUT"
+echo " .markdown-body {" >> "$OUTPUT"
+echo " padding: 15px;" >> "$OUTPUT"
+echo " }" >> "$OUTPUT"
+echo " }" >> "$OUTPUT"
+echo "</style>" >> "$OUTPUT"
+echo "<article class=\"markdown-body\">" >> "$OUTPUT"
+
+# Add body (from markdown) using compiler (default is marked --gfm)
+# TODO work on getting syntax highlighting support
+$COMPILER "$INPUT" >> "$OUTPUT"
+
+# Close output
+echo "</article>" >> "$OUTPUT"