syntax highlighting because why not. go code or go home.

This commit is contained in:
Ted Unangst 2019-10-10 00:40:29 -04:00
parent 9617011a50
commit b32b7b9dcc
5 changed files with 42 additions and 8 deletions

15
fun.go
View File

@ -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" {

2
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=

View File

@ -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<b>$2</b>$3")
s = re_italicer.ReplaceAllString(s, "$1<i>$2</i>$3")
@ -57,17 +61,18 @@ func markitzero(s string) string {
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

View File

@ -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;