优化 调整目录

This commit is contained in:
xing 2023-03-12 20:41:10 +08:00
parent b6b92ede34
commit 2d476ea4f6
15 changed files with 140 additions and 147 deletions

View File

@ -1,4 +1,4 @@
package components
package widgets
const (
SearchFormArgs = "SearchFormArgs"

View File

@ -38,7 +38,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"), "wp/template.gohtml", "wp/*/*/*.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

@ -5,10 +5,11 @@ import (
"encoding/json"
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/plugins/wphandle"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/theme/wp/components"
"strings"
)
@ -39,9 +40,9 @@ func Hook(h *wp.Handle) {
}
func dispatch(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
h.WidgetArea()
components.WidgetArea(h)
h.GetPassword()
h.PushComponentFilterFn(components.SearchFormArgs, func(h *wp.Handle, s string) string {
h.PushComponentFilterFn(widgets.SearchFormArgs, func(h *wp.Handle, s string) string {
return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`)
})
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)

View File

@ -1,21 +1,5 @@
{{define "layout/sidebar" }}
<section id="search-2" class="widget widget_search">
<form role="search" method="get" class="search-form" action="/">
<label for="search-form-1">
<span class="screen-reader-text">搜索:</span>
</label>
<input type="search" id="search-form-1" class="search-field" placeholder="搜索…" value="{{.search}}" name="s">
<button type="submit" class="search-submit">
<svg class="icon icon-search" aria-hidden="true" role="img">
<use href="#icon-search" xlink:href="#icon-search"></use>
</svg>
<span class="screen-reader-text">搜索</span>
</button>
</form>
</section>
{{template "common/recent-posts" .}}
{{template "common/recent-comments" .}}
{{template "common/archives" .}}
{{template "common/sidebarWidget" .}}
<section id="categories-2" class="widget widget_categories">
<h2 class="widget-title">分类</h2>
<nav aria-label="分类">

View File

@ -15,6 +15,7 @@ import (
"github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/plugins/wphandle"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/theme/wp/components"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/gin-gonic/gin"
"strings"
@ -47,7 +48,7 @@ func Hook(h *wp.Handle) {
}
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
h.WidgetArea()
components.WidgetArea(h)
h.GetPassword()
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20))

View File

@ -0,0 +1,71 @@
package components
import (
"github.com/dlclark/regexp2"
)
type Block struct {
Closer string
NameSpace string
Name string
Attrs string
Void string
Len int
StartOffset int
}
var block = regexp2.MustCompile(`<!--\s+(?<closer>/)?wp:(?<namespace>[a-z][a-z0-9_-]*\/)?(?<name>[a-z][a-z0-9_-]*)\s+(?<attrs>{(?:(?:[^}]+|}+(?=})|(?!}\s+\/?-->).)*)?}\s+)?(?<void>\/)?-->`, regexp2.IgnoreCase|regexp2.Singleline)
func ParseBlock(s string) []Block {
m, err := block.FindStringMatch(s)
if err != nil {
panic(err)
}
var blocks []Block
for m != nil {
if m.GroupCount() < 1 {
continue
}
b, _ := token(m.Groups())
b.StartOffset = m.Group.Index
b.Len = m.Length
blocks = append(blocks, b)
m, _ = block.FindNextMatch(m)
}
return blocks
}
func token(g []regexp2.Group) (Block, string) {
if len(g) < 1 {
return Block{}, "no-more-tokens"
}
b := Block{NameSpace: "core/"}
for i, group := range g {
v := group.String()
if v == "" {
continue
}
switch i {
case 1:
b.Closer = v
case 2:
b.NameSpace = v
case 3:
b.Name = v
case 4:
b.Attrs = v
case 5:
b.Void = v
default:
continue
}
}
if b.Void != "" {
return b, ""
}
if b.Closer != "" {
return b, "block-closer"
}
return b, "block-opener"
}

View File

@ -1,4 +1,4 @@
package wp
package widget
import (
"fmt"
@ -6,8 +6,9 @@ import (
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings"
@ -47,8 +48,8 @@ var archiveTemplate = `{$before_widget}
{$after_widget}
`
func Archive(h *Handle) string {
args := GetComponentsArgs(h, components.ArchiveArgs, archiveArgs.Load())
func Archive(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.ArchiveArgs, archiveArgs.Load())
args = maps.FilterZeroMerge(archiveArgs.Load(), args)
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig.Load(), int64(2))
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
@ -58,7 +59,7 @@ func Archive(h *Handle) string {
} else {
s = strings.ReplaceAll(s, "{$html}", archiveUl(h, conf, args, cache.Archives(h.C)))
}
return h.ComponentFilterFnHook(components.ArchiveArgs, str.Replace(s, args))
return h.ComponentFilterFnHook(widgets.ArchiveArgs, str.Replace(s, args))
}
var dropdownScript = `
@ -76,7 +77,7 @@ var dropdownScript = `
/* ]]> */
</script>`
func archiveDropDown(h *Handle, conf map[any]any, args map[string]string, archives []models.PostArchive) string {
func archiveDropDown(h *wp.Handle, conf map[any]any, args map[string]string, archives []models.PostArchive) string {
option := str.NewBuilder()
option.Sprintf(`<option value="">%s</option>`, args["{$dropdown_label}"])
month := strings.TrimLeft(h.Index.Param.Month, "0")
@ -100,7 +101,7 @@ func archiveDropDown(h *Handle, conf map[any]any, args map[string]string, archiv
return s.String()
}
func archiveUl(h *Handle, conf map[any]any, args map[string]string, archives []models.PostArchive) string {
func archiveUl(h *wp.Handle, conf map[any]any, args map[string]string, archives []models.PostArchive) string {
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"].(string))
args["{$navCloser}"] = "</nav>"

View File

@ -1,38 +0,0 @@
{{define "common/archives"}}
<section id="archives-2" class="widget widget_archive">
<h2 class="widget-title">{{.archivesConfig.title}}</h2>
{{ if eq .archivesConfig.dropdown 1}}
<label class="screen-reader-text" for="archives-dropdown-2">{{.archivesConfig.title}}</label>
<select id="archives-dropdown-2" name="archive-dropdown">
<option value="">选择月份</option>
{{range $k,$v := .archives}}
<option {{if and (eq $.archiveYear $v.Year) (eq $.archiveMonth $v.Month) }}selected{{end}} value="/p/date/{{$v.Year}}/{{$v.Month|printf "%02s"}}"> {{$v.Year}}年{{$v.Month}}月 {{if eq $.archivesConfig.count 1}}&nbsp;({{$v.Posts}}){{end}} </option>
{{end}}
</select>
<script>
/* <![CDATA[ */
(function() {
const dropdown = document.getElementById("archives-dropdown-2");
function onSelectChange() {
if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) {
document.location.href = this.options[ this.selectedIndex ].value;
}
}
dropdown.onchange = onSelectChange;
})();
/* ]]> */
</script>
{{else}}
<nav aria-label="{{.archiveTitle}}">
<ul>
{{range $k,$v := .archives}}
<li><a href="/p/date/{{$v.Year}}/{{$v.Month|printf "%02s"}}">{{$v.Year}}年{{$v.Month}}月 {{if eq $.showArchiveCount 1}}&nbsp;({{$v.Posts}}){{end}}</a>
</li>
{{end}}
</ul>
</nav>
{{end}}
</section>
{{end}}

View File

@ -1,14 +0,0 @@
{{define "common/recent-comments"}}
<aside id="recent-comments-2" class="widget widget_recent_comments">
<h2 class="widget-title">{{.recentCommentsConfig.title}}</h2>
<nav aria-label="近期评论">
<ul id="recentcomments">
{{ range $i,$v := .recentComments}}
<li class="recentcomments">
<span class="comment-author-link">{{$v.CommentAuthor}}</span>发表在《<a class="wp-block-latest-comments__comment-link" href="/p/{{$v.CommentPostId}}#comment-{{$v.CommentId}}">{{$v.PostTitle}}</a>》
</li>
{{end}}
</ul>
</nav>
</aside>
{{end}}

View File

@ -1,18 +0,0 @@
{{define "common/recent-posts"}}
<aside id="recent-posts-2" class="widget widget_recent_entries">
<h2 class="widget-title">{{.recentPostsConfig.title}}</h2>
<nav aria-label="{{.recentPostsConfig.title}}">
<ul>
{{range $k,$v:=.recentPosts}}
<li>
<a href="/p/{{$v.Id}}">{{$v.PostTitle}}</a>
{{ if $.recentPostsConfig.show_date}}
<span class="post-date">{{ timeFormat $v.PostDateGmt "2006年01月02日"}}</span>
{{end}}
</li>
{{end}}
</ul>
</nav>
</aside>
{{end}}

View File

@ -1,4 +1,4 @@
package wp
package widget
import (
"fmt"
@ -6,8 +6,9 @@ import (
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings"
@ -46,8 +47,8 @@ var recentCommentsTemplate = `{$before_widget}
{$after_widget}
`
func RecentComments(h *Handle) string {
args := GetComponentsArgs(h, components.RecentCommentsArgs, recentCommentsArgs.Load())
func RecentComments(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.RecentCommentsArgs, recentCommentsArgs.Load())
args = maps.FilterZeroMerge(recentCommentsArgs.Load(), args)
conf := wpconfig.GetPHPArrayVal("widget_recent-comments", recentCommentConf.Load(), int64(2))
conf = maps.FilterZeroMerge(recentCommentConf.Load(), conf)
@ -64,5 +65,5 @@ func RecentComments(h *Handle) string {
</li>`, t.CommentAuthor, t.CommentId, t.CommentPostId, t.PostTitle)
})
s := strings.ReplaceAll(recentCommentsTemplate, "{$li}", strings.Join(comments, "\n"))
return h.ComponentFilterFnHook(components.RecentCommentsArgs, str.Replace(s, args))
return h.ComponentFilterFnHook(widgets.RecentCommentsArgs, str.Replace(s, args))
}

View File

@ -1,4 +1,4 @@
package wp
package widget
import (
"fmt"
@ -7,8 +7,9 @@ import (
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings"
@ -48,8 +49,8 @@ var recentConf = func() safety.Var[map[any]any] {
return v
}()
func RecentPosts(h *Handle) string {
args := GetComponentsArgs(h, components.RecentPostsArgs, recentPostsArgs.Load())
func RecentPosts(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.RecentPostsArgs, recentPostsArgs.Load())
args = maps.FilterZeroMerge(recentPostsArgs.Load(), args)
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf.Load(), int64(2))
conf = maps.FilterZeroMerge(recentConf.Load(), conf)
@ -63,7 +64,7 @@ func RecentPosts(h *Handle) string {
currentPostId = str.ToInteger(h.C.Param("id"), uint64(0))
}
posts := slice.Map(cache.RecentPosts(h.C, int(conf["number"].(int64))), func(t models.Posts) string {
t = ProjectTitle(t)
t = wp.ProjectTitle(t)
date := ""
if v, ok := conf["show_date"].(bool); ok && v {
date = fmt.Sprintf(`<span class="post-date">%s</span>`, t.PostDateGmt.Format("2006年01月02日"))
@ -78,5 +79,5 @@ func RecentPosts(h *Handle) string {
</li>`, t.Id, ariaCurrent, t.PostTitle, date)
})
s := strings.ReplaceAll(recentPostsTemplate, "{$li}", strings.Join(posts, "\n"))
return h.ComponentFilterFnHook(components.RecentPostsArgs, str.Replace(s, args))
return h.ComponentFilterFnHook(widgets.RecentPostsArgs, str.Replace(s, args))
}

View File

@ -1,4 +1,4 @@
package wp
package widget
import (
"github.com/fthvgb1/wp-go/helper/html"
@ -6,7 +6,8 @@ import (
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings"
@ -48,8 +49,8 @@ var xmlSearchForm = `<form role="search" {$aria_label} method="get" id="searchfo
</div>
</form>`
func SearchForm(h *Handle) string {
args := GetComponentsArgs(h, components.SearchFormArgs, searchArgs.Load())
func SearchForm(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.SearchFormArgs, searchArgs.Load())
args = maps.FilterZeroMerge(searchArgs.Load(), args)
if args["{$title}"] == "" {
args["{$title}"] = wpconfig.GetPHPArrayVal("widget_search", "", int64(2), "title")
@ -66,5 +67,5 @@ func SearchForm(h *Handle) string {
form = xmlSearchForm
}
s := strings.ReplaceAll(searchTemplate, "{$form}", form)
return h.ComponentFilterFnHook(components.SearchFormArgs, str.Replace(s, args))
return h.ComponentFilterFnHook(widgets.SearchFormArgs, str.Replace(s, args))
}

View File

@ -0,0 +1,33 @@
package components
import (
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/theme/wp/components/widget"
"github.com/fthvgb1/wp-go/internal/wpconfig"
)
var widgets = map[string]func(*wp.Handle) string{
"search-2": widget.SearchForm,
"recent-posts-2": widget.RecentPosts,
"recent-comments-2": widget.RecentComments,
"archives-2": widget.Archive,
}
func WidgetArea(h *wp.Handle) {
v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1")
sidebar := slice.FilterAndMap(v, func(t any) (func(*wp.Handle) string, bool) {
vv := t.(string)
fn, ok := widgets[vv]
if ok {
return fn, true
}
return nil, false
})
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(func(h *wp.Handle) {
h.PushGroupComponentFns(constraints.SidebarsWidgets, 10, sidebar...)
}, 30))
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
}

View File

@ -1,31 +0,0 @@
package wp
import (
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/wpconfig"
)
var widgets = map[string]func(*Handle) string{
"search-2": SearchForm,
"recent-posts-2": RecentPosts,
"recent-comments-2": RecentComments,
"archives-2": Archive,
}
func (h *Handle) WidgetArea() {
v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1")
sidebar := slice.FilterAndMap(v, func(t any) (func(*Handle) string, bool) {
widget := t.(string)
fn, ok := widgets[widget]
if ok {
return fn, true
}
return nil, false
})
h.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) {
h.PushGroupComponentFns(constraints.SidebarsWidgets, 10, sidebar...)
}, 30))
h.ginH["categories"] = cache.CategoriesTags(h.C, constraints.Category)
}