Skip to content

Commit

Permalink
Add option to link to original post in ALLPOST
Browse files Browse the repository at this point in the history
  • Loading branch information
robertabcd committed Jan 21, 2021
1 parent 7b58b33 commit b58c37f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type PttwebConfig struct {

EnableOver18Cookie bool

// EnableLinkOriginalInAllPost indicates whether to parse the board name in
// ALLPOST board and link to original posts.
EnableLinkOriginalInAllPost bool

FeedPrefix string
AtomFeedTitleTemplate string

Expand Down
21 changes: 17 additions & 4 deletions pttbbs/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ var (
const (
ArticleAuthor = "作者"
ArticleTitle = "標題"

AllPostBrdName = "ALLPOST"
)

var (
validBrdNameRegexp = regexp.MustCompile(`^[0-9a-zA-Z][0-9a-zA-Z_\.\-]+$`)
validFileNameRegexp = regexp.MustCompile(`^[MG]\.\d+\.A(\.[0-9A-F]+)?$`)
validUserIDRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z]{1,11}$`)
fileNameTimeRegexp = regexp.MustCompile(`^[MG]\.(\d+)\.A(\.[0-9A-F]+)?$`)
validBrdNameRegexp = regexp.MustCompile(`^[0-9a-zA-Z][0-9a-zA-Z_\.\-]+$`)
validFileNameRegexp = regexp.MustCompile(`^[MG]\.\d+\.A(\.[0-9A-F]+)?$`)
validUserIDRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z]{1,11}$`)
fileNameTimeRegexp = regexp.MustCompile(`^[MG]\.(\d+)\.A(\.[0-9A-F]+)?$`)
allPostBrdNameTitleRegexp = regexp.MustCompile(`\(([0-9a-zA-Z][0-9a-zA-Z_\.\-]+)\)$`)
)

func IsValidBrdName(brdname string) bool {
Expand Down Expand Up @@ -109,3 +112,13 @@ func countPrefixSpaces(s string) int {
}
return 0
}

// BrdNameFromAllPostTitle returns the board name parsed from the post title in
// ALLPOST board.
func BrdNameFromAllPostTitle(title string) (string, bool) {
m := allPostBrdNameTitleRegexp.FindStringSubmatch(title)
if len(m) == 0 {
return "", false
}
return m[1], true
}
44 changes: 44 additions & 0 deletions pttbbs/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package pttbbs

import "testing"

func TestBrdNameFromAllPostTitle(t *testing.T) {
for _, test := range []struct {
desc string
title string
want string
}{
{
desc: "normal title with board name",
title: "測試 (Test)",
want: "Test",
},
{
desc: "empty title with board name",
title: "(SYSOP)",
want: "SYSOP",
},
{
desc: "normal title but bad board name",
title: "測試 (SYS%%) ",
},
{
desc: "bad title",
title: "(Bad) ",
},
{
desc: "empty title",
title: "",
},
} {
t.Run(test.desc, func(t *testing.T) {
wantOk := test.want != ""

got, gotOk := BrdNameFromAllPostTitle(test.title)
if got != test.want || gotOk != wantOk {
t.Errorf("BrdNameFromAllPostTitle(%q) = (%q, %t); want (%q, %t)",
test.title, got, gotOk, test.want, wantOk)
}
})
}
}
7 changes: 6 additions & 1 deletion pttweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,12 @@ func templateFuncMap() template.FuncMap {
"valid_article": func(a pttbbs.Article) bool {
return pttbbs.IsValidArticleFileName(a.FileName)
},
"route_bbsarticle": func(brdname, filename string) (*url.URL, error) {
"route_bbsarticle": func(brdname, filename, title string) (*url.URL, error) {
if config.EnableLinkOriginalInAllPost && brdname == pttbbs.AllPostBrdName {
if origBrdName, ok := pttbbs.BrdNameFromAllPostTitle(title); ok {
brdname = origBrdName
}
}
return router.Get("bbsarticle").URLPath("brdname", brdname, "filename", filename)
},
"route_askover18": func() (*url.URL, error) {
Expand Down

0 comments on commit b58c37f

Please sign in to comment.