all repos — honk @ 4d440bc2b3467b93018e125ead82242ffc2615ac

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?)`)
 32var re_link = regexp.MustCompile(`.?.?https?://[^\s"]+[\w/)!]`)
 33var re_zerolink = regexp.MustCompile(`\[([^]]*)\]\(([^)]*\)?)\)`)
 34var re_imgfix = regexp.MustCompile(`<img ([^>]*)>`)
 35
 36var lighter = synlight.New(synlight.Options{Format: synlight.HTML})
 37
 38func markitzero(s string) string {
 39	// prepare the string
 40	s = strings.TrimSpace(s)
 41	s = strings.Replace(s, "\r", "", -1)
 42
 43	// save away the code blocks so we don't mess them up further
 44	var bigcodes, lilcodes, images []string
 45	s = re_bigcoder.ReplaceAllStringFunc(s, func(code string) string {
 46		bigcodes = append(bigcodes, code)
 47		return "``````"
 48	})
 49	s = re_coder.ReplaceAllStringFunc(s, func(code string) string {
 50		lilcodes = append(lilcodes, code)
 51		return "`x`"
 52	})
 53	s = re_imgfix.ReplaceAllStringFunc(s, func(img string) string {
 54		images = append(images, img)
 55		return "<img x>"
 56	})
 57
 58	// fewer side effects than html.EscapeString
 59	buf := make([]byte, 0, len(s))
 60	for _, c := range []byte(s) {
 61		switch c {
 62		case '&':
 63			buf = append(buf, []byte("&amp;")...)
 64		case '<':
 65			buf = append(buf, []byte("&lt;")...)
 66		case '>':
 67			buf = append(buf, []byte("&gt;")...)
 68		default:
 69			buf = append(buf, c)
 70		}
 71	}
 72	s = string(buf)
 73
 74	// mark it zero
 75	s = re_link.ReplaceAllStringFunc(s, linkreplacer)
 76	s = re_zerolink.ReplaceAllString(s, `<a href="$2">$1</a>`)
 77	s = re_bolder.ReplaceAllString(s, "$1<b>$2</b>$3")
 78	s = re_italicer.ReplaceAllString(s, "$1<i>$2</i>$3")
 79	s = re_quoter.ReplaceAllString(s, "<blockquote>$1</blockquote><p>")
 80
 81	// restore images
 82	s = strings.Replace(s, "&lt;img x&gt;", "<img x>", -1)
 83	s = re_imgfix.ReplaceAllStringFunc(s, func(string) string {
 84		img := images[0]
 85		images = images[1:]
 86		return img
 87	})
 88
 89	// now restore the code blocks
 90	s = re_coder.ReplaceAllStringFunc(s, func(string) string {
 91		code := lilcodes[0]
 92		lilcodes = lilcodes[1:]
 93		code = html.EscapeString(code)
 94		return code
 95	})
 96	s = re_bigcoder.ReplaceAllStringFunc(s, func(string) string {
 97		code := bigcodes[0]
 98		bigcodes = bigcodes[1:]
 99		m := re_bigcoder.FindStringSubmatch(code)
100		return "<pre><code>" + lighter.HighlightString(m[2], m[1]) + "</code></pre><p>"
101	})
102	s = re_coder.ReplaceAllString(s, "<code>$1</code>")
103
104	// some final fixups
105	s = strings.Replace(s, "\n", "<br>", -1)
106	s = strings.Replace(s, "<br><blockquote>", "<blockquote>", -1)
107	s = strings.Replace(s, "<br><pre>", "<pre>", -1)
108	s = strings.Replace(s, "<p><br>", "<p>", -1)
109	return s
110}
111
112func linkreplacer(url string) string {
113	if url[0:2] == "](" {
114		return url
115	}
116	prefix := ""
117	for !strings.HasPrefix(url, "http") {
118		prefix += url[0:1]
119		url = url[1:]
120	}
121	addparen := false
122	adddot := false
123	if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
124		url = url[:len(url)-1]
125		addparen = true
126	}
127	if strings.HasSuffix(url, ".") {
128		url = url[:len(url)-1]
129		adddot = true
130	}
131	url = fmt.Sprintf(`<a href="%s">%s</a>`, url, url)
132	if adddot {
133		url += "."
134	}
135	if addparen {
136		url += ")"
137	}
138	return prefix + url
139}