summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovki <david@davidovski.xyz>2023-03-11 17:04:50 +0000
committerdavidovki <david@davidovski.xyz>2023-03-11 17:04:50 +0000
commita5fcbde9b233ab2af45ab0c204da1c764d33547a (patch)
tree0bf53530a13441996fd3dfdab522674712fe69c9
parentdd541cc6dedf8a911486e326c68254f0da234e47 (diff)
Fix inline code from being formatted
-rwxr-xr-xsrc/md2html.sh94
-rw-r--r--src/test.md20
2 files changed, 86 insertions, 28 deletions
diff --git a/src/md2html.sh b/src/md2html.sh
index 776527b..1edc6a1 100755
--- a/src/md2html.sh
+++ b/src/md2html.sh
@@ -2,6 +2,29 @@
ESC_SEQ='\0'
+# remove traling whitespace from empty lines
+#
+_pre_strip () {
+ while IFS= read -r line; do
+ set -- $line
+ [ "$*" ] && {
+ local l c
+ l="$line"
+ line=
+ while [ "$l" != "${l#?}" ]; do
+ c="${l%*"${l#?}"}"
+ case "$c" in
+ " ") line="$line ";;
+ *) line="$line$c" ;;
+ esac
+ l="${l#?}"
+ done
+
+ printf "%s\n" "$line"
+ } || printf "\n"
+ done
+}
+
# replace all * with _ for easier processing
#
_pre_emph () {
@@ -107,8 +130,8 @@ _p () {
;;
*)
$empty &&
- printf "<p>\n%s\n " "$line" ||
- printf "%s\n " "$line"
+ printf "<p>\n%s\n" "$line" ||
+ printf "%s\n" "$line"
empty=false ;;
esac
@@ -133,6 +156,10 @@ _a_img () {
case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac
next="$line"
while [ "$next" != "${next#*$close}" ]; do
+ case "$next" in
+ *"["*"]("*")"*);;
+ *) break;
+ esac
before="${next%%$open*}"
text=${next#*$open} text=${text%%$mid*}
url=${next#*$mid} url=${url%%$close*}
@@ -144,12 +171,12 @@ _a_img () {
|| title=
case "$before" in
- *!) h="%s<img src=\"%s\"%s alt=\"%s\"></img>"
+ *!) h="%s\n%s<img src=\"%s\"%s alt=\"%s\"></img>\n"
before="${before%!}" ;;
- *) h="%s<a href=\"%s\"%s>%s</a>" ;;
+ *) h="%s\n%s<a href=\"%s\"%s>\n%s</a>\n" ;;
esac
- printf "$h" "$before" "$url" "$title" "$text"
+ printf "$h" "$before" "$ESC_SEQ" "$url" "$title" "$text"
next="${next#*$close}"
done
@@ -163,7 +190,7 @@ _get_indent () {
indent=0
l="$*"
while [ "$l" ]; do
- c="${l%*${l#?}}"
+ c="${l%*"${l#?}"}"
case "$c" in
" ") indent=$((indent+1)) ;;
*)
@@ -261,6 +288,14 @@ _ol () {
print_x $to_close "</ol>\n"
}
+# parse inline codeblocks
+#
+_inline_code () {
+ _emph '`' "
+$ESC_SEQ<code>" "</code>
+"
+}
+
# parse multiline codeblocks
#
_code () {
@@ -268,14 +303,19 @@ _code () {
while IFS= read -r line; do
case "$line" in
" "*)
+ # prefix lines with newline to avoid trailing newline
$codeblock &&
- printf "%s\n" "$ESC_SEQ${line# }" ||
+ printf "\n%s" "$ESC_SEQ${line# }" ||
$content || {
- printf "<pre><code>\n"
+ printf "%s<pre><code>%s" "$ESC_SEQ" "${line# }"
codeblock=true
- printf "%s\n" "$ESC_SEQ${line# }"
}
;;
+ "")
+ $codeblock \
+ && printf "\n%s" "$ESC_SEQ" \
+ || printf "\n"
+ ;;
*)
$codeblock && {
printf "</code></pre>\n"
@@ -323,50 +363,60 @@ _blockquote () {
print_x $((indent_level)) "</blockquote>\n"
}
-_post_escape () {
+# add html header
+#
+_html () {
+ printf "<!DOCTYPE html>\n"
+ while IFS= read -r line; do
+ printf "%s\n" "$line"
+ done
+}
+
+# remove all unecessary newlines
+#
+_squash () {
while IFS= read -r line; do
case "$line" in
"$ESC_SEQ"*)
- printf "%s\n" "${line#??}"
+ printf "\n%s" "${line#??}"
;;
*)
- printf "%s\n" "$line"
+ printf "%s" "$line"
;;
esac
done
+ printf "\n"
}
-_html () {
- printf "<!DOCTYPE html>\n"
- while IFS= read -r line; do
- printf "%s\n" "$line"
- done
-}
# convert the markdown from stdin into html
#
md2html () {
# the order of these somewhat matters
- _pre_emph \
+ _pre_strip \
| _code \
+ | _pre_emph \
| _blockquote \
| _ul \
| _ol \
| _p \
+ | _a_img \
+ | _inline_code \
| _emph '__' "<strong>" "</strong>" \
| _emph '_' "<em>" "</em>" \
- | _emph '`' "<code>" "</code>" \
| _post_emph \
- | _a_img \
| _h 6 \
| _h 5 \
| _h 4 \
| _h 3 \
| _h 2 \
| _h 1 \
- | _post_escape \
+ | _squash \
| _html
+
+ cat > /dev/null << EOF
+EOF
}
md2html
diff --git a/src/test.md b/src/test.md
index a4a3f67..203d425 100644
--- a/src/test.md
+++ b/src/test.md
@@ -1,18 +1,22 @@
# This is a test md file hello
-This is *italics* this is **bold** this is ***both*** wow
+This is *italics* this is **bold** this is ***both*** wow (this is in brackets ssh) and [this is in square brackets not a anchor lol]
click [here](http) for stuff and [over here](http12 "my title") for more
+and [click here](http://this_has_stuffinside) too
+
![this is an image](httpsomething)
this is a paragraph
+with many lines
+that are joined together
> this is a quote hi
ok that was a quote
-> this quote has a list inside i
+> this quote has a list inside it
> - this is a list in a quote
> - it was quoted
>
@@ -29,8 +33,12 @@ haha
ok
- int main() {
- printf("hello %s\n", "world");
+ int main() {
+ printf("hello %s\n", "world");
+ }
+
+ int func(int* a) {
+ return a;
}
- list
@@ -44,10 +52,10 @@ ok
+ plus now wow
+ plus wow so cool
-
+
1. hello
2. world
3. lOL
-
+
> ok that worked?