1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-20 09:26:23 -07:00

Replace clunky template rendering with (slightly) less clunky template rendering

This commit is contained in:
Joshua Merrell 2017-10-29 00:59:42 -07:00
parent 66fd647c3e
commit 6edda7b5e1
3 changed files with 126 additions and 74 deletions

View file

@ -8,8 +8,6 @@ import (
"database/sql"
"encoding/json"
"fmt"
"github.com/disintegration/imaging"
"github.com/nyarla/go-crypt"
"html"
"image"
"io/ioutil"
@ -23,6 +21,9 @@ import (
"strings"
"syscall"
"time"
"github.com/disintegration/imaging"
crypt "github.com/nyarla/go-crypt"
)
const (
@ -78,7 +79,6 @@ func buildBoardPages(board *BoardsTable) (html string) {
// start_time := benchmarkTimer("buildBoard"+strconv.Itoa(board.ID), time.Now(), true)
var boardinfo_i []interface{}
var current_page_file *os.File
var interfaces []interface{}
var threads []interface{}
var thread_pages [][]interface{}
var stickied_threads []interface{}
@ -200,16 +200,7 @@ func buildBoardPages(board *BoardsTable) (html string) {
boardinfo_i = nil
boardinfo_i = append(boardinfo_i, board)
// Package up boards, sections, threads, the boardinfo for the template to use.
interfaces = nil
interfaces = append(interfaces, config,
&Wrapper{IName: "boards", Data: all_boards},
&Wrapper{IName: "sections", Data: all_sections},
&Wrapper{IName: "threads", Data: threads},
&Wrapper{IName: "boardinfo", Data: boardinfo_i})
wrapped := &Wrapper{IName: "boardpage", Data: interfaces}
// Write to board.html for the first page.
// Open board.html for writing to the first page.
printf(1, "Current page: %s/%d\n", board.Dir, board.CurrentPage)
board_page_file, err := os.OpenFile(path.Join(config.DocumentRoot, board.Dir, "board.html"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0777)
if err != nil {
@ -220,8 +211,14 @@ func buildBoardPages(board *BoardsTable) (html string) {
return
}
// Run the template, pointing it to the file, and passing in the data required.
err = img_boardpage_tmpl.Execute(board_page_file, wrapped)
// Render board page template to the file,
// packaging the board/section list, threads, and board info
err = renderTemplate(img_boardpage_tmpl, "boardpage", board_page_file,
&Wrapper{IName: "boards", Data: all_boards},
&Wrapper{IName: "sections", Data: all_sections},
&Wrapper{IName: "threads", Data: threads},
&Wrapper{IName: "boardinfo", Data: boardinfo_i},
)
if err != nil {
errortext = "Failed building /" + board.Dir + "/: " + err.Error()
html += errortext + "<br />\n"
@ -262,15 +259,6 @@ func buildBoardPages(board *BoardsTable) (html string) {
boardinfo_i = nil
boardinfo_i = append(boardinfo_i, board)
// Package up boards, sections, threads, the boardinfo for the template to use.
interfaces = nil
interfaces = append(interfaces, config,
&Wrapper{IName: "boards", Data: all_boards},
&Wrapper{IName: "sections", Data: all_sections},
&Wrapper{IName: "threads", Data: page_threads},
&Wrapper{IName: "boardinfo", Data: boardinfo_i})
wrapped := &Wrapper{IName: "boardpage", Data: interfaces}
// Write to board.html for the first page.
var current_page_filepath string
if board.CurrentPage == 0 {
@ -278,6 +266,7 @@ func buildBoardPages(board *BoardsTable) (html string) {
} else {
current_page_filepath = path.Join(config.DocumentRoot, board.Dir, strconv.Itoa(page_num)+".html")
}
current_page_file, err = os.OpenFile(current_page_filepath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0777)
if err != nil {
errortext = "Failed opening board page: " + err.Error()
@ -286,9 +275,13 @@ func buildBoardPages(board *BoardsTable) (html string) {
println(1, errortext)
continue
}
// Run the template, pointing it to the file, and passing in the data required.
err = img_boardpage_tmpl.Execute(current_page_file, wrapped)
// Render the boardpage template, given boards, sections, threads, and board info
err = renderTemplate(img_boardpage_tmpl, "boardpage", current_page_file,
&Wrapper{IName: "boards", Data: all_boards},
&Wrapper{IName: "sections", Data: all_sections},
&Wrapper{IName: "threads", Data: page_threads},
&Wrapper{IName: "boardinfo", Data: boardinfo_i},
)
if err != nil {
errortext = "Failed building /" + board.Dir + "/: " + err.Error()
html += errortext + "<br />\n"
@ -385,8 +378,6 @@ func buildThreadPages(op *PostTable) (html string) {
var board_dir string
var anonymous string
var replies []interface{}
var interfaces []interface{}
var page []interface{}
var current_page_file *os.File
var errortext string
@ -416,13 +407,6 @@ func buildThreadPages(op *PostTable) (html string) {
deleteMatchingFiles(path.Join(config.DocumentRoot, board_dir, "res"), "^"+strconv.Itoa(op.ID)+"p")
op.NumPages = len(thread_pages)
// build main page
page = append([]interface{}{op}, replies...)
interfaces = append(interfaces, config,
&Wrapper{IName: "boards_", Data: all_boards},
&Wrapper{IName: "sections_w", Data: all_sections},
&Wrapper{IName: "posts_w", Data: page})
wrapped := &Wrapper{IName: "threadpage", Data: interfaces}
current_page_filepath := path.Join(config.DocumentRoot, board_dir, "res", strconv.Itoa(op.ID)+".html")
current_page_file, err = os.OpenFile(current_page_filepath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0777)
@ -433,7 +417,12 @@ func buildThreadPages(op *PostTable) (html string) {
error_log.Println(errortext)
return
}
err = img_threadpage_tmpl.Execute(current_page_file, wrapped)
// render main page
err = renderTemplate(img_threadpage_tmpl, "threadpage", current_page_file,
&Wrapper{IName: "boards_", Data: all_boards},
&Wrapper{IName: "sections_w", Data: all_sections},
&Wrapper{IName: "posts_w", Data: append([]interface{}{op}, replies...)},
)
if err != nil {
errortext = "Failed building /" + board_dir + "/res/" + strconv.Itoa(op.ID) + ".html: " + err.Error()
html += errortext + "<br />\n"
@ -498,17 +487,8 @@ func buildThreadPages(op *PostTable) (html string) {
for page_num, page_posts := range thread_pages {
op.CurrentPage = page_num
interfaces = nil
interfaces = append(interfaces, config,
&Wrapper{IName: "boards_", Data: all_boards},
&Wrapper{IName: "sections_w", Data: all_sections},
&Wrapper{IName: "posts_w", Data: page_posts})
wrapped := &Wrapper{IName: "threadpage", Data: interfaces}
var current_page_filepath string
current_page_filepath = path.Join(config.DocumentRoot, board_dir, "res", strconv.Itoa(op.ID)+"p"+strconv.Itoa(op.CurrentPage+1)+".html")
current_page_filepath := path.Join(config.DocumentRoot, board_dir, "res", strconv.Itoa(op.ID)+"p"+strconv.Itoa(op.CurrentPage+1)+".html")
current_page_file, err = os.OpenFile(current_page_filepath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0777)
if err != nil {
errortext = "Failed opening " + current_page_filepath + ": " + err.Error()
@ -517,8 +497,11 @@ func buildThreadPages(op *PostTable) (html string) {
error_log.Println(errortext)
return
}
err = img_threadpage_tmpl.Execute(current_page_file, wrapped)
err = renderTemplate(img_threadpage_tmpl, "threadpage", current_page_file,
&Wrapper{IName: "boards_", Data: all_boards},
&Wrapper{IName: "sections_w", Data: all_sections},
&Wrapper{IName: "posts_w", Data: page_posts},
)
if err != nil {
errortext = "Failed building /" + board_dir + "/" + strconv.Itoa(op.ID) + ": " + err.Error()
html += errortext + "<br />\n"
@ -537,6 +520,7 @@ func buildFrontPage() (html string) {
initTemplates()
var front_arr []interface{}
var recent_posts_arr []interface{}
var errortext string
os.Remove(path.Join(config.DocumentRoot, "index.html"))
front_file, err := os.OpenFile(path.Join(config.DocumentRoot, "index.html"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0777)
@ -605,15 +589,12 @@ func buildFrontPage() (html string) {
recent_posts_arr = append(recent_posts_arr, recent_post)
}
var interfaces []interface{}
interfaces = append(interfaces, config,
err = renderTemplate(front_page_tmpl, "frontpage", front_file,
&Wrapper{IName: "fronts", Data: front_arr},
&Wrapper{IName: "boards", Data: all_boards},
&Wrapper{IName: "sections", Data: all_sections},
&Wrapper{IName: "recent posts", Data: recent_posts_arr})
wrapped := &Wrapper{IName: "frontpage", Data: interfaces}
err = front_page_tmpl.Execute(front_file, wrapped)
&Wrapper{IName: "recent posts", Data: recent_posts_arr},
)
if err != nil {
errortext = "Failed executing front page template: " + err.Error()
error_log.Println(errortext)
@ -1091,13 +1072,10 @@ func makePost(w http.ResponseWriter, r *http.Request, data interface{}) {
}
if len(isbanned) > 0 {
wrapped := &Wrapper{IName: "bans", Data: isbanned}
var banpage_buffer bytes.Buffer
var banpage_html string
banpage_buffer.Write([]byte(""))
err = banpage_tmpl.Execute(&banpage_buffer, wrapped)
err = renderTemplate(banpage_tmpl, "bans", isbanned)
if err != nil {
fmt.Fprintf(writer, banpage_html+err.Error()+"\n</body>\n</html>")
println(1, err.Error())

View file

@ -3,7 +3,8 @@ package main
import (
"bytes"
"fmt"
"html"
"html"
"io"
"io/ioutil"
"net/http"
"os"
@ -87,20 +88,20 @@ var funcMap = template.FuncMap{
}
return msg
},
"truncateString": func(msg string, limit int, ellipsis bool) string {
if len(msg) > limit {
if ellipsis {
return msg[:limit] + "..."
} else {
return msg[:limit]
}
} else {
return msg
}
},
"unescapeString": func(a string) string {
return html.UnescapeString(a)
},
"truncateString": func(msg string, limit int, ellipsis bool) string {
if len(msg) > limit {
if ellipsis {
return msg[:limit] + "..."
} else {
return msg[:limit]
}
} else {
return msg
}
},
"unescapeString": func(a string) string {
return html.UnescapeString(a)
},
"intEq": func(a, b int) bool {
return a == b
},
@ -328,3 +329,14 @@ func getStyleLinks(w http.ResponseWriter, stylesheet string) {
os.Exit(2)
}
}
func renderTemplate(tmpl *template.Template, name string, output io.Writer, wrappers ...*Wrapper) error {
var interfaces []interface{}
interfaces = append(interfaces, config)
for _, wrapper := range wrappers {
interfaces = append(interfaces, wrapper)
}
wrapped := &Wrapper{IName: name, Data: interfaces}
return img_boardpage_tmpl.Execute(output, wrapped)
}

View file

@ -0,0 +1,62 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Gochan Manage Page - Boards</title>
<script type="text/javascript" src="/javascript/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
var styles = [{{range $ii, $style := $config.Styles_img}}{{if gt $ii 0}}, {{end}}"{{$style}}"{{end}}];
var webroot = "{{$config.SiteWebfolder}}";
var thread_type = "board";
</script>
<script type="text/javascript" src="/javascript/gochan.js"></script>
<script type="text/javascript" src="/javascript/manage.js"></script>
<link rel="stylesheet" href="/css/global/img.css" />
{{range $i, $style := $config.Styles_img}}
<link rel="{{if isStyleNotDefault_img $style}}alternate {{end}}stylesheet" href="/css/{{$style}}/img.css" />{{end}}
</head>
<body>
<h1>Manage boards</h1>
<form action="/manage?action=boards" method="POST">
<input type="hidden" name="do" value="existing" />
<select name="boardselect">
<option>Select board...</option>
<option>test</option>
</select>
<input type="submit" value="Edit" /> <input type="submit" value="Delete" /> <input type="submit" value="Go To" />
</form>
<h2>Create new board</h2>
<form action="/manage?action=boards" method="POST">
<input type="hidden" name="do" value="add" />
<table class="board-options">
<tr><th class="option-column">Option</th><th class="value-column">Value</th></tr>
<tr><td>Directory</td><td><input type="text" name="dir" value="" /></td></tr>
<tr><td>Section</td><td><select name="section" selected="0">
<option value="none">Select section...</option>
</select></td></tr>
<tr><td>Order</td><td><input type="text" name="order" value="0" /></td></tr>
<tr><td>Title</td><td><input type="text" name="title" value="" /></td></tr>
<tr><td>Subtitle</td><td><input type="text" name="subtitle" value="" /></td></tr>
<tr><td>Description</td><td><input type="text" name="description" value="" /></td></tr>
<tr><td>Max image size</td><td><input type="text" name="maximagesize" value="4718592" /></td></tr>
<tr><td>Max pages</td><td><input type="text" name="maxpages" value="11" /></td></tr>
<tr><td>Default style</td><td><select name="defaultstyle" selected="">
{{range $i, $style := $config.Styles_img}}
<option value="{{$style}}">{{$style}}</option>{{end}}
</select></td></tr>
<tr><td>Locked</td><td><input type="checkbox" name="locked" /></td></tr>
<tr><td>Forced anonymity</td><td><input type="checkbox" name="forcedanon" /></td></tr>
<tr><td>Anonymous name</td><td><input type="text" name="anonymous" value="Anonymous" /></td></tr>
<tr><td>Max age</td><td><input type="text" name="maxage" value="0"/></td></tr>
<tr><td>Bump limit</td><td><input type="text" name="autosageafter" value="200"/></td></tr>
<tr><td>No images after</td><td><input type="text" name="noimagesafter" value="0"/></td></tr>
<tr><td>Max message length</td><td><input type="text" name="maxmessagelength" value="8192"/></td></tr>
<tr><td>Embeds allowed</td><td><input type="checkbox" name="embedsallowed" /></td></tr>
<tr><td>Redirect to thread</td><td><input type="checkbox" name="redirecttothread" /></td></tr>
<tr><td>Require an uploaded file</td><td><input type="checkbox" name="require_file" checked/></td></tr>
<tr><td>Enable catalog</td><td><input type="checkbox" name="enablecatalog" checked /></td></tr>
</table>
<input type="submit" /></form>
</body>
</html>