Skip to content

Commit

Permalink
Add template functions for search shortcuts.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertabcd committed Apr 24, 2018
1 parent f84eaa6 commit b068666
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pttbbs/search_predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ func WithTitle(title string) SearchPredicate {
}}
}

func WithExactTitle(title string) SearchPredicate {
return &searchPredicate{&apipb.SearchFilter{
Type: apipb.SearchFilter_TYPE_EXACT_TITLE,
StringData: title,
}}
}

func WithAuthor(author string) SearchPredicate {
return &searchPredicate{&apipb.SearchFilter{
Type: apipb.SearchFilter_TYPE_AUTHOR,
Expand Down
30 changes: 30 additions & 0 deletions pttbbs/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"regexp"
"strconv"
"strings"
"time"
)

Expand All @@ -16,6 +17,8 @@ var (
SignaturePrefixStrings = []string{"※", "==>"}

ArticlePushPrefixStrings = []string{"推 ", "噓 ", "→ "}

subjectPrefixStrings = []string{"re:", "fw:", "[轉錄]"}
)

const (
Expand All @@ -26,6 +29,7 @@ const (
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]+)?$`)
)

Expand All @@ -37,6 +41,10 @@ func IsValidArticleFileName(filename string) bool {
return validFileNameRegexp.MatchString(filename)
}

func IsValidUserID(userID string) bool {
return validUserIDRegexp.MatchString(userID)
}

func ParseArticleFirstLine(line []byte) (tag1, val1, tag2, val2 []byte, ok bool) {
m := ArticleFirstLineRegexp.FindSubmatch(line)
if m == nil {
Expand Down Expand Up @@ -79,3 +87,25 @@ func ParseFileNameTime(filename string) (time.Time, error) {
}
return time.Unix(int64(unix), 0), nil
}

func Subject(subject string) string {
lower := strings.ToLower(subject)
off := 0
for _, p := range subjectPrefixStrings {
for strings.HasPrefix(lower[off:], p) {
off += len(p)
off += countPrefixSpaces(lower[off:])
}
off += countPrefixSpaces(lower[off:])
}
return subject[off:]
}

func countPrefixSpaces(s string) int {
for i, c := range s {
if c != ' ' {
return i
}
}
return 0
}
27 changes: 27 additions & 0 deletions pttweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,15 @@ func templateFuncMap() template.FuncMap {
"route": func(where string, attrs ...string) (*url.URL, error) {
return router.Get(where).URLPath(attrs...)
},
"route_search_author": func(b pttbbs.Board, author string) (*url.URL, error) {
if !pttbbs.IsValidUserID(author) {
return nil, nil
}
return bbsSearchURL(b, "author:"+author)
},
"route_search_thread": func(b pttbbs.Board, title string) (*url.URL, error) {
return bbsSearchURL(b, "thread:"+pttbbs.Subject(title))
},
"static_prefix": func() string {
return config.StaticPrefix
},
Expand Down Expand Up @@ -475,6 +484,17 @@ func handleBbs(c *Context, w http.ResponseWriter) error {
return page.ExecutePage(w, (*page.BbsIndex)(bbsindex))
}

func bbsSearchURL(b pttbbs.Board, query string) (*url.URL, error) {
u, err := router.Get("bbssearch").URLPath("brdname", b.BrdName)
if err != nil {
return nil, err
}
q := url.Values{}
q.Set("q", query)
u.RawQuery = q.Encode()
return u, nil
}

func parseKeyValueTerm(term string) (pttbbs.SearchPredicate, bool) {
kv := strings.SplitN(term, ":", 2)
if len(kv) != 2 {
Expand All @@ -495,6 +515,13 @@ func parseKeyValueTerm(term string) (pttbbs.SearchPredicate, bool) {
}

func parseQuery(query string) ([]pttbbs.SearchPredicate, error) {
// Special case, thread takes up all the query.
if strings.HasPrefix(query, "thread:") {
return []pttbbs.SearchPredicate{
pttbbs.WithExactTitle(strings.TrimSpace(strings.TrimPrefix(query, "thread:"))),
}, nil
}

segs := strings.Split(query, " ")
var titleSegs []string
var preds []pttbbs.SearchPredicate
Expand Down

0 comments on commit b068666

Please sign in to comment.