all repos — honk @ 81ba3920651b03a714619569b8f9c8d7ef0e2cff

my fork of honk

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:^&gt; (.*)(\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("&amp;")...)
 67		case '<':
 68			buf = append(buf, []byte("&lt;")...)
 69		case '>':
 70			buf = append(buf, []byte("&gt;")...)
 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		alignments := make(map[int]string)
102		for _, row := range rows {
103			hastr := false
104			cells := strings.Split(row, "|")
105			for i, cell := range cells {
106				cell = strings.TrimSpace(cell)
107				if cell == "" && (i == 0 || i == len(cells)-1) {
108					continue
109				}
110				switch cell {
111				case ":---":
112					alignments[i] = `style="text-align: left"`
113					continue
114				case ":---:":
115					alignments[i] = `style="text-align: center"`
116					continue
117				case "---:":
118					alignments[i] = `style="text-align: right"`
119					continue
120				}
121				if !hastr {
122					r.WriteString("<tr>")
123					hastr = true
124				}
125				fmt.Fprintf(&r, "<td %s>", alignments[i])
126				r.WriteString(cell)
127			}
128		}
129		r.WriteString("</table><p>")
130		return r.String()
131	})
132
133	// restore images
134	s = strings.Replace(s, "&lt;img x&gt;", "<img x>", -1)
135	s = re_imgfix.ReplaceAllStringFunc(s, func(string) string {
136		img := images[0]
137		images = images[1:]
138		return img
139	})
140
141	// now restore the code blocks
142	s = re_coder.ReplaceAllStringFunc(s, func(string) string {
143		code := lilcodes[0]
144		lilcodes = lilcodes[1:]
145		code = html.EscapeString(code)
146		return code
147	})
148	s = re_bigcoder.ReplaceAllStringFunc(s, func(string) string {
149		code := bigcodes[0]
150		bigcodes = bigcodes[1:]
151		m := re_bigcoder.FindStringSubmatch(code)
152		return "<pre><code>" + lighter.HighlightString(m[2], m[1]) + "</code></pre><p>"
153	})
154	s = re_coder.ReplaceAllString(s, "<code>$1</code>")
155
156	// some final fixups
157	s = strings.Replace(s, "\n", "<br>", -1)
158	s = strings.Replace(s, "<br><blockquote>", "<blockquote>", -1)
159	s = strings.Replace(s, "<br><cite></cite>", "", -1)
160	s = strings.Replace(s, "<br><pre>", "<pre>", -1)
161	s = strings.Replace(s, "<br><ul>", "<ul>", -1)
162	s = strings.Replace(s, "<br><table>", "<table>", -1)
163	s = strings.Replace(s, "<p><br>", "<p>", -1)
164	return s
165}
166
167func linkreplacer(url string) string {
168	if url[0:2] == "](" {
169		return url
170	}
171	prefix := ""
172	for !strings.HasPrefix(url, "http") {
173		prefix += url[0:1]
174		url = url[1:]
175	}
176	addparen := false
177	adddot := false
178	if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
179		url = url[:len(url)-1]
180		addparen = true
181	}
182	if strings.HasSuffix(url, ".") {
183		url = url[:len(url)-1]
184		adddot = true
185	}
186	url = fmt.Sprintf(`<a href="%s">%s</a>`, url, url)
187	if adddot {
188		url += "."
189	}
190	if addparen {
191		url += ")"
192	}
193	return prefix + url
194}