Skip to content

Commit

Permalink
Add bbcode, HTML reparsing function for imageboard admins
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Merrell committed Feb 11, 2018
1 parent 4d93f8b commit 46a3e8a
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ GOCHAN_DEBUG=1
GOCHAN_VERBOSE=2
GOCHAN_VERBOSITY=0 # This is set by "make release/debug/verbose"

GOCHAN_VERSION=1.8.2
GOCHAN_VERSION=1.9.0
GOCHAN_BUILDTIME=$(shell date +%y%m%d.%H%M)
ifeq ($(GOOS), windows)
GOCHAN_BIN=gochan.exe
Expand Down
2 changes: 1 addition & 1 deletion dist.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

VERSION=1.8.2
VERSION=1.9.0
GOOS_ORIG=$GOOS

function copyStuff {
Expand Down
4 changes: 1 addition & 3 deletions html/css/global/img.css
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ table#pages td {
}

.post-text {
white-space: pre-wrap;
padding: 8px;
}

Expand Down Expand Up @@ -232,8 +231,7 @@ table#pages td {
}

.dropdown-button {
display: inline;
padding: 4px;
padding: 4px 8px;
float: right;
}

Expand Down
2 changes: 1 addition & 1 deletion html/error/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<h1>404: File not found</h1>
<img src="/error/lol 404.gif" border="0" alt="">
<p>The requested file could not be found on this server. Are you just typing random stuff in the address bar? If you followed a link from this site here, then post <a href="/site">here</a></p>
<hr><address>http://gochan.org powered by Gochan v1.8.2</address>
<hr><address>http://gochan.org powered by Gochan v1.9.0</address>
</body>
</html>
2 changes: 1 addition & 1 deletion html/error/500.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<h1>500: Internal Server error</h1>
<img src="/error/derpy server.gif" border="0" alt="">
<p>The server encountered an error while trying to serve the page, and we apologize for the inconvenience. The <a href="https://en.wikipedia.org/wiki/Idiot">system administrator</a> will try to fix things as soon has he/she/it can.</p>
<hr><address>http://gochan.org powered by Gochan v1.8.2</address>
<hr><address>http://gochan.org powered by Gochan v1.9.0</address>
</body>
</html>
4 changes: 2 additions & 2 deletions html/javascript/gochan.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ var DropDownMenu = function(title,menu_html) {
this.menuHTML = menu_html;
this.button = new TopBarButton(title, function() {
topbar.after("<div id=\""+title.toLowerCase()+"\" class=\"dropdown-menu\">"+menu_html+"</div>");
$jq("a#"+title.toLowerCase()).children(0).html(title+up_arrow_symbol);
$jq("a#"+title.toLowerCase() + "-menu").children(0).html(title+up_arrow_symbol);
$jq("div#"+title.toLowerCase()).css({
top:topbar.height()
});
}, function() {
$jq("div#"+title.toLowerCase() + ".dropdown-menu").remove();
$jq("a#"+title.toLowerCase()).children(0).html(title+down_arrow_symbol);
$jq("a#"+title.toLowerCase() + "-menu").children(0).html(title+down_arrow_symbol);
});
}

Expand Down
34 changes: 33 additions & 1 deletion src/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,12 +791,13 @@ var manage_functions = map[string]ManageFunction{
"<a href=\"javascript:void(0)\" id=\"announcements\" class=\"staffmenu-item\">Announcements</a><br />\n"
if rank == 3 {
html += "<b>Admin stuff</b><br />\n<a href=\"javascript:void(0)\" id=\"staff\" class=\"staffmenu-item\">Manage staff</a><br />\n" +
"<a href=\"javascript:void(0)\" id=\"purgeeverything\" class=\"staffmenu-item\">Purge everything!</a><br />\n" +
//"<a href=\"javascript:void(0)\" id=\"purgeeverything\" class=\"staffmenu-item\">Purge everything!</a><br />\n" +
"<a href=\"javascript:void(0)\" id=\"executesql\" class=\"staffmenu-item\">Execute SQL statement(s)</a><br />\n" +
"<a href=\"javascript:void(0)\" id=\"cleanup\" class=\"staffmenu-item\">Run cleanup</a><br />\n" +
"<a href=\"javascript:void(0)\" id=\"rebuildall\" class=\"staffmenu-item\">Rebuild all</a><br />\n" +
"<a href=\"javascript:void(0)\" id=\"rebuildfront\" class=\"staffmenu-item\">Rebuild front page</a><br />\n" +
"<a href=\"javascript:void(0)\" id=\"rebuildboards\" class=\"staffmenu-item\">Rebuild board pages</a><br />\n" +
"<a href=\"javascript:void(0)\" id=\"reparsehtml\" class=\"staffmenu-item\">Reparse all posts</a><br />\n" +
"<a href=\"javascript:void(0)\" id=\"boards\" class=\"staffmenu-item\">Add/edit/delete boards</a><br />\n"
}
if rank >= 2 {
Expand Down Expand Up @@ -832,6 +833,37 @@ var manage_functions = map[string]ManageFunction{
initTemplates()
return buildBoards(true, 0)
}},
"reparsehtml": {
Permissions: 3,
Callback: func() (html string) {
posts, err := getPostArr(map[string]interface{}{
"deleted_timestamp": nil_timestamp,
}, "")
if err != nil {
html += err.Error() + "<br />"
return
}

for _, postInter := range posts {
post := postInter.(PostTable)
stmt, err := db.Prepare("UPDATE `" + config.DBprefix + "posts` SET `message` = ? WHERE `id` = ? AND `boardid` = ?")
if err != nil {
html += err.Error() + "<br />"
return
}
defer func() {
if stmt != nil {
stmt.Close()
}
}()
stmt.Exec(formatMessage(post.MessageText), post.ID, post.BoardID)
}
html += "Done reparsing HTML<hr />" +
buildFrontPage() + "<hr />\n" +
buildBoardListJSON() + "<hr />\n" +
buildBoards(true, 0) + "<hr />\n"
return
}},
"recentposts": {
Permissions: 1,
Callback: func() (html string) {
Expand Down
12 changes: 5 additions & 7 deletions src/posting.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,8 +1008,7 @@ func makePost(w http.ResponseWriter, r *http.Request, data interface{}) {
serveErrorPage(w, "Post body is too long")
return
}
post.MessageHTML = html.EscapeString(post.MessageText)
formatMessage(&post)
post.MessageHTML = formatMessage(post.MessageText)

post.Password = md5Sum(request.FormValue("postpassword"))

Expand Down Expand Up @@ -1335,11 +1334,10 @@ func makePost(w http.ResponseWriter, r *http.Request, data interface{}) {
benchmarkTimer("makePost", startTime, false)
}

func formatMessage(post *PostTable) {
message := post.MessageHTML

func formatMessage(message string) string {
message = bbcompiler.Compile(message)
// prepare each line to be formatted
postLines := strings.Split(message, "\\r\\n")
postLines := strings.Split(message, "<br>")
for i, line := range postLines {
trimmedLine := strings.TrimSpace(line)
//lineWords := regexp.MustCompile("\\s").Split(trimmedLine, -1)
Expand Down Expand Up @@ -1377,5 +1375,5 @@ func formatMessage(post *PostTable) {
}
postLines[i] = line
}
post.MessageHTML = strings.Join(postLines, "<br />")
return strings.Join(postLines, "<br />")
}
10 changes: 10 additions & 0 deletions src/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"path"
"time"

"github.com/frustra/bbcode"
)

var (
Expand All @@ -16,6 +18,7 @@ var (
errorLog *log.Logger
modLog *log.Logger
readBannedIPs []string
bbcompiler bbcode.Compiler
)

type RecentPost struct {
Expand Down Expand Up @@ -662,6 +665,13 @@ func initConfig() {
println(0, "RandomSeed not set in gochan.json, halting.")
os.Exit(2)
}
bbcompiler = bbcode.NewCompiler(true, true)
bbcompiler.SetTag("center", nil)
bbcompiler.SetTag("code", nil)
bbcompiler.SetTag("color", nil)
bbcompiler.SetTag("img", nil)
bbcompiler.SetTag("quote", nil)
bbcompiler.SetTag("size", nil)

config.Version = version
}

0 comments on commit 46a3e8a

Please sign in to comment.