syntax highlighting because why not. go code or go home.
Ted Unangst tedu@tedunangst.com
Thu, 10 Oct 2019 00:40:29 -0400
5 files changed,
42 insertions(+),
8 deletions(-)
M
fun.go
→
fun.go
@@ -32,9 +32,24 @@ "humungus.tedunangst.com/r/webs/htfilter"
"humungus.tedunangst.com/r/webs/httpsig" ) +var allowedclasses = make(map[string]bool) + +func init() { + allowedclasses["kw"] = true + allowedclasses["bi"] = true + allowedclasses["st"] = true + allowedclasses["nm"] = true + allowedclasses["tp"] = true + allowedclasses["op"] = true + allowedclasses["cm"] = true + allowedclasses["al"] = true + allowedclasses["dl"] = true +} + func reverbolate(userid int64, honks []*Honk) { filt := htfilter.New() filt.Imager = replaceimg + filt.SpanClasses = allowedclasses for _, h := range honks { h.What += "ed" if h.What == "tonked" {
M
go.mod
→
go.mod
@@ -7,5 +7,5 @@ github.com/mattn/go-runewidth v0.0.4
golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 golang.org/x/net v0.0.0-20190620200207-3b0461eec859 humungus.tedunangst.com/r/go-sqlite3 v1.1.3 - humungus.tedunangst.com/r/webs v0.6.8 + humungus.tedunangst.com/r/webs v0.6.9 )
M
go.sum
→
go.sum
@@ -23,3 +23,5 @@ humungus.tedunangst.com/r/go-sqlite3 v1.1.3 h1:G2N4wzDS0NbuvrZtQJhh4F+3X+s7BF8b9ga8k38geUI=
humungus.tedunangst.com/r/go-sqlite3 v1.1.3/go.mod h1:FtEEmQM7U2Ey1TuEEOyY1BmphTZnmiEjPsNLEAkpf/M= humungus.tedunangst.com/r/webs v0.6.8 h1:0Xn6+iHZO8F4R3jhkql0qBR8nh5v9D59xG2vWQJzoa8= humungus.tedunangst.com/r/webs v0.6.8/go.mod h1:Ho+nmafD/aUWF7LnH+Yl2/b0ob7f2pCkXm4onteWvLE= +humungus.tedunangst.com/r/webs v0.6.9 h1:bW2rBRZTBgov2yNUCvQ09dAK9oLfQ9A84QkMRLwaXZM= +humungus.tedunangst.com/r/webs v0.6.9/go.mod h1:Ho+nmafD/aUWF7LnH+Yl2/b0ob7f2pCkXm4onteWvLE=
M
markitzero.go
→
markitzero.go
@@ -21,22 +21,23 @@ "regexp"
"strings" "golang.org/x/net/html" + "humungus.tedunangst.com/r/webs/synlight" ) var re_bolder = regexp.MustCompile(`(^|\W)\*\*([\w\s,.!?':_-]+)\*\*($|\W)`) var re_italicer = regexp.MustCompile(`(^|\W)\*([\w\s,.!?':_-]+)\*($|\W)`) -var re_bigcoder = regexp.MustCompile("```\n?((?s:.*?))\n?```\n?") +var re_bigcoder = regexp.MustCompile("```(.*)\n?((?s:.*?))\n?```\n?") var re_coder = regexp.MustCompile("`([^`]*)`") var re_quoter = regexp.MustCompile(`(?m:^> (.*)\n?)`) var re_link = regexp.MustCompile(`.?.?https?://[^\s"]+[\w/)]`) var re_zerolink = regexp.MustCompile(`\[([^]]*)\]\(([^)]*\)?)\)`) + +var lighter = synlight.New(synlight.Options{Format: synlight.HTML}) func markitzero(s string) string { // prepare the string s = strings.TrimSpace(s) s = strings.Replace(s, "\r", "", -1) - s = html.EscapeString(s) - s = strings.Replace(s, "'", "'", -1) // dammit go // save away the code blocks so we don't mess them up further var bigcodes, lilcodes []string@@ -48,6 +49,9 @@ s = re_coder.ReplaceAllStringFunc(s, func(code string) string {
lilcodes = append(lilcodes, code) return "`x`" }) + + s = html.EscapeString(s) + s = strings.Replace(s, "'", "'", -1) // dammit go // mark it zero s = re_bolder.ReplaceAllString(s, "$1<b>$2</b>$3")@@ -57,17 +61,18 @@ s = re_link.ReplaceAllStringFunc(s, linkreplacer)
s = re_zerolink.ReplaceAllString(s, `<a class="mention u-url" href="$2">$1</a>`) // now restore the code blocks - s = re_coder.ReplaceAllStringFunc(s, func(s string) string { + s = re_coder.ReplaceAllStringFunc(s, func(string) string { code := lilcodes[0] lilcodes = lilcodes[1:] + code = html.EscapeString(code) return code }) - s = re_bigcoder.ReplaceAllStringFunc(s, func(s string) string { + s = re_bigcoder.ReplaceAllStringFunc(s, func(string) string { code := bigcodes[0] bigcodes = bigcodes[1:] - return code + m := re_bigcoder.FindStringSubmatch(code) + return "<pre><code>" + lighter.HighlightString(m[2], m[1]) + "</code></pre><p>" }) - s = re_bigcoder.ReplaceAllString(s, "<pre><code>$1</code></pre><p>") s = re_coder.ReplaceAllString(s, "<code>$1</code>") // some final fixups
M
views/style.css
→
views/style.css
@@ -193,6 +193,18 @@ margin-top: 2em;
} .noise { line-height: 1.4; + code { + .kw { font-weight: bold; } + .bi { font-weight: bold; } + .st { color: $fg-subtle; } + .nm { color: #ba88ff; } + .op { color: #ba88ff; } + .tp { font-weight: bold; } + .cm { color: $fg-subtle; font-style: italic; } + .al { } + .dl { } + + } } details.actions summary { color: $fg-subtle;