Fix inline code from being formatted

This commit is contained in:
davidovki 2023-03-11 17:04:50 +00:00
parent dd541cc6de
commit a5fcbde9b2
2 changed files with 90 additions and 32 deletions

View File

@ -2,6 +2,29 @@
ESC_SEQ='\0' 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 # replace all * with _ for easier processing
# #
_pre_emph () { _pre_emph () {
@ -107,8 +130,8 @@ _p () {
;; ;;
*) *)
$empty && $empty &&
printf "<p>\n%s\n " "$line" || printf "<p>\n%s\n" "$line" ||
printf "%s\n " "$line" printf "%s\n" "$line"
empty=false ;; empty=false ;;
esac esac
@ -133,6 +156,10 @@ _a_img () {
case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac
next="$line" next="$line"
while [ "$next" != "${next#*$close}" ]; do while [ "$next" != "${next#*$close}" ]; do
case "$next" in
*"["*"]("*")"*);;
*) break;
esac
before="${next%%$open*}" before="${next%%$open*}"
text=${next#*$open} text=${text%%$mid*} text=${next#*$open} text=${text%%$mid*}
url=${next#*$mid} url=${url%%$close*} url=${next#*$mid} url=${url%%$close*}
@ -144,12 +171,12 @@ _a_img () {
|| title= || title=
case "$before" in 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%!}" ;; before="${before%!}" ;;
*) h="%s<a href=\"%s\"%s>%s</a>" ;; *) h="%s\n%s<a href=\"%s\"%s>\n%s</a>\n" ;;
esac esac
printf "$h" "$before" "$url" "$title" "$text" printf "$h" "$before" "$ESC_SEQ" "$url" "$title" "$text"
next="${next#*$close}" next="${next#*$close}"
done done
@ -163,7 +190,7 @@ _get_indent () {
indent=0 indent=0
l="$*" l="$*"
while [ "$l" ]; do while [ "$l" ]; do
c="${l%*${l#?}}" c="${l%*"${l#?}"}"
case "$c" in case "$c" in
" ") indent=$((indent+1)) ;; " ") indent=$((indent+1)) ;;
*) *)
@ -261,6 +288,14 @@ _ol () {
print_x $to_close "</ol>\n" print_x $to_close "</ol>\n"
} }
# parse inline codeblocks
#
_inline_code () {
_emph '`' "
$ESC_SEQ<code>" "</code>
"
}
# parse multiline codeblocks # parse multiline codeblocks
# #
_code () { _code () {
@ -268,14 +303,19 @@ _code () {
while IFS= read -r line; do while IFS= read -r line; do
case "$line" in case "$line" in
" "*) " "*)
# prefix lines with newline to avoid trailing newline
$codeblock && $codeblock &&
printf "%s\n" "$ESC_SEQ${line# }" || printf "\n%s" "$ESC_SEQ${line# }" ||
$content || { $content || {
printf "<pre><code>\n" printf "%s<pre><code>%s" "$ESC_SEQ" "${line# }"
codeblock=true codeblock=true
printf "%s\n" "$ESC_SEQ${line# }"
} }
;; ;;
"")
$codeblock \
&& printf "\n%s" "$ESC_SEQ" \
|| printf "\n"
;;
*) *)
$codeblock && { $codeblock && {
printf "</code></pre>\n" printf "</code></pre>\n"
@ -323,19 +363,8 @@ _blockquote () {
print_x $((indent_level)) "</blockquote>\n" print_x $((indent_level)) "</blockquote>\n"
} }
_post_escape () { # add html header
while IFS= read -r line; do #
case "$line" in
"$ESC_SEQ"*)
printf "%s\n" "${line#??}"
;;
*)
printf "%s\n" "$line"
;;
esac
done
}
_html () { _html () {
printf "<!DOCTYPE html>\n" printf "<!DOCTYPE html>\n"
while IFS= read -r line; do while IFS= read -r line; do
@ -343,30 +372,51 @@ _html () {
done done
} }
# remove all unecessary newlines
#
_squash () {
while IFS= read -r line; do
case "$line" in
"$ESC_SEQ"*)
printf "\n%s" "${line#??}"
;;
*)
printf "%s" "$line"
;;
esac
done
printf "\n"
}
# convert the markdown from stdin into html # convert the markdown from stdin into html
# #
md2html () { md2html () {
# the order of these somewhat matters # the order of these somewhat matters
_pre_emph \ _pre_strip \
| _code \ | _code \
| _pre_emph \
| _blockquote \ | _blockquote \
| _ul \ | _ul \
| _ol \ | _ol \
| _p \ | _p \
| _a_img \
| _inline_code \
| _emph '__' "<strong>" "</strong>" \ | _emph '__' "<strong>" "</strong>" \
| _emph '_' "<em>" "</em>" \ | _emph '_' "<em>" "</em>" \
| _emph '`' "<code>" "</code>" \
| _post_emph \ | _post_emph \
| _a_img \
| _h 6 \ | _h 6 \
| _h 5 \ | _h 5 \
| _h 4 \ | _h 4 \
| _h 3 \ | _h 3 \
| _h 2 \ | _h 2 \
| _h 1 \ | _h 1 \
| _post_escape \ | _squash \
| _html | _html
cat > /dev/null << EOF
EOF
} }
md2html md2html

View File

@ -1,18 +1,22 @@
# This is a test md file hello # 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 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 an image](httpsomething)
this is a paragraph this is a paragraph
with many lines
that are joined together
> this is a quote hi > this is a quote hi
ok that was a quote 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 > - this is a list in a quote
> - it was quoted > - it was quoted
> >
@ -29,8 +33,12 @@ haha
ok ok
int main() { int main() {
printf("hello %s\n", "world"); printf("hello %s\n", "world");
}
int func(int* a) {
return a;
} }
- list - list
@ -44,10 +52,10 @@ ok
+ plus now wow + plus now wow
+ plus wow so cool + plus wow so cool
1. hello 1. hello
2. world 2. world
3. lOL 3. lOL
> ok that worked? > ok that worked?