优化代码

This commit is contained in:
xing 2023-03-01 13:17:12 +08:00
parent 4d50f60c62
commit 9af2a04a97
22 changed files with 82 additions and 74 deletions

View File

@ -2,16 +2,16 @@ package actions
import ( import (
"github.com/fthvgb1/wp-go/internal/theme" "github.com/fthvgb1/wp-go/internal/theme"
"github.com/fthvgb1/wp-go/internal/theme/common" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func ThemeHook(scene int) func(*gin.Context) { func ThemeHook(scene int) func(*gin.Context) {
return func(ctx *gin.Context) { return func(ctx *gin.Context) {
t := theme.GetTemplateName() t := theme.GetTemplateName()
h := common.NewHandle(ctx, scene, t) h := wp.NewHandle(ctx, scene, t)
h.Index = common.NewIndexHandle(h) h.Index = wp.NewIndexHandle(h)
h.Detail = common.NewDetailHandle(h) h.Detail = wp.NewDetailHandle(h)
theme.Hook(t, h) theme.Hook(t, h)
} }
} }

View File

@ -33,7 +33,7 @@ func commonTemplate(t *multipTemplate.MultipleFsTemplate) {
for _, main := range m { for _, main := range m {
file := filepath.Base(main) file := filepath.Base(main)
dir := strings.Split(main, "/")[0] dir := strings.Split(main, "/")[0]
templ := template.Must(template.New(file).Funcs(t.FuncMap).ParseFS(t.Fs, main, filepath.Join(dir, "layout/*.gohtml"), "common/template.gohtml")) templ := template.Must(template.New(file).Funcs(t.FuncMap).ParseFS(t.Fs, main, filepath.Join(dir, "layout/*.gohtml"), "wp/template.gohtml"))
t.SetTemplate(main, templ) t.SetTemplate(main, templ)
} }
} }

View File

@ -1,20 +1,20 @@
package theme package theme
import ( import (
"github.com/fthvgb1/wp-go/internal/theme/common"
"github.com/fthvgb1/wp-go/internal/theme/twentyfifteen" "github.com/fthvgb1/wp-go/internal/theme/twentyfifteen"
"github.com/fthvgb1/wp-go/internal/theme/wp"
) )
var themeMap = map[string]func(*common.Handle){} var themeMap = map[string]func(*wp.Handle){}
func addThemeHookFunc(name string, fn func(handle *common.Handle)) { func addThemeHookFunc(name string, fn func(handle *wp.Handle)) {
if _, ok := themeMap[name]; ok { if _, ok := themeMap[name]; ok {
panic("exists same name theme") panic("exists same name theme")
} }
themeMap[name] = fn themeMap[name] = fn
} }
func Hook(themeName string, handle *common.Handle) { func Hook(themeName string, handle *wp.Handle) {
fn, ok := themeMap[themeName] fn, ok := themeMap[themeName]
if ok && fn != nil { if ok && fn != nil {
fn(handle) fn(handle)

View File

@ -3,12 +3,12 @@ package twentyfifteen
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/theme/common" "github.com/fthvgb1/wp-go/internal/theme/wp"
"strconv" "strconv"
"strings" "strings"
) )
func colorSchemeCss(h *common.Handle) string { func colorSchemeCss(h *wp.Handle) string {
s := slice.Filter([]string{calColorSchemeCss(h), calSidebarTextColorCss(h), calHeaderBackgroundColorCss(h)}, func(s string) bool { s := slice.Filter([]string{calColorSchemeCss(h), calSidebarTextColorCss(h), calHeaderBackgroundColorCss(h)}, func(s string) bool {
return s != "" return s != ""
}) })
@ -17,7 +17,7 @@ func colorSchemeCss(h *common.Handle) string {
} }
return fmt.Sprintf(`<style id='%s-inline-css'%s>\n%s\n</style>`, "twentyfifteen-style", "", strings.Join(s, "\n")) return fmt.Sprintf(`<style id='%s-inline-css'%s>\n%s\n</style>`, "twentyfifteen-style", "", strings.Join(s, "\n"))
} }
func calColorSchemeCss(h *common.Handle) (r string) { func calColorSchemeCss(h *wp.Handle) (r string) {
color := getColorScheme(h) color := getColorScheme(h)
if "default" == h.CommonThemeMods().ColorScheme || len(color) < 1 { if "default" == h.CommonThemeMods().ColorScheme || len(color) < 1 {
return return
@ -46,7 +46,7 @@ func calColorSchemeCss(h *common.Handle) (r string) {
return return
} }
func calSidebarTextColorCss(h *common.Handle) (r string) { func calSidebarTextColorCss(h *wp.Handle) (r string) {
colors := getColorScheme(h) colors := getColorScheme(h)
themeMods := h.CommonThemeMods() themeMods := h.CommonThemeMods()
if themeMods.SidebarTextcolor == "" || themeMods.SidebarTextcolor == colors[4] { if themeMods.SidebarTextcolor == "" || themeMods.SidebarTextcolor == colors[4] {
@ -61,7 +61,7 @@ func calSidebarTextColorCss(h *common.Handle) (r string) {
return return
} }
func calHeaderBackgroundColorCss(h *common.Handle) (r string) { func calHeaderBackgroundColorCss(h *wp.Handle) (r string) {
colors := getColorScheme(h) colors := getColorScheme(h)
themeMods := h.CommonThemeMods() themeMods := h.CommonThemeMods()
if themeMods.HeaderBackgroundColor == "" || themeMods.HeaderBackgroundColor == colors[1] { if themeMods.HeaderBackgroundColor == "" || themeMods.HeaderBackgroundColor == colors[1] {
@ -71,7 +71,7 @@ func calHeaderBackgroundColorCss(h *common.Handle) (r string) {
return return
} }
func getColorScheme(h *common.Handle) (r []string) { func getColorScheme(h *wp.Handle) (r []string) {
x, ok := colorscheme[h.CommonThemeMods().ColorScheme] x, ok := colorscheme[h.CommonThemeMods().ColorScheme]
if ok { if ok {
r = x.Colors r = x.Colors

View File

@ -5,7 +5,7 @@ import (
"github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/theme/common" "github.com/fthvgb1/wp-go/internal/theme/wp"
) )
var postx = map[string]string{ var postx = map[string]string{
@ -30,7 +30,7 @@ var repeat = map[string]string{
"no-repeat": "no-repeat", "no-repeat": "no-repeat",
} }
func CalCustomBackGround(h *common.Handle) (r string) { func CalCustomBackGround(h *wp.Handle) (r string) {
themeMods := h.CommonThemeMods() themeMods := h.CommonThemeMods()
if themeMods.BackgroundImage == "" && themeMods.BackgroundColor == themesupport.CustomBackground.DefaultColor { if themeMods.BackgroundImage == "" && themeMods.BackgroundColor == themesupport.CustomBackground.DefaultColor {
return return

View File

@ -4,7 +4,7 @@ import (
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/theme/common" "github.com/fthvgb1/wp-go/internal/theme/wp"
) )
var style = `<style type="text/css" id="twentyfifteen-header-css">` var style = `<style type="text/css" id="twentyfifteen-header-css">`
@ -83,7 +83,7 @@ var imgStyle = `.site-header {
var header = reload.Vars(constraints.Defaults) var header = reload.Vars(constraints.Defaults)
func calCustomHeader(h *common.Handle) (r string, rand bool) { func calCustomHeader(h *wp.Handle) (r string, rand bool) {
img, rand := h.GetCustomHeader() img, rand := h.GetCustomHeader()
if img.Path == "" && h.DisplayHeaderText() { if img.Path == "" && h.DisplayHeaderText() {
return return
@ -108,7 +108,7 @@ func calCustomHeader(h *common.Handle) (r string, rand bool) {
return return
} }
func customHeader(h *common.Handle) { func customHeader(h *wp.Handle) {
headers := header.Load() headers := header.Load()
if headers == constraints.Defaults { if headers == constraints.Defaults {
headerss, rand := calCustomHeader(h) headerss, rand := calCustomHeader(h)

View File

@ -5,7 +5,7 @@ import (
"encoding/json" "encoding/json"
"github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/theme/common" "github.com/fthvgb1/wp-go/internal/theme/wp"
) )
const ThemeName = "twentyfifteen" const ThemeName = "twentyfifteen"
@ -28,20 +28,20 @@ func Init(fs embed.FS) {
logs.ErrPrintln(err, "解析colorscheme失败") logs.ErrPrintln(err, "解析colorscheme失败")
} }
var pipe = common.HandlePipe(common.Render, dispatch) var pipe = wp.HandlePipe(wp.Render, dispatch)
func Hook(h *common.Handle) { func Hook(h *wp.Handle) {
pipe(h) pipe(h)
} }
func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) { func dispatch(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
h.WidgetAreaData() h.WidgetAreaData()
h.GetPassword() h.GetPassword()
h.PushHeadScript( h.PushHeadScript(
common.NewComponents(CalCustomBackGround, 10), wp.NewComponents(CalCustomBackGround, 10),
common.NewComponents(colorSchemeCss, 10), wp.NewComponents(colorSchemeCss, 10),
) )
h.PushHandleFn(constraints.AllStats, common.NewHandleFn(customHeader, 10)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10))
switch h.Scene() { switch h.Scene() {
case constraints.Detail: case constraints.Detail:
detail(next, h.Detail) detail(next, h.Detail)
@ -50,10 +50,10 @@ func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
} }
} }
func index(next common.HandleFn[*common.Handle], i *common.IndexHandle) { func index(next wp.HandleFn[*wp.Handle], i *wp.IndexHandle) {
i.Indexs() i.Indexs()
} }
func detail(fn common.HandleFn[*common.Handle], d *common.DetailHandle) { func detail(fn wp.HandleFn[*wp.Handle], d *wp.DetailHandle) {
d.Details() d.Details()
} }

View File

@ -4,12 +4,12 @@ import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/number"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/theme/common" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"strings" "strings"
) )
func colorScheme(h *common.Handle) (r string) { func colorScheme(h *wp.Handle) (r string) {
if "custom" != wpconfig.GetThemeModsVal(ThemeName, "colorscheme", "light") { if "custom" != wpconfig.GetThemeModsVal(ThemeName, "colorscheme", "light") {
return return
} }

View File

@ -2,10 +2,10 @@ package twentyseventeen
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/internal/theme/common" "github.com/fthvgb1/wp-go/internal/theme/wp"
) )
func customHeader(h *common.Handle) (r string) { func customHeader(h *wp.Handle) (r string) {
themeMods := h.CommonThemeMods() themeMods := h.CommonThemeMods()
headerTextColor := themeMods.HeaderTextcolor headerTextColor := themeMods.HeaderTextcolor
if headerTextColor == "" || headerTextColor == themeMods.ThemeSupport.CustomHeader.DefaultTextColor { if headerTextColor == "" || headerTextColor == themeMods.ThemeSupport.CustomHeader.DefaultTextColor {

View File

@ -9,7 +9,7 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/theme/common" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"strings" "strings"
@ -27,26 +27,26 @@ var paginate = func() plugins.PageEle {
return p return p
}() }()
var pipe = common.HandlePipe(common.Render, ready, dispatch) var pipe = wp.HandlePipe(wp.Render, ready, dispatch)
func Hook(h *common.Handle) { func Hook(h *wp.Handle) {
pipe(h) pipe(h)
} }
func ready(next common.HandleFn[*common.Handle], h *common.Handle) { func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
h.WidgetAreaData() h.WidgetAreaData()
h.GetPassword() h.GetPassword()
h.PushHandleFn(constraints.AllStats, common.NewHandleFn(calClass, 15)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 15))
h.PushHeadScript( h.PushHeadScript(
common.NewComponents(colorScheme, 10), wp.NewComponents(colorScheme, 10),
common.NewComponents(customHeader, 10), wp.NewComponents(customHeader, 10),
) )
h.SetData("HeaderImage", getHeaderImage(h)) h.SetData("HeaderImage", getHeaderImage(h))
h.SetData("scene", h.Scene()) h.SetData("scene", h.Scene())
next(h) next(h)
} }
func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) { func dispatch(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
switch h.Scene() { switch h.Scene() {
case constraints.Detail: case constraints.Detail:
detail(next, h.Detail) detail(next, h.Detail)
@ -55,14 +55,14 @@ func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
} }
} }
var listPostsPlugins = func() map[string]common.Plugin[models.Posts, *common.Handle] { var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] {
return maps.Merge(common.ListPostPlugins(), map[string]common.Plugin[models.Posts, *common.Handle]{ return maps.Merge(wp.ListPostPlugins(), map[string]wp.Plugin[models.Posts, *wp.Handle]{
"twentyseventeen_postThumbnail": postThumbnail, "twentyseventeen_postThumbnail": postThumbnail,
}) })
}() }()
func index(next common.HandleFn[*common.Handle], i *common.IndexHandle) { func index(next wp.HandleFn[*wp.Handle], i *wp.IndexHandle) {
err := i.BuildIndexData(common.NewIndexParams(i.C)) err := i.BuildIndexData(wp.NewIndexParams(i.C))
if err != nil { if err != nil {
i.SetTempl(str.Join(ThemeName, "/posts/error.gohtml")) i.SetTempl(str.Join(ThemeName, "/posts/error.gohtml"))
i.Render() i.Render()
@ -73,7 +73,7 @@ func index(next common.HandleFn[*common.Handle], i *common.IndexHandle) {
next(i.Handle) next(i.Handle)
} }
func detail(next common.HandleFn[*common.Handle], d *common.DetailHandle) { func detail(next wp.HandleFn[*wp.Handle], d *wp.DetailHandle) {
err := d.BuildDetailData() err := d.BuildDetailData()
if err != nil { if err != nil {
d.SetTempl(str.Join(ThemeName, "/posts/error.gohtml")) d.SetTempl(str.Join(ThemeName, "/posts/error.gohtml"))
@ -112,7 +112,7 @@ func (c comment) FormatLi(ctx *gin.Context, m models.Comments, depth int, isTls
return plugins.FormatLi(templ, ctx, m, depth, isTls, eo, parent) return plugins.FormatLi(templ, ctx, m, depth, isTls, eo, parent)
} }
func postThumbnail(next common.Fn[models.Posts], h *common.Handle, t models.Posts) models.Posts { func postThumbnail(next wp.Fn[models.Posts], h *wp.Handle, t models.Posts) models.Posts {
if t.Thumbnail.Path != "" { if t.Thumbnail.Path != "" {
t.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" t.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px"
if h.Scene() == constraints.Detail { if h.Scene() == constraints.Detail {
@ -124,7 +124,7 @@ func postThumbnail(next common.Fn[models.Posts], h *common.Handle, t models.Post
var header = reload.Vars(models.PostThumbnail{}) var header = reload.Vars(models.PostThumbnail{})
func getHeaderImage(h *common.Handle) (r models.PostThumbnail) { func getHeaderImage(h *wp.Handle) (r models.PostThumbnail) {
img := header.Load() img := header.Load()
if img.Path != "" { if img.Path != "" {
return r return r
@ -146,7 +146,7 @@ func getHeaderImage(h *common.Handle) (r models.PostThumbnail) {
return return
} }
func calClass(h *common.Handle) { func calClass(h *wp.Handle) {
themeMods := h.CommonThemeMods() themeMods := h.CommonThemeMods()
u := wpconfig.GetThemeModsVal(ThemeName, "header_image", themeMods.ThemeSupport.CustomHeader.DefaultImage) u := wpconfig.GetThemeModsVal(ThemeName, "header_image", themeMods.ThemeSupport.CustomHeader.DefaultImage)
var class []string var class []string

View File

@ -1,4 +1,4 @@
package common package wp
import ( import (
"github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/number"

View File

@ -1,4 +1,4 @@
package common package wp
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package common package wp
import ( import (
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"

View File

@ -1,4 +1,4 @@
package common package wp
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package common package wp
import ( import (
"errors" "errors"

View File

@ -1,4 +1,4 @@
package common package wp
import ( import (
"database/sql" "database/sql"

View File

@ -1,4 +1,4 @@
package common package wp
import ( import (
"errors" "errors"

View File

@ -1,4 +1,4 @@
package common package wp
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package common package wp
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package common package wp
import ( import (
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"

View File

@ -1,4 +1,4 @@
package common package wp
import ( import (
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
@ -31,6 +31,11 @@ type Handle struct {
themeMods wpconfig.ThemeMods themeMods wpconfig.ThemeMods
handleFns map[int][]HandleCall handleFns map[int][]HandleCall
err error err error
abort bool
}
func (h *Handle) Abort() {
h.abort = true
} }
func (h *Handle) CommonThemeMods() wpconfig.ThemeMods { func (h *Handle) CommonThemeMods() wpconfig.ThemeMods {
@ -132,21 +137,23 @@ func (h *Handle) GetPassword() {
func (h *Handle) ExecHandleFns() { func (h *Handle) ExecHandleFns() {
calls, ok := h.handleFns[h.Stats] calls, ok := h.handleFns[h.Stats]
var fns []HandleCall
if ok { if ok {
slice.SortSelf(calls, func(i, j HandleCall) bool { fns = append(fns, calls...)
return i.Order > j.Order }
}) call, ok := h.handleFns[constraints.AllStats]
for _, call := range calls { if ok {
call.Fn(h) fns = append(fns, call...)
}
slice.SortSelf(fns, func(i, j HandleCall) bool {
return i.Order > j.Order
})
for _, fn := range fns {
fn.Fn(h)
if h.abort {
break
} }
} }
fns, ok := h.handleFns[constraints.AllStats]
if ok {
for _, fn := range fns {
fn.Fn(h)
}
}
} }
func (h *Handle) PreTemplate() { func (h *Handle) PreTemplate() {
@ -181,11 +188,12 @@ func (h *Handle) Render() {
h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) { h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) {
h.CalMultipleComponents() h.CalMultipleComponents()
h.CalBodyClass() h.CalBodyClass()
}, 5)) }, 5), NewHandleFn(func(h *Handle) {
h.C.HTML(h.Code, h.templ, h.ginH)
}, 0))
h.ExecHandleFns() h.ExecHandleFns()
h.C.HTML(h.Code, h.templ, h.ginH)
} }
func (h *Handle) PushComponents(name string, components ...Components) { func (h *Handle) PushComponents(name string, components ...Components) {