summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-02-17 23:53:07 +0000
committerdavidovski <david@davidovski.xyz>2023-02-17 23:53:07 +0000
commite13af149eaa5d8f83437e273b17a535d250a4cde (patch)
tree308ae973796caeb8098f79e4ce774830f9e8026a
Paragraph and heading parsing for md2html
-rwxr-xr-xsrc/md2html.sh130
-rw-r--r--src/shblg4
-rw-r--r--src/test.md8
3 files changed, 142 insertions, 0 deletions
diff --git a/src/md2html.sh b/src/md2html.sh
new file mode 100755
index 0000000..f675399
--- /dev/null
+++ b/src/md2html.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+
+# replace all * with _ for easier processing
+#
+_pre_emph () {
+ while IFS= read -r line; do
+ while [ "$line" != "${line%%\**}" ]; do
+ printf "%s_" "${line%%\**}"
+ line="${line#*\*}"
+ done
+ printf "${line}\n"
+ done
+}
+
+
+# fix any misaligned <em> and <strong> closign tags
+#
+_post_emph () {
+ while IFS= read -r line; do
+ # TODO: avoid this problem entirely?
+
+ local wrong="</strong></em>" right="</em></strong>"
+
+ while [ "$line" != "${line%%${wrong}*}" ]; do
+ printf "%s${right}" "${line%%${wrong}*}"
+ line="${line#*${wrong}}"
+ done
+ printf "${line}\n"
+ done
+}
+
+# parse emphasis in a line
+#
+# emph [bound] <lefttag> <righttag>
+#
+_emph () {
+ local bound="$1"
+ local lefttag="$2" righttag="$3"
+
+ while IFS= read -r line; do
+ next="${line}"
+ line=
+ rightofbold=
+
+ while [ "$next" != "${next#*${bound}}" ]; do
+ leftofbold="${next%%${bound}*}"
+ rightofbold="${next#$leftofbold${bound}*}"
+ bold="${rightofbold%%${bound}*}"
+ next="${rightofbold#*${bound}}"
+ printf "%s%s%s%s" "${leftofbold}" "${lefttag}" "${bold}" "${righttag}"
+ done
+ printf "%s\n" "${next}"
+ done
+}
+
+# parse heading
+#
+# h [heading no.]
+_h () {
+ local num=$1
+ while IFS= read -r line; do
+ s=
+
+ n=$num
+ while [ "$n" -gt "0" ]; do
+ s="#$s"
+ n=$((n-1))
+ done
+
+ case "$line" in
+ "$s "*)
+ printf "<h$num>%s</h$num>\n" "${line#$s }"
+ ;;
+ *)
+ printf "%s\n" "$line"
+ ;;
+ esac
+ done
+}
+
+# parse paragraphs
+#
+# p
+_p () {
+ local num=$1
+ empty=true
+ while IFS= read -r line; do
+ case "$line" in
+ "#"*)
+ printf "%s\n" "$line"
+ ;;
+ "")
+ $empty ||
+ printf "</p>\n"
+
+ empty=true ;;
+ *)
+ $empty &&
+ printf "<p>%s " "$line" ||
+ printf "%s " "$line"
+
+ empty=false ;;
+ esac
+
+ done
+
+ $empty || {
+ printf "</p>\n"
+ }
+}
+
+
+# convert the markdown from stdin into html
+#
+md2html () {
+ _p \
+ | _pre_emph \
+ | _emph '__' "<strong>" "</strong>" \
+ | _emph '_' "<em>" "</em>" \
+ | _post_emph \
+ | _h 6 \
+ | _h 5 \
+ | _h 4 \
+ | _h 3 \
+ | _h 2 \
+ | _h 1
+}
+
+md2html
+
diff --git a/src/shblg b/src/shblg
new file mode 100644
index 0000000..5361975
--- /dev/null
+++ b/src/shblg
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+BLOG_DIR
+OUTPUT_DIR
diff --git a/src/test.md b/src/test.md
new file mode 100644
index 0000000..15bd4a1
--- /dev/null
+++ b/src/test.md
@@ -0,0 +1,8 @@
+# This is a test md file hello
+
+This is *italics* this is **bold** this is ***both*** wow
+
+so hold tight
+this is a paragraph
+
+haha