markitzero.go (view raw)
1//
2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
3//
4// Permission to use, copy, modify, and distribute this software for any
5// purpose with or without fee is hereby granted, provided that the above
6// copyright notice and this permission notice appear in all copies.
7//
8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16package main
17
18import (
19 "fmt"
20 "regexp"
21 "strings"
22
23 "golang.org/x/net/html"
24 "humungus.tedunangst.com/r/webs/synlight"
25)
26
27var re_bolder = regexp.MustCompile(`(^|\W)\*\*((?s:.*?))\*\*($|\W)`)
28var re_italicer = regexp.MustCompile(`(^|\W)\*((?s:.*?))\*($|\W)`)
29var re_bigcoder = regexp.MustCompile("```(.*)\n?((?s:.*?))\n?```\n?")
30var re_coder = regexp.MustCompile("`([^`]*)`")
31var re_quoter = regexp.MustCompile(`(?m:^> (.*)(\n- ?(.*))?\n?)`)
32var re_reciter = regexp.MustCompile(`(<cite><a href=".*?">)https://twitter.com/([^/]+)/.*?(</a></cite>)`)
33var re_link = regexp.MustCompile(`.?.?https?://[^\s"]+[\w/)!]`)
34var re_zerolink = regexp.MustCompile(`\[([^]]*)\]\(([^)]*\)?)\)`)
35var re_imgfix = regexp.MustCompile(`<img ([^>]*)>`)
36var re_lister = regexp.MustCompile(`((^|\n)(\+|-).*)+\n?`)
37var re_tabler = regexp.MustCompile(`((^|\n)\|.*)+\n?`)
38
39var lighter = synlight.New(synlight.Options{Format: synlight.HTML})
40
41func markitzero(s string) string {
42 // prepare the string
43 s = strings.TrimSpace(s)
44 s = strings.Replace(s, "\r", "", -1)
45
46 // save away the code blocks so we don't mess them up further
47 var bigcodes, lilcodes, images []string
48 s = re_bigcoder.ReplaceAllStringFunc(s, func(code string) string {
49 bigcodes = append(bigcodes, code)
50 return "``````"
51 })
52 s = re_coder.ReplaceAllStringFunc(s, func(code string) string {
53 lilcodes = append(lilcodes, code)
54 return "`x`"
55 })
56 s = re_imgfix.ReplaceAllStringFunc(s, func(img string) string {
57 images = append(images, img)
58 return "<img x>"
59 })
60
61 // fewer side effects than html.EscapeString
62 buf := make([]byte, 0, len(s))
63 for _, c := range []byte(s) {
64 switch c {
65 case '&':
66 buf = append(buf, []byte("&")...)
67 case '<':
68 buf = append(buf, []byte("<")...)
69 case '>':
70 buf = append(buf, []byte(">")...)
71 default:
72 buf = append(buf, c)
73 }
74 }
75 s = string(buf)
76
77 // mark it zero
78 s = re_link.ReplaceAllStringFunc(s, linkreplacer)
79 s = re_zerolink.ReplaceAllString(s, `<a href="$2">$1</a>`)
80 s = re_bolder.ReplaceAllString(s, "$1<b>$2</b>$3")
81 s = re_italicer.ReplaceAllString(s, "$1<i>$2</i>$3")
82 s = re_quoter.ReplaceAllString(s, "<blockquote>$1<br><cite>$3</cite></blockquote><p>")
83 s = re_reciter.ReplaceAllString(s, "$1$2$3")
84 s = strings.Replace(s, "\n---\n", "<hr><p>", -1)
85
86 s = re_lister.ReplaceAllStringFunc(s, func(m string) string {
87 m = strings.Trim(m, "\n")
88 items := strings.Split(m, "\n")
89 r := "<ul>"
90 for _, item := range items {
91 r += "<li>" + strings.Trim(item[1:], " ")
92 }
93 r += "</ul><p>"
94 return r
95 })
96 s = re_tabler.ReplaceAllStringFunc(s, func(m string) string {
97 m = strings.Trim(m, "\n")
98 rows := strings.Split(m, "\n")
99 var r strings.Builder
100 r.WriteString("<table>")
101 for _, row := range rows {
102 r.WriteString("<tr>")
103 cells := strings.Split(row, "|")
104 for i, cell := range cells {
105 cell = strings.TrimSpace(cell)
106 if cell == "" && (i == 0 || i == len(cells)-1) {
107 continue
108 }
109 r.WriteString("<td>")
110 r.WriteString(cell)
111 }
112 }
113 r.WriteString("</table><p>")
114 return r.String()
115 })
116
117 // restore images
118 s = strings.Replace(s, "<img x>", "<img x>", -1)
119 s = re_imgfix.ReplaceAllStringFunc(s, func(string) string {
120 img := images[0]
121 images = images[1:]
122 return img
123 })
124
125 // now restore the code blocks
126 s = re_coder.ReplaceAllStringFunc(s, func(string) string {
127 code := lilcodes[0]
128 lilcodes = lilcodes[1:]
129 code = html.EscapeString(code)
130 return code
131 })
132 s = re_bigcoder.ReplaceAllStringFunc(s, func(string) string {
133 code := bigcodes[0]
134 bigcodes = bigcodes[1:]
135 m := re_bigcoder.FindStringSubmatch(code)
136 return "<pre><code>" + lighter.HighlightString(m[2], m[1]) + "</code></pre><p>"
137 })
138 s = re_coder.ReplaceAllString(s, "<code>$1</code>")
139
140 // some final fixups
141 s = strings.Replace(s, "\n", "<br>", -1)
142 s = strings.Replace(s, "<br><blockquote>", "<blockquote>", -1)
143 s = strings.Replace(s, "<br><cite></cite>", "", -1)
144 s = strings.Replace(s, "<br><pre>", "<pre>", -1)
145 s = strings.Replace(s, "<br><ul>", "<ul>", -1)
146 s = strings.Replace(s, "<br><table>", "<table>", -1)
147 s = strings.Replace(s, "<p><br>", "<p>", -1)
148 return s
149}
150
151func linkreplacer(url string) string {
152 if url[0:2] == "](" {
153 return url
154 }
155 prefix := ""
156 for !strings.HasPrefix(url, "http") {
157 prefix += url[0:1]
158 url = url[1:]
159 }
160 addparen := false
161 adddot := false
162 if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
163 url = url[:len(url)-1]
164 addparen = true
165 }
166 if strings.HasSuffix(url, ".") {
167 url = url[:len(url)-1]
168 adddot = true
169 }
170 url = fmt.Sprintf(`<a href="%s">%s</a>`, url, url)
171 if adddot {
172 url += "."
173 }
174 if addparen {
175 url += ")"
176 }
177 return prefix + url
178}