From b32b7b9dcc1ffe62f606e5e76878a8991995a32f Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Thu, 10 Oct 2019 00:40:29 -0400 Subject: [PATCH] syntax highlighting because why not. go code or go home. --- fun.go | 15 +++++++++++++++ go.mod | 2 +- go.sum | 2 ++ markitzero.go | 19 ++++++++++++------- views/style.css | 12 ++++++++++++ 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/fun.go b/fun.go index 08db677..72e6046 100644 --- a/fun.go +++ b/fun.go @@ -32,9 +32,24 @@ import ( "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" { diff --git a/go.mod b/go.mod index efbd987..28de336 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,5 @@ require ( 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 ) diff --git a/go.sum b/go.sum index ff79a71..478c65d 100644 --- a/go.sum +++ b/go.sum @@ -23,3 +23,5 @@ humungus.tedunangst.com/r/go-sqlite3 v1.1.3 h1:G2N4wzDS0NbuvrZtQJhh4F+3X+s7BF8b9 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= diff --git a/markitzero.go b/markitzero.go index 71d1ba7..b315e26 100644 --- a/markitzero.go +++ b/markitzero.go @@ -21,22 +21,23 @@ import ( "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 @@ -49,6 +50,9 @@ func markitzero(s string) string { return "`x`" }) + s = html.EscapeString(s) + s = strings.Replace(s, "'", "'", -1) // dammit go + // mark it zero s = re_bolder.ReplaceAllString(s, "$1$2$3") s = re_italicer.ReplaceAllString(s, "$1$2$3") @@ -57,17 +61,18 @@ func markitzero(s string) string { s = re_zerolink.ReplaceAllString(s, `$1`) // 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 "
" + lighter.HighlightString(m[2], m[1]) + "

" }) - s = re_bigcoder.ReplaceAllString(s, "

$1

") s = re_coder.ReplaceAllString(s, "$1") // some final fixups diff --git a/views/style.css b/views/style.css index ed6f590..bed7ff2 100644 --- a/views/style.css +++ b/views/style.css @@ -193,6 +193,18 @@ input[type=file] { } .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;