wp-go/internal/plugins/pagination.go

70 lines
1.6 KiB
Go
Raw Permalink Normal View History

2023-01-18 15:04:12 +00:00
package plugins
2023-01-19 14:02:45 +00:00
import (
"fmt"
2023-01-21 11:31:23 +00:00
str "github.com/fthvgb1/wp-go/helper/strings"
2023-01-19 14:02:45 +00:00
"regexp"
"strings"
)
2023-01-18 15:04:12 +00:00
type PageEle struct {
PrevEle string
NextEle string
DotsEle string
MiddleEle string
CurrentEle string
}
func TwentyFifteenPagination() PageEle {
return twentyFifteen
}
var twentyFifteen = PageEle{
PrevEle: `<a class="prev page-numbers" href="%s">上一页</a>`,
NextEle: `<a class="next page-numbers" href="%s">下一页</a>`,
DotsEle: `<span class="page-numbers dots">…</span>`,
MiddleEle: `<a class="page-numbers" href="%s"><span class="meta-nav screen-reader-text"> </span>%d</a>
`,
CurrentEle: `<span aria-current="page" class="page-numbers current">
<span class="meta-nav screen-reader-text"> </span>%d</span>`,
}
2023-02-14 15:44:48 +00:00
func (p PageEle) Current(page, totalPage, totalRow int) string {
2023-01-18 15:04:12 +00:00
return fmt.Sprintf(p.CurrentEle, page)
}
func (p PageEle) Prev(url string) string {
return fmt.Sprintf(p.PrevEle, url)
}
func (p PageEle) Next(url string) string {
return fmt.Sprintf(p.NextEle, url)
}
func (p PageEle) Dots() string {
return p.DotsEle
}
func (p PageEle) Middle(page int, url string) string {
return fmt.Sprintf(p.MiddleEle, url, page)
}
2023-01-19 14:02:45 +00:00
var reg = regexp.MustCompile(`(/page)/(\d+)`)
func (p PageEle) Url(path, query string, page int) string {
if !strings.Contains(path, "/page/") {
path = fmt.Sprintf("%s%s", path, "/page/1")
}
if page == 1 {
path = reg.ReplaceAllString(path, "")
} else {
s := fmt.Sprintf("$1/%d", page)
path = reg.ReplaceAllString(path, s)
}
path = strings.Replace(path, "//", "/", -1)
if path == "" {
path = "/"
}
2023-01-21 11:31:23 +00:00
return str.Join(path, query)
2023-01-19 14:02:45 +00:00
}