## markdown to html module for pinopress
## based off https://github.com/stamby/md-to-html
-# input $1 - filepath to html file
+## usage:
+## md [md_filepath]
-# parse header of document
+# remove document meta
+## matches block of -- at top of file
+1,/^([^-]|-[^-]|^$)/ {
+ /^([^-]|-[^-]|^$)/ !{
+ /^-- ?/d
+ }
+}
-# html style comments
-s/<!--(.*)-->//g
+# html style comments (this will also work inside code blocks - unintended)
+/<!--(.*)-->/d
# special html characters
s/\&/\&\;/g
s/</\<\;/g
s/>/\>\;/g
+# code blocks
+/^ *```/ {
+ x
+ # check if hold space contains READING
+ # this is to see if this is the first ``` we read or no
+ /^ *READING/ !{
+ # if it is the first one, add READING to hold space
+ s/.*/READING/
+ x
+ # write opening tag (also concat with next line)
+ N
+ s/.*\n(.*)/<pre><code>\1/
+ b
+ }
+ # if its the second ```, we are done
+ # first clear hold
+ s/.*//
+ x
+ # then write closing tag
+ s/.*/<\/code><\/pre>/
+ b
+}
+
+# if we aren't reading ```, but hold space has READING in it
+# that means we are currently processing a block, just ignore and
+# keep going
+x
+/^ *READING/ {
+ x
+ b
+}
+x
+
+
# horizontal rule
s/^\s*-{3,}\s*$/<hr\/>/
s/!\[(.*)\]\((.*)\)/<img src="\2" alt="\1"\/>/g
# links
-s/\[(.*)\]\((.*)\)/<a href="\1">\2<\/a>/g
+s/\[(.*)\]\((.*)\)/<a href="\2">\1<\/a>/g
s/\[(.*)\]/<a href="\1">\1<\/a>/g
# headers
s/^#{4} (.*)/<h4>\1<\/h4>/
s/^#{3} (.*)/<h3>\1<\/h3>/
s/^#{2} (.*)/<h2>\1<\/h2>/
-s/^#} (.*)/<h1>\1<\/h1>/
-
-# code block
-
-# paragraphs
+s/^# (.*)/<h1>\1<\/h1>/
-# /./{H;$!d} ; x ; s/^/\n<p>/ ; s/$/\n<\/p>/
+# lists
+# paragraphs (blocks of text separated by one or more blank lines)
+/./ {
+ H
+ $!d
+}
+x
+/^\s*$/ !{
+ s/^/\n<p>/
+ s/$/\n<\/p>/
+}