From a320b1a2a69e2c3fd3d3de77efc9489b7787fbae Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Sun, 6 Oct 2019 15:01:23 -0400 Subject: [PATCH] rework markdown module, prepare for embetterment --- activity.go | 2 +- fun.go | 78 +--------------------------------------------- fun_test.go | 15 --------- markitzero_test.go | 15 +++++++++ web.go | 2 +- 5 files changed, 18 insertions(+), 94 deletions(-) delete mode 100644 fun_test.go create mode 100644 markitzero_test.go diff --git a/activity.go b/activity.go index 0206654..81d4dc7 100644 --- a/activity.go +++ b/activity.go @@ -1153,7 +1153,7 @@ func honkworldwide(user *WhatAbout, honk *Honk) { } func asjonker(user *WhatAbout) junk.Junk { - about := obfusbreak(user.About) + about := markitzero(user.About) j := junk.New() j["@context"] = itiswhatitis diff --git a/fun.go b/fun.go index 265229f..fb4ba81 100644 --- a/fun.go +++ b/fun.go @@ -172,7 +172,7 @@ func translate(honk *Honk) { noise = strings.TrimSpace(noise) noise = quickrename(noise, honk.UserID) - noise = obfusbreak(noise) + noise = markitzero(noise) honk.Noise = noise honk.Onts = oneofakind(ontologies(honk.Noise)) @@ -316,82 +316,6 @@ func memetize(honk *Honk) { 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$2$3") - s = re_italicer.ReplaceAllString(s, "$1$2$3") - s = re_quoter.ReplaceAllString(s, "
$1

") - 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, "

$1

") - s = re_coder.ReplaceAllString(s, "$1") - 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, "'", "'", -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(`%s`, url, url) - if adddot { - url += "." - } - if addparen { - url += ")" - } - return url - } - s = re_link.ReplaceAllStringFunc(s, linkfn) - - s = markitzero(s) - - s = strings.Replace(s, "\n", "
", -1) - return s -} - var re_quickmention = regexp.MustCompile("(^| )@[[:alnum:]]+ ") func quickrename(s string, userid int64) string { diff --git a/fun_test.go b/fun_test.go deleted file mode 100644 index 49c6363..0000000 --- a/fun_test.go +++ /dev/null @@ -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 https://example.com/ with bold text` - - tmp := obfusbreak(input) - if tmp != output { - t.Errorf("%s is not %s", tmp, output) - } -} diff --git a/markitzero_test.go b/markitzero_test.go new file mode 100644 index 0000000..a26b234 --- /dev/null +++ b/markitzero_test.go @@ -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 https://example.com/ with bold text` + + result := markitzero(input) + if result != output { + t.Errorf("\nexpected:\n%s\noutput:\n%s", output, result) + } +} diff --git a/web.go b/web.go index 7db75aa..9d6356b 100644 --- a/web.go +++ b/web.go @@ -527,7 +527,7 @@ func showuser(w http.ResponseWriter, r *http.Request) { filt := htfilter.New() templinfo["Name"] = user.Name whatabout := user.About - whatabout = obfusbreak(user.About) + whatabout = markitzero(user.About) templinfo["WhatAbout"], _ = filt.String(whatabout) templinfo["ServerMessage"] = "" templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)