reading time
[pinopress.git] / modules / md
1 #!/bin/sed -Ef
2
3 ## markdown to html module for pinopress
4 ## based off https://github.com/stamby/md-to-html
5 ## usage:
6 ##      md [md_filepath]
7
8 # remove document meta
9 ## matches block of -- at top of file
10 1,/^([^-]|-[^-]|^$)/ {
11     /^([^-]|-[^-]|^$)/ !{
12         /^-- ?/d
13     }
14 }
15
16 # html style comments (this will also work inside code blocks - unintended)
17 /<!--(.*)-->/d
18
19 # special html characters
20 s/\&/\&amp\;/g
21 s/</\&lt\;/g
22 s/>/\&gt\;/g
23
24 # code blocks
25 /^ *```/ {
26     x
27     # check if hold space contains READING
28     # this is to see if this is the first ``` we read or no
29     /^ *READING/ !{
30         # if it is the first one, add READING to hold space
31         s/.*/READING/
32         x
33         # write opening tag (also concat with next line)
34         N
35         s/.*\n(.*)/<pre><code>\1/
36         b
37     }
38     # if its the second ```, we are done
39     # first clear hold
40     s/.*//
41     x
42     # then write closing tag
43     s/.*/<\/code><\/pre>/
44     b
45 }
46
47 # if we aren't reading ```, but hold space has READING in it
48 # that means we are currently processing a block, just ignore and
49 # keep going
50 x
51 /^ *READING/ {
52     x
53     b
54 }
55 x
56
57
58 # horizontal rule
59 s/^\s*-{3,}\s*$/<hr\/>/
60
61 # inline styles
62 s/(^|[^\\\*])\*{3}([^\*]+)\*{3}([^\*]|$)/\1<strong><em>\2<\/em><\/strong>\3/g
63 s/(^|[^\\_])_{3}([^_]+)_{3}([^_]|$)/\1<strong><em>\2<\/em><\/strong>\3/g
64 s/(^|[^\\\*])\*{2}([^\*]+)\*{2}([^\*]|$)/\1<strong>\2<\/strong>\3/g
65 s/(^|[^\\_])_{2}([^\_]+)_{2}([^_]|$)/\1<strong>\2<\/strong>\3/g
66 s/(^|[^\\\*])\*([^\*]+)\*([^\*]|$)/\1<em>\2<\/em>\3/g
67 s/(^|[^\\_])_([^_]+)_([^_]|$)/\1<em>\2<\/em>\3/g
68 s/(^|[^\\`])`([^`]+)`([^`]|$)/\1<code>\2<\/code>\3/g
69 s/(^|[^\\~])~{2}([^~]+)~{2}([^~]|$)/\1<del>\2<\/del>\3/g
70
71 # images
72 s/!\[(.*)\]\((.*)\)/<img src="\2" alt="\1"\/>/g
73
74 # links
75 s/\[(.*)\]\((.*)\)/<a href="\2">\1<\/a>/g
76 s/\[(.*)\]/<a href="\1">\1<\/a>/g
77
78 # headers
79 s/^#{6} (.*)/<h6>\1<\/h6>/
80 s/^#{5} (.*)/<h5>\1<\/h5>/
81 s/^#{4} (.*)/<h4>\1<\/h4>/
82 s/^#{3} (.*)/<h3>\1<\/h3>/
83 s/^#{2} (.*)/<h2>\1<\/h2>/
84 s/^# (.*)/<h1>\1<\/h1>/
85
86 # lists
87
88
89 # paragraphs (blocks of text separated by one or more blank lines)
90 /./ {
91     H
92     $!d
93 }
94 x
95 /^\s*$/ !{
96     s/^/\n<p>/
97     s/$/\n<\/p>/
98 }
99