Compare commits

...

2 Commits

Author SHA1 Message Date
xing
1d80534333 custom-header fifteen页眉图片设置 2023-02-16 14:36:43 +08:00
xing
f722d685ea 优化 2023-02-16 11:53:24 +08:00
14 changed files with 200 additions and 46 deletions

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/fthvgb1/wp-go
go 1.20
go 1.19
require (
github.com/dlclark/regexp2 v1.7.0

View File

@ -2,7 +2,6 @@ package strings
import (
"crypto/md5"
"errors"
"fmt"
"golang.org/x/exp/constraints"
"io"
@ -61,16 +60,14 @@ func NewBuilder() *Builder {
return &Builder{&strings.Builder{}}
}
func (b *Builder) WriteString(s ...string) (count int, err error) {
func (b *Builder) WriteString(s ...string) (count int) {
for _, ss := range s {
i, er := b.Builder.WriteString(ss)
if er != nil {
err = errors.Join(er)
}
i, _ := b.Builder.WriteString(ss)
count += i
}
return
}
func (b *Builder) Sprintf(format string, a ...any) (int, error) {
return b.WriteString(fmt.Sprintf(format, a...))
func (b *Builder) Sprintf(format string, a ...any) int {
i, _ := fmt.Fprintf(b, format, a...)
return i
}

View File

@ -86,11 +86,8 @@ func TestBuilder_WriteString(t *testing.T) {
b := &Builder{
Builder: tt.fields.Builder,
}
gotCount, err := b.WriteString(tt.args.s...)
if (err != nil) != tt.wantErr {
t.Errorf("WriteString() error = %v, wantErr %v", err, tt.wantErr)
return
}
gotCount := b.WriteString(tt.args.s...)
if gotCount != tt.wantCount {
t.Errorf("WriteString() gotCount = %v, want %v", gotCount, tt.wantCount)
}

View File

@ -11,7 +11,6 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/theme"
"github.com/fthvgb1/wp-go/internal/theme/common"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/model"
"log"
@ -107,7 +106,7 @@ func reload() {
logs.ErrPrintln(err, "获取网站设置WpOption失败")
err = wpconfig.InitTerms()
wpconfig.FlushModes()
common.Reload()
theme.Reload()
logs.ErrPrintln(err, "获取WpTerms表失败")
if middleWareReloadFn != nil {
middleWareReloadFn()

View File

@ -3,6 +3,7 @@ package middleware
import (
"bytes"
"fmt"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/mail"
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
@ -64,17 +65,13 @@ var (
)
func formatStack(s string) (r string) {
ss := strings.Builder{}
ss := str.NewBuilder()
t := strings.Split(s, "\n")
for i, line := range t {
if i%2 == 0 {
ss.WriteString("<dt>")
ss.WriteString(line)
ss.WriteString("</dt>")
ss.WriteString("<dt>", line, "</dt>")
} else {
ss.WriteString("<dd>")
ss.WriteString(strings.Trim(line, "\t"))
ss.WriteString("</dd>")
ss.WriteString("<dt>", strings.Trim(line, "\t"), "</dt>")
}
}
r = ss.String()

View File

@ -2,6 +2,7 @@ package plugins
import (
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/gin-gonic/gin"
"net/url"
@ -50,7 +51,7 @@ func FormatComments(c *gin.Context, i CommentHtml, comments []models.Comments, m
}
func (d CommentHandler) formatComment(comments []*Comments, isTop bool) (html string) {
s := strings.Builder{}
s := str.NewBuilder()
if d.depth > d.maxDepth {
comments = d.findComments(comments)
}
@ -69,9 +70,7 @@ func (d CommentHandler) formatComment(comments []*Comments, isTop bool) (html st
s.WriteString(d.i.FormatLi(d.Context, comment.Comments, d.depth, d.isTls, eo, parent))
if fl {
d.depth++
s.WriteString(`<ol class="children">`)
s.WriteString(d.formatComment(comment.Children, false))
s.WriteString(`</ol>`)
s.WriteString(`<ol class="children">`, d.formatComment(comment.Children, false), `</ol>`)
if isTop {
d.depth = 1
}

View File

@ -4,9 +4,9 @@ import (
"fmt"
"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/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings"
)
var postx = map[string]string{
@ -50,11 +50,9 @@ func (h *Handle) CalCustomBackGround() (r string) {
if mods.BackgroundImage == "" && mods.BackgroundColor == mods.ThemeSupport.CustomBackground.DefaultColor {
return
}
s := strings.Builder{}
s := str.NewBuilder()
if mods.BackgroundImage != "" {
s.WriteString(` background-image: url("`)
s.WriteString(helper.CutUrlHost(mods.BackgroundImage))
s.WriteString(`");`)
s.Sprintf(` background-image: url("%s");`, helper.CutUrlHost(mods.BackgroundImage))
}
backgroundPositionX := helper.Defaults(mods.BackgroundPositionX, mods.ThemeSupport.CustomBackground.DefaultPositionX)
backgroundPositionX = maps.WithDefaultVal(postx, backgroundPositionX, "left")
@ -74,10 +72,7 @@ func (h *Handle) CalCustomBackGround() (r string) {
attachment := helper.Defaults(mods.BackgroundAttachment, mods.ThemeSupport.CustomBackground.DefaultAttachment)
attachment = helper.Or(attachment == "fixed", "fixed", "scroll")
attachment = fmt.Sprintf(" background-attachment: %s;", attachment)
s.WriteString(positon)
s.WriteString(siz)
s.WriteString(repeats)
s.WriteString(attachment)
s.WriteString(positon, siz, repeats, attachment)
r = fmt.Sprintf(`<style id="custom-background-css">
body.custom-background {%s}
</style>`, s.String())

View File

@ -0,0 +1,33 @@
package common
import (
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/wpconfig"
)
func (h *Handle) DisplayHeaderText() bool {
mods, err := wpconfig.GetThemeMods(h.Theme)
if err != nil {
return false
}
return mods.ThemeSupport.CustomHeader.HeaderText && "blank" != mods.HeaderTextcolor
}
func (h *Handle) GetCustomHeader() (r models.PostThumbnail, isRand bool) {
hs, err := cache.GetHeaderImages(h.C, h.Theme)
if err != nil {
logs.ErrPrintln(err, "获取页眉背景图失败")
return
}
if len(hs) < 1 {
return
}
if len(hs) > 1 {
isRand = true
}
r, _ = slice.RandPop(&hs)
return
}

View File

@ -1,5 +1,5 @@
package common
func Reload() {
backgroud.Flush()
backgroud.Store("default")
}

View File

@ -14,6 +14,11 @@ func InitTheme() {
common.AddThemeSupport(twentyfifteen.ThemeName, twentyfifteen.ThemeSupport())
}
func Reload() {
twentyfifteen.Reload()
common.Reload()
}
func GetTemplateName() string {
tmlp := config.GetConfig().Theme
if tmlp == "" {

View File

@ -0,0 +1,124 @@
package twentyfifteen
import (
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/safety"
)
var style = `<style type="text/css" id="twentyfifteen-header-css">`
var defaultTextStyle = `.site-header {
padding-top: 14px;
padding-bottom: 14px;
}
.site-branding {
min-height: 42px;
}
@media screen and (min-width: 46.25em) {
.site-header {
padding-top: 21px;
padding-bottom: 21px;
}
.site-branding {
min-height: 56px;
}
}
@media screen and (min-width: 55em) {
.site-header {
padding-top: 25px;
padding-bottom: 25px;
}
.site-branding {
min-height: 62px;
}
}
@media screen and (min-width: 59.6875em) {
.site-header {
padding-top: 0;
padding-bottom: 0;
}
.site-branding {
min-height: 0;
}
}`
var imgStyle = `.site-header {
/*
* No shorthand so the Customizer can override individual properties.
* @see https://core.trac.wordpress.org/ticket/31460
*/
background-image: url("%s");
background-repeat: no-repeat;
background-position: 50% 50%;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
@media screen and (min-width: 59.6875em) {
body:before {
/*
* No shorthand so the Customizer can override individual properties.
* @see https://core.trac.wordpress.org/ticket/31460
*/
background-image: url("%s");
background-repeat: no-repeat;
background-position: 100% 50%;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
border-right: 0;
}
.site-header {
background: transparent;
}
}`
var header = safety.NewVar("default")
func Reload() {
header.Store("default")
}
func (h *handle) CalCustomHeader() (r string, rand bool) {
img, rand := h.IndexHandle.GetCustomHeader()
if img.Path == "" && h.IndexHandle.DisplayHeaderText() {
return
}
ss := str.NewBuilder()
ss.WriteString(style)
if img.Path == "" && !h.IndexHandle.DisplayHeaderText() {
ss.WriteString(defaultTextStyle)
}
if img.Path != "" {
ss.Sprintf(imgStyle, img.Path, img.Path)
}
if !h.IndexHandle.DisplayHeaderText() {
ss.WriteString(`.site-title,
.site-description {
clip: rect(1px, 1px, 1px, 1px);
position: absolute;
}`)
}
ss.WriteString("</style>")
r = ss.String()
return
}
func (h *handle) CustomHeader() {
headers := header.Load()
if headers == "default" {
headerss, rand := h.CalCustomHeader()
headers = headerss
if !rand {
header.Store(headers)
}
}
h.IndexHandle.GinH["customHeader"] = headers
return
}

View File

@ -61,4 +61,7 @@
{{if .customBackground}}
{{.customBackground|unescaped}}
{{end}}
{{if .customHeader}}
{{.customHeader|unescaped}}
{{end}}
{{end}}

View File

@ -12,12 +12,13 @@ import (
const ThemeName = "twentyfifteen"
type indexHandle struct {
type handle struct {
*common.IndexHandle
*common.DetailHandle
}
func newIndexHandle(iHandle *common.IndexHandle) *indexHandle {
return &indexHandle{iHandle}
func newHandle(iHandle *common.IndexHandle, dHandle *common.DetailHandle) *handle {
return &handle{iHandle, dHandle}
}
type detailHandle struct {
@ -31,19 +32,22 @@ func newDetailHandle(dHandle *common.DetailHandle) *detailHandle {
func Hook(h *common.Handle) {
h.WidgetAreaData()
h.GetPassword()
handle := newHandle(common.NewIndexHandle(h), common.NewDetailHandle(h))
if h.Scene == constraints.Detail {
newDetailHandle(common.NewDetailHandle(h)).Detail()
handle.Detail()
return
}
newIndexHandle(common.NewIndexHandle(h)).Index()
handle.Index()
}
func (i *indexHandle) Index() {
i.Indexs()
func (h *handle) Index() {
h.CustomHeader()
h.Indexs()
}
func (d *detailHandle) Detail() {
d.Details()
func (h *handle) Detail() {
h.CustomHeader()
h.Details()
}
func getHeaderImage(c *gin.Context) (r models.PostThumbnail) {

View File

@ -114,6 +114,7 @@ func GetThemeMods(theme string) (r ThemeMods, err error) {
return
}
r.setThemeColorScheme(theme)
r.setThemeSupport(theme)
themeModes.Store(theme, r)
return
}