rework markdown module, prepare for embetterment

This commit is contained in:
Ted Unangst 2019-10-06 15:01:23 -04:00
parent 271e7d98ce
commit a320b1a2a6
5 changed files with 18 additions and 94 deletions

View File

@ -1153,7 +1153,7 @@ func honkworldwide(user *WhatAbout, honk *Honk) {
} }
func asjonker(user *WhatAbout) junk.Junk { func asjonker(user *WhatAbout) junk.Junk {
about := obfusbreak(user.About) about := markitzero(user.About)
j := junk.New() j := junk.New()
j["@context"] = itiswhatitis j["@context"] = itiswhatitis

78
fun.go
View File

@ -172,7 +172,7 @@ func translate(honk *Honk) {
noise = strings.TrimSpace(noise) noise = strings.TrimSpace(noise)
noise = quickrename(noise, honk.UserID) noise = quickrename(noise, honk.UserID)
noise = obfusbreak(noise) noise = markitzero(noise)
honk.Noise = noise honk.Noise = noise
honk.Onts = oneofakind(ontologies(honk.Noise)) honk.Onts = oneofakind(ontologies(honk.Noise))
@ -316,82 +316,6 @@ func memetize(honk *Honk) {
honk.Noise = re_memes.ReplaceAllStringFunc(honk.Noise, repl) honk.Noise = re_memes.ReplaceAllStringFunc(honk.Noise, repl)
} }
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_coder = regexp.MustCompile("`([^`]*)`")
var re_quoter = regexp.MustCompile(`(?m:^> (.*)\n?)`)
func markitzero(s string) string {
var bigcodes []string
bigsaver := func(code string) string {
bigcodes = append(bigcodes, code)
return "``````"
}
s = re_bigcoder.ReplaceAllStringFunc(s, bigsaver)
var lilcodes []string
lilsaver := func(code string) string {
lilcodes = append(lilcodes, code)
return "`x`"
}
s = re_coder.ReplaceAllStringFunc(s, lilsaver)
s = re_bolder.ReplaceAllString(s, "$1<b>$2</b>$3")
s = re_italicer.ReplaceAllString(s, "$1<i>$2</i>$3")
s = re_quoter.ReplaceAllString(s, "<blockquote>$1</blockquote><p>")
lilun := func(s string) string {
code := lilcodes[0]
lilcodes = lilcodes[1:]
return code
}
s = re_coder.ReplaceAllStringFunc(s, lilun)
bigun := func(s string) string {
code := bigcodes[0]
bigcodes = bigcodes[1:]
return code
}
s = re_bigcoder.ReplaceAllStringFunc(s, bigun)
s = re_bigcoder.ReplaceAllString(s, "<pre><code>$1</code></pre><p>")
s = re_coder.ReplaceAllString(s, "<code>$1</code>")
return s
}
func obfusbreak(s string) string {
s = strings.TrimSpace(s)
s = strings.Replace(s, "\r", "", -1)
s = html.EscapeString(s)
// dammit go
s = strings.Replace(s, "&#39;", "'", -1)
linkfn := func(url string) string {
if url[0] == '@' {
return url
}
addparen := false
adddot := false
if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
url = url[:len(url)-1]
addparen = true
}
if strings.HasSuffix(url, ".") {
url = url[:len(url)-1]
adddot = true
}
url = fmt.Sprintf(`<a class="mention u-url" href="%s">%s</a>`, url, url)
if adddot {
url += "."
}
if addparen {
url += ")"
}
return url
}
s = re_link.ReplaceAllStringFunc(s, linkfn)
s = markitzero(s)
s = strings.Replace(s, "\n", "<br>", -1)
return s
}
var re_quickmention = regexp.MustCompile("(^| )@[[:alnum:]]+ ") var re_quickmention = regexp.MustCompile("(^| )@[[:alnum:]]+ ")
func quickrename(s string, userid int64) string { func quickrename(s string, userid int64) string {

View File

@ -1,15 +0,0 @@
package main
import (
"testing"
)
func TestObfusbreak(t *testing.T) {
input := `link to https://example.com/ with **bold** text`
output := `link to <a href="https://example.com/">https://example.com/</a> with <b>bold</b> text`
tmp := obfusbreak(input)
if tmp != output {
t.Errorf("%s is not %s", tmp, output)
}
}

15
markitzero_test.go Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"testing"
)
func TestMarkitzero(t *testing.T) {
input := `link to https://example.com/ with **bold** text`
output := `link to <a class="mention u-url" href="https://example.com/">https://example.com/</a> with <b>bold</b> text`
result := markitzero(input)
if result != output {
t.Errorf("\nexpected:\n%s\noutput:\n%s", output, result)
}
}

2
web.go
View File

@ -527,7 +527,7 @@ func showuser(w http.ResponseWriter, r *http.Request) {
filt := htfilter.New() filt := htfilter.New()
templinfo["Name"] = user.Name templinfo["Name"] = user.Name
whatabout := user.About whatabout := user.About
whatabout = obfusbreak(user.About) whatabout = markitzero(user.About)
templinfo["WhatAbout"], _ = filt.String(whatabout) templinfo["WhatAbout"], _ = filt.String(whatabout)
templinfo["ServerMessage"] = "" templinfo["ServerMessage"] = ""
templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r) templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)