From f84eaa653955b3dc06ba3ef8a3129f664c0f12d9 Mon Sep 17 00:00:00 2001 From: Robert Wang Date: Mon, 23 Apr 2018 21:59:00 -0700 Subject: [PATCH] Switch to html/template. --- atomfeed/atomfeed.go | 2 +- page/pages.go | 9 +++++---- page/template.go | 2 +- pttweb.go | 10 +++++----- tmpl_funcs.go | 33 +++++++++++++++++---------------- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/atomfeed/atomfeed.go b/atomfeed/atomfeed.go index 39c7afe..8a1c3fc 100644 --- a/atomfeed/atomfeed.go +++ b/atomfeed/atomfeed.go @@ -3,7 +3,7 @@ package atomfeed import ( "bytes" "fmt" - "text/template" + "html/template" "time" "github.com/ptt/pttweb/pttbbs" diff --git a/page/pages.go b/page/pages.go index b7abd04..7815b5a 100644 --- a/page/pages.go +++ b/page/pages.go @@ -1,6 +1,7 @@ package page import ( + "html/template" "net/http" manpb "github.com/ptt/pttweb/proto/man" @@ -83,8 +84,8 @@ type BbsArticle struct { Description string Board *pttbbs.Board FileName string - ContentHtml string - ContentTailHtml string + Content template.HTML + ContentTail template.HTML ContentTruncated bool PollUrl string LongPollUrl string @@ -106,8 +107,8 @@ type ManArticle struct { Description string Board *pttbbs.Board Path string - ContentHtml string - ContentTailHtml string + Content template.HTML + ContentTail template.HTML ContentTruncated bool } diff --git a/page/template.go b/page/template.go index 95646e4..06ba46a 100644 --- a/page/template.go +++ b/page/template.go @@ -2,9 +2,9 @@ package page import ( "errors" + "html/template" "net/http" "path/filepath" - "text/template" ) type TemplateMap map[string]*template.Template diff --git a/pttweb.go b/pttweb.go index 7347540..7b21fc9 100644 --- a/pttweb.go +++ b/pttweb.go @@ -6,6 +6,7 @@ import ( "errors" "flag" "fmt" + "html/template" "log" "net" "net/http" @@ -17,7 +18,6 @@ import ( "path/filepath" "strconv" "strings" - "text/template" "time" "google.golang.org/grpc" @@ -663,8 +663,8 @@ func handleArticleCommon(c *Context, w http.ResponseWriter, brdname, filename st Description: ar.PreviewContent, Board: brd, FileName: filename, - ContentHtml: string(ar.ContentHtml), - ContentTailHtml: string(ar.ContentTailHtml), + Content: template.HTML(string(ar.ContentHtml)), + ContentTail: template.HTML(string(ar.ContentTailHtml)), ContentTruncated: ar.IsTruncated, PollUrl: pollUrl, LongPollUrl: longPollUrl, @@ -905,8 +905,8 @@ func handleManArticle(c *Context, w http.ResponseWriter, brd *pttbbs.Board, path Description: ar.PreviewContent, Board: brd, Path: path, - ContentHtml: string(ar.ContentHtml), - ContentTailHtml: string(ar.ContentTailHtml), + Content: template.HTML(string(ar.ContentHtml)), + ContentTail: template.HTML(string(ar.ContentTailHtml)), ContentTruncated: ar.IsTruncated, }) } diff --git a/tmpl_funcs.go b/tmpl_funcs.go index 31fb9d2..9ee54ff 100644 --- a/tmpl_funcs.go +++ b/tmpl_funcs.go @@ -1,50 +1,51 @@ package main import ( + "html/template" "strconv" "github.com/ptt/pttweb/pttbbs" ) -func colored_counter(num int) string { +func colored_counter(num int) template.HTML { switch { case num >= 100: - return `` + return template.HTML(``) case num >= 10: - return `` + strconv.Itoa(num) + `` + return template.HTML(`` + strconv.Itoa(num) + ``) case num > 0: - return `` + strconv.Itoa(num) + `` + return template.HTML(`` + strconv.Itoa(num) + ``) case num > -10: return "" case num > -100: - return `X` + strconv.Itoa(-num/10) + `` + return template.HTML(`X` + strconv.Itoa(-num/10) + ``) default: - return `XX` + return template.HTML(`XX`) } } -func decorate_board_nuser(num int) string { +func decorate_board_nuser(num int) template.HTML { switch { case num < 1: return "" case num <= 10: - return strconv.Itoa(num) + return template.HTML(strconv.Itoa(num)) case num <= 50: - return `` + strconv.Itoa(num) + `` + return template.HTML(`` + strconv.Itoa(num) + ``) case num < 2000: - return `` + strconv.Itoa(num) + `` + return template.HTML(`` + strconv.Itoa(num) + ``) case num < 5000: - return `` + strconv.Itoa(num) + `` + return template.HTML(`` + strconv.Itoa(num) + ``) case num < 10000: - return `` + strconv.Itoa(num) + `` + return template.HTML(`` + strconv.Itoa(num) + ``) case num < 30000: - return `` + strconv.Itoa(num) + `` + return template.HTML(`` + strconv.Itoa(num) + ``) case num < 60000: - return `` + strconv.Itoa(num) + `` + return template.HTML(`` + strconv.Itoa(num) + ``) case num < 100000: - return `` + strconv.Itoa(num) + `` + return template.HTML(`` + strconv.Itoa(num) + ``) default: - return `` + strconv.Itoa(num) + `` + return template.HTML(`` + strconv.Itoa(num) + ``) } }