From cc76d399e556c8ea3c89f7daa22233cea460d891 Mon Sep 17 00:00:00 2001 From: Azareal Date: Thu, 7 Nov 2019 07:15:43 +1000 Subject: [PATCH] Simple spoilers. --- common/parser.go | 13 ++++++------ extend/plugin_bbcode.go | 13 ++++++++---- extend/plugin_markdown.go | 22 ++++++++++++++------ parser_test.go | 2 ++ plugin_test.go | 16 +++++++------- public/global.js | 8 +++++++ themes/cosora/public/main.css | 19 +++++++++++++++++ themes/nox/public/main.css | 28 ++++++++++++++++++------- themes/shadow/public/main.css | 20 +++++++++++++++++- themes/tempra_simple/public/main.css | 31 +++++++++++++++++++++++----- 10 files changed, 135 insertions(+), 37 deletions(-) diff --git a/common/parser.go b/common/parser.go index 6ebb2059..b0c97f97 100644 --- a/common/parser.go +++ b/common/parser.go @@ -143,14 +143,14 @@ func PreparseMessage(msg string) string { // There are a few useful cases for having spaces, but I'd like to stop the WYSIWYG from inserting random lines here and there msg = SanitiseBody(msg) - var runes = []rune(msg) + runes := []rune(msg) msg = "" // TODO: We can maybe reduce the size of this by using an offset? // TODO: Move some of these closures out of this function to make things a little more efficient - var allowedTags = [][]string{ + allowedTags := [][]string{ 'e': []string{"m"}, - 's': []string{"", "trong", "pan"}, + 's': []string{"", "trong", "poiler", "pan"}, 'd': []string{"el"}, 'u': []string{""}, 'b': []string{"", "lockquote"}, @@ -159,7 +159,7 @@ func PreparseMessage(msg string) string { //'p': []string{""}, 'g': []string{""}, // Quick and dirty fix for Grammarly } - var buildLitMatch = func(tag string) func(*TagToAction, bool, int, []rune) (int, string) { + buildLitMatch := func(tag string) func(*TagToAction, bool, int, []rune) (int, string) { return func(action *TagToAction, open bool, _ int, _ []rune) (int, string) { if open { action.Depth++ @@ -172,11 +172,12 @@ func PreparseMessage(msg string) string { return -1, "" } } - var tagToAction = [][]*TagToAction{ + tagToAction := [][]*TagToAction{ 'e': []*TagToAction{&TagToAction{"m", buildLitMatch("em"), 0, false}}, 's': []*TagToAction{ &TagToAction{"", buildLitMatch("del"), 0, false}, &TagToAction{"trong", buildLitMatch("strong"), 0, false}, + &TagToAction{"poiler", buildLitMatch("spoiler"), 0, false}, // Hides the span tags Trumbowyg loves blasting out randomly &TagToAction{"pan", func(act *TagToAction, open bool, i int, runes []rune) (int, string) { if open { @@ -727,7 +728,7 @@ func validatedURLBytes(data []byte) (url []byte) { // ? - There should only be one : and that's only if the URL is on a non-standard port. Same for ?s. for ; datalen > i; i++ { - ch := data[i] //char + ch := data[i] // char if ch != '\\' && ch != '_' && ch != ':' && ch != '?' && ch != '&' && ch != '=' && ch != ';' && ch != '@' && ch != '#' && ch != ']' && !(ch > 44 && ch < 58) && !(ch > 64 && ch < 92) && !(ch > 96 && ch < 123) { // 90 is Z, 91 is [ return InvalidURL } diff --git a/extend/plugin_bbcode.go b/extend/plugin_bbcode.go index 0a7f012f..55a8008c 100644 --- a/extend/plugin_bbcode.go +++ b/extend/plugin_bbcode.go @@ -24,13 +24,14 @@ var bbcodeURL *regexp.Regexp var bbcodeURLLabel *regexp.Regexp var bbcodeQuotes *regexp.Regexp var bbcodeCode *regexp.Regexp +var bbcodeSpoiler *regexp.Regexp func init() { c.Plugins.Add(&c.Plugin{UName: "bbcode", Name: "BBCode", Author: "Azareal", URL: "https://github.com/Azareal", Init: InitBbcode, Deactivate: deactivateBbcode}) } -func InitBbcode(plugin *c.Plugin) error { - plugin.AddHook("parse_assign", BbcodeFullParse) +func InitBbcode(pl *c.Plugin) error { + pl.AddHook("parse_assign", BbcodeFullParse) bbcodeInvalidNumber = []byte("[Invalid Number]") bbcodeNoNegative = []byte("[No Negative Numbers]") @@ -46,13 +47,14 @@ func InitBbcode(plugin *c.Plugin) error { bbcodeURLLabel = regexp.MustCompile(`(?s)\[url=` + urlpattern + `\](.*)\[/url\]`) bbcodeQuotes = regexp.MustCompile(`\[quote\](.*)\[/quote\]`) bbcodeCode = regexp.MustCompile(`\[code\](.*)\[/code\]`) + bbcodeSpoiler = regexp.MustCompile(`\[spoiler\](.*)\[/spoiler\]`) bbcodeRandom = rand.New(rand.NewSource(time.Now().UnixNano())) return nil } -func deactivateBbcode(plugin *c.Plugin) { - plugin.RemoveHook("parse_assign", BbcodeFullParse) +func deactivateBbcode(pl *c.Plugin) { + pl.RemoveHook("parse_assign", BbcodeFullParse) } func BbcodeRegexParse(msg string) string { @@ -63,6 +65,7 @@ func BbcodeRegexParse(msg string) string { msg = bbcodeURL.ReplaceAllString(msg, "$1$2//$3") msg = bbcodeURLLabel.ReplaceAllString(msg, "$4") msg = bbcodeQuotes.ReplaceAllString(msg, "
$1
") + msg = bbcodeSpoiler.ReplaceAllString(msg, "$1") msg = bbcodeH1.ReplaceAllString(msg, "

$1

") //msg = bbcodeCode.ReplaceAllString(msg,"$1") return msg @@ -199,6 +202,7 @@ func BbcodeParseWithoutCode(msg string) string { msg = string(msgbytes) msg = bbcodeURL.ReplaceAllString(msg, "
$1$2//$3") msg = bbcodeURLLabel.ReplaceAllString(msg, "$4") + msg = bbcodeSpoiler.ReplaceAllString(msg, "$1") msg = bbcodeQuotes.ReplaceAllString(msg, "
$1
") return bbcodeCode.ReplaceAllString(msg, "$1") } @@ -328,6 +332,7 @@ func BbcodeFullParse(msg string) string { msg = bbcodeURLLabel.ReplaceAllString(msg, "
$4") msg = bbcodeQuotes.ReplaceAllString(msg, "
$1
") msg = bbcodeCode.ReplaceAllString(msg, "$1") + msg = bbcodeSpoiler.ReplaceAllString(msg, "$1") msg = bbcodeH1.ReplaceAllString(msg, "

$1

") } else { msg = string(msgbytes[0 : len(msgbytes)-10]) diff --git a/extend/plugin_markdown.go b/extend/plugin_markdown.go index 1a404a87..0e138523 100644 --- a/extend/plugin_markdown.go +++ b/extend/plugin_markdown.go @@ -19,6 +19,8 @@ var markdownStrikeTagOpen []byte var markdownStrikeTagClose []byte var markdownQuoteTagOpen []byte var markdownQuoteTagClose []byte +var markdownSpoilerTagOpen []byte +var markdownSpoilerTagClose []byte var markdownH1TagOpen []byte var markdownH1TagClose []byte @@ -26,8 +28,8 @@ func init() { c.Plugins.Add(&c.Plugin{UName: "markdown", Name: "Markdown", Author: "Azareal", URL: "https://github.com/Azareal", Init: InitMarkdown, Deactivate: deactivateMarkdown}) } -func InitMarkdown(plugin *c.Plugin) error { - plugin.AddHook("parse_assign", MarkdownParse) +func InitMarkdown(pl *c.Plugin) error { + pl.AddHook("parse_assign", MarkdownParse) markdownUnclosedElement = []byte("[Unclosed Element]") @@ -41,13 +43,15 @@ func InitMarkdown(plugin *c.Plugin) error { markdownStrikeTagClose = []byte("") markdownQuoteTagOpen = []byte("
") markdownQuoteTagClose = []byte("
") + markdownSpoilerTagOpen = []byte("") + markdownSpoilerTagClose = []byte("") markdownH1TagOpen = []byte("

") markdownH1TagClose = []byte("

") return nil } -func deactivateMarkdown(plugin *c.Plugin) { - plugin.RemoveHook("parse_assign", MarkdownParse) +func deactivateMarkdown(pl *c.Plugin) { + pl.RemoveHook("parse_assign", MarkdownParse) } // An adapter for the parser, so that the parser can call itself recursively. @@ -251,6 +255,12 @@ func _markdownParse(msg string, n int) string { if breaking { break } + // TODO: Might need to be double pipe + case '|': + simpleMatch('|', markdownSpoilerTagOpen, markdownSpoilerTagClose) + if breaking { + break + } case 10: // newline if (index + 1) >= len(msg) { break @@ -284,8 +294,8 @@ func _markdownParse(msg string, n int) string { return string(outbytes) } -func isMarkdownStartChar(char byte) bool { - return char == '\\' || char == '~' || char == '_' || char == 10 || char == '`' || char == '*' +func isMarkdownStartChar(ch byte) bool { // char + return ch == '\\' || ch == '~' || ch == '_' || ch == 10 || ch == '`' || ch == '*' || ch == '|' } func markdownFindChar(data string, index int, char byte) bool { diff --git a/parser_test.go b/parser_test.go index 4f9e708d..2d43805a 100644 --- a/parser_test.go +++ b/parser_test.go @@ -60,6 +60,8 @@ func TestPreparser(t *testing.T) { l.Add("hi", "hi") l.Add("hi", "hi") l.Add("hi", "hi") + l.Add("hi", "hi") + l.Add("hi", "hi") // Grammarly fix l.Add("hi", "hi") l.Add("hi", "hi") l.Add("hi", "hi") diff --git a/plugin_test.go b/plugin_test.go index 38a2ca59..c10ad1d8 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -59,6 +59,7 @@ func TestBBCodeRender(t *testing.T) { //l.Add("[b][b][b]hi", "[b][b][b]hi") //l.Add("[/b]hi", "[/b]hi") } + l.Add("[spoiler]hi[/spoiler]", "hi") l.Add("[code]hi[/code]", "hi") l.Add("[code][b]hi[/b][/code]", "[b]hi[/b]") l.Add("[code][b]hi[/code][/b]", "[b]hi[/b]") @@ -91,13 +92,13 @@ func TestBBCodeRender(t *testing.T) { t.Error("Expected:", "'"+expects+"'") } } - f("[rand][/rand]","[Invalid Number][rand][/rand]") - f("[rand]-1[/rand]","[No Negative Numbers][rand]-1[/rand]") - f("[rand]-01[/rand]","[No Negative Numbers][rand]-01[/rand]") - f("[rand]NaN[/rand]","[Invalid Number][rand]NaN[/rand]") - f("[rand]Inf[/rand]","[Invalid Number][rand]Inf[/rand]") - f("[rand]+[/rand]","[Invalid Number][rand]+[/rand]") - f("[rand]1+1[/rand]","[Invalid Number][rand]1+1[/rand]") + f("[rand][/rand]", "[Invalid Number][rand][/rand]") + f("[rand]-1[/rand]", "[No Negative Numbers][rand]-1[/rand]") + f("[rand]-01[/rand]", "[No Negative Numbers][rand]-01[/rand]") + f("[rand]NaN[/rand]", "[Invalid Number][rand]NaN[/rand]") + f("[rand]Inf[/rand]", "[Invalid Number][rand]Inf[/rand]") + f("[rand]+[/rand]", "[Invalid Number][rand]+[/rand]") + f("[rand]1+1[/rand]", "[Invalid Number][rand]1+1[/rand]") msg := "[rand]1[/rand]" t.Log("Testing string '" + msg + "'") @@ -240,6 +241,7 @@ func TestMarkdownRender(t *testing.T) { l.Add("~~~", "~~~") l.Add("~~~~", "~~~~") l.Add("~~~~~", "~~~~~") + l.Add("|hi|", "hi") l.Add("__", "__") l.Add("___", "___") l.Add("_ _", " ") diff --git a/public/global.js b/public/global.js index 375d7a39..8285d385 100644 --- a/public/global.js +++ b/public/global.js @@ -905,6 +905,14 @@ function mainInit(){ this.innerText = formattedTime; }); + $("spoiler").addClass("hide_spoil"); + $(".hide_spoil").click(function(event) { + event.stopPropagation(); + event.preventDefault(); + $(this).removeClass("hide_spoil"); + $(this).unbind("click"); + }); + this.onkeyup = function(event) { if(event.which == 37) this.querySelectorAll("#prevFloat a")[0].click(); if(event.which == 39) this.querySelectorAll("#nextFloat a")[0].click(); diff --git a/themes/cosora/public/main.css b/themes/cosora/public/main.css index 09524d57..9c799c0d 100644 --- a/themes/cosora/public/main.css +++ b/themes/cosora/public/main.css @@ -1246,6 +1246,25 @@ red { margin-bottom: 14px; } +.hide_spoil { + background-color: lightgrey; + color: lightgrey; +} +.hide_spoil img { + border: 0; + clip: rect(0 0 0 0); + height: 1px; + margin: -1px; + overflow: hidden; + padding: 50px; + white-space: nowrap; + width: 1px; + background-color: lightgrey; +} +.hide_spoil img { + content: " "; +} + #ip_search_container .rowlist:not(.has_items) { display: block; } diff --git a/themes/nox/public/main.css b/themes/nox/public/main.css index b4f14830..3ba1dc51 100644 --- a/themes/nox/public/main.css +++ b/themes/nox/public/main.css @@ -221,10 +221,7 @@ li a { display: flex; padding: 12px; } -.sidebar .rowblock:not(.topic_list):not(.rowhead):not(.opthead) .rowitem { - margin-left: 12px; -} -.sidebar .search { +.sidebar .rowblock:not(.topic_list):not(.rowhead):not(.opthead) .rowitem, .sidebar .search { margin-left: 12px; } .widget_search:first-child { @@ -253,15 +250,12 @@ li a { text-overflow: ellipsis; overflow: hidden; } -.colstack_right .colstack_item:not(.colstack_head):not(.rowhead) .rowitem:not(:last-child) { +.colstack_right .colstack_item:not(.colstack_head):not(.rowhead) .rowitem:not(:last-child), .rowmsg { margin-bottom: 8px; } .colstack_right .colstack_head:not(:first-child) { margin-top: 16px; } -.rowmsg { - margin-bottom: 8px; -} h1, h2, h3, h4, h5 { -webkit-margin-before: 0; @@ -912,6 +906,24 @@ blockquote:first-child { red { color: red; } +.hide_spoil { + background-color: grey; + color: grey; +} +.hide_spoil img { + border: 0; + clip: rect(0 0 0 0); + height: 1px; + margin: -1px; + overflow: hidden; + padding: 50px; + white-space: nowrap; + width: 1px; + background-color: grey; +} +.hide_spoil img { + content: " "; +} .update_buttons { display: flex; background-color: #444444; diff --git a/themes/shadow/public/main.css b/themes/shadow/public/main.css index d44392b2..2eff2fad 100644 --- a/themes/shadow/public/main.css +++ b/themes/shadow/public/main.css @@ -303,7 +303,6 @@ red { float: right; color: var(--dim-text-color); } - .real_username { float: left; margin-right: 7px; @@ -361,6 +360,25 @@ red { margin-left: 3px; } +.hide_spoil { + background-color: grey; + color: grey; +} +.hide_spoil img { + border: 0; + clip: rect(0 0 0 0); + height: 1px; + margin: -1px; + overflow: hidden; + padding: 50px; + white-space: nowrap; + width: 1px; + background-color: grey; +} +.hide_spoil img { + content: " "; +} + .formrow.real_first_child, .formrow:first-child { margin-top: 8px; } diff --git a/themes/tempra_simple/public/main.css b/themes/tempra_simple/public/main.css index 6dcb652d..4ff2f622 100644 --- a/themes/tempra_simple/public/main.css +++ b/themes/tempra_simple/public/main.css @@ -3,12 +3,10 @@ -moz-box-sizing: border-box; -webkit-box-sizing: border-box; } - body { font-family: arial; padding-bottom: 8px; } - /* Patch for Edge, until they fix emojis in arial x.x */ @supports (-ms-ime-align:auto) { .user_content { font-family: Segoe UI Emoji, arial; } } @@ -227,7 +225,6 @@ main > *:last-child { padding-bottom: 12px; font-size: 16px; } - /*.grid_istat { margin-bottom: 5px; }*/ @@ -285,7 +282,6 @@ h1, h2, h3, h4, h5 { .rowitem:not(:last-child) { border-bottom: 1px solid hsl(0,0%,85%); } - .rowitem a { text-decoration: none; color: black; @@ -378,7 +374,6 @@ h1, h2, h3, h4, h5 { width: 100%; background-color: white; } - /* Clearfix */ .formrow:before, .formrow:after { content: " "; @@ -803,6 +798,32 @@ red { padding-right: 5px; } +.hide_spoil { + background-color: rgb(220,220,220); + color: rgb(220,220,220) !important; +} +.hide_spoil img { + border: 0; + clip: rect(0 0 0 0); + height: 1px; + margin: -1px; + overflow: hidden; + padding: 50px; + white-space: nowrap; + width: 1px; + background-color: rgb(220,220,220); +} +.hide_spoil img { + content: " "; +} +.staff_post .hide_spoil { + background-color: rgb(240,180,240); /*rgb(255, 234, 255)*/ + color: rgb(240,180,240) !important; +} +.staff_post .hide_spoil img { + background-color: rgb(240,180,240); +} + blockquote { border: 1px solid hsl(0, 0%, 80%); background: white;