优化代码

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 (
"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"
)
func ThemeHook(scene int) func(*gin.Context) {
return func(ctx *gin.Context) {
t := theme.GetTemplateName()
h := common.NewHandle(ctx, scene, t)
h.Index = common.NewIndexHandle(h)
h.Detail = common.NewDetailHandle(h)
h := wp.NewHandle(ctx, scene, t)
h.Index = wp.NewIndexHandle(h)
h.Detail = wp.NewDetailHandle(h)
theme.Hook(t, h)
}
}

View File

@ -33,7 +33,7 @@ func commonTemplate(t *multipTemplate.MultipleFsTemplate) {
for _, main := range m {
file := filepath.Base(main)
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)
}
}

View File

@ -1,20 +1,20 @@
package theme
import (
"github.com/fthvgb1/wp-go/internal/theme/common"
"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 {
panic("exists same name theme")
}
themeMap[name] = fn
}
func Hook(themeName string, handle *common.Handle) {
func Hook(themeName string, handle *wp.Handle) {
fn, ok := themeMap[themeName]
if ok && fn != nil {
fn(handle)

View File

@ -3,12 +3,12 @@ package twentyfifteen
import (
"fmt"
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/theme/common"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"strconv"
"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 {
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"))
}
func calColorSchemeCss(h *common.Handle) (r string) {
func calColorSchemeCss(h *wp.Handle) (r string) {
color := getColorScheme(h)
if "default" == h.CommonThemeMods().ColorScheme || len(color) < 1 {
return
@ -46,7 +46,7 @@ func calColorSchemeCss(h *common.Handle) (r string) {
return
}
func calSidebarTextColorCss(h *common.Handle) (r string) {
func calSidebarTextColorCss(h *wp.Handle) (r string) {
colors := getColorScheme(h)
themeMods := h.CommonThemeMods()
if themeMods.SidebarTextcolor == "" || themeMods.SidebarTextcolor == colors[4] {
@ -61,7 +61,7 @@ func calSidebarTextColorCss(h *common.Handle) (r string) {
return
}
func calHeaderBackgroundColorCss(h *common.Handle) (r string) {
func calHeaderBackgroundColorCss(h *wp.Handle) (r string) {
colors := getColorScheme(h)
themeMods := h.CommonThemeMods()
if themeMods.HeaderBackgroundColor == "" || themeMods.HeaderBackgroundColor == colors[1] {
@ -71,7 +71,7 @@ func calHeaderBackgroundColorCss(h *common.Handle) (r string) {
return
}
func getColorScheme(h *common.Handle) (r []string) {
func getColorScheme(h *wp.Handle) (r []string) {
x, ok := colorscheme[h.CommonThemeMods().ColorScheme]
if ok {
r = x.Colors

View File

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

View File

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

View File

@ -5,7 +5,7 @@ import (
"encoding/json"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"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"
@ -28,20 +28,20 @@ func Init(fs embed.FS) {
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)
}
func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
func dispatch(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
h.WidgetAreaData()
h.GetPassword()
h.PushHeadScript(
common.NewComponents(CalCustomBackGround, 10),
common.NewComponents(colorSchemeCss, 10),
wp.NewComponents(CalCustomBackGround, 10),
wp.NewComponents(colorSchemeCss, 10),
)
h.PushHandleFn(constraints.AllStats, common.NewHandleFn(customHeader, 10))
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10))
switch h.Scene() {
case constraints.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()
}
func detail(fn common.HandleFn[*common.Handle], d *common.DetailHandle) {
func detail(fn wp.HandleFn[*wp.Handle], d *wp.DetailHandle) {
d.Details()
}

View File

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

View File

@ -2,10 +2,10 @@ package twentyseventeen
import (
"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()
headerTextColor := themeMods.HeaderTextcolor
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/models"
"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/gin-gonic/gin"
"strings"
@ -27,26 +27,26 @@ var paginate = func() plugins.PageEle {
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)
}
func ready(next common.HandleFn[*common.Handle], h *common.Handle) {
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
h.WidgetAreaData()
h.GetPassword()
h.PushHandleFn(constraints.AllStats, common.NewHandleFn(calClass, 15))
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 15))
h.PushHeadScript(
common.NewComponents(colorScheme, 10),
common.NewComponents(customHeader, 10),
wp.NewComponents(colorScheme, 10),
wp.NewComponents(customHeader, 10),
)
h.SetData("HeaderImage", getHeaderImage(h))
h.SetData("scene", h.Scene())
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() {
case constraints.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] {
return maps.Merge(common.ListPostPlugins(), map[string]common.Plugin[models.Posts, *common.Handle]{
var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] {
return maps.Merge(wp.ListPostPlugins(), map[string]wp.Plugin[models.Posts, *wp.Handle]{
"twentyseventeen_postThumbnail": postThumbnail,
})
}()
func index(next common.HandleFn[*common.Handle], i *common.IndexHandle) {
err := i.BuildIndexData(common.NewIndexParams(i.C))
func index(next wp.HandleFn[*wp.Handle], i *wp.IndexHandle) {
err := i.BuildIndexData(wp.NewIndexParams(i.C))
if err != nil {
i.SetTempl(str.Join(ThemeName, "/posts/error.gohtml"))
i.Render()
@ -73,7 +73,7 @@ func index(next common.HandleFn[*common.Handle], i *common.IndexHandle) {
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()
if err != nil {
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)
}
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 != "" {
t.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px"
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{})
func getHeaderImage(h *common.Handle) (r models.PostThumbnail) {
func getHeaderImage(h *wp.Handle) (r models.PostThumbnail) {
img := header.Load()
if img.Path != "" {
return r
@ -146,7 +146,7 @@ func getHeaderImage(h *common.Handle) (r models.PostThumbnail) {
return
}
func calClass(h *common.Handle) {
func calClass(h *wp.Handle) {
themeMods := h.CommonThemeMods()
u := wpconfig.GetThemeModsVal(ThemeName, "header_image", themeMods.ThemeSupport.CustomHeader.DefaultImage)
var class []string

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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