diff --git a/go.mod b/go.mod index 73273df..f2cd1fb 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,11 @@ module pageswiki -go 1.22 +go 1.22.12 require ( github.com/gomarkdown/markdown v0.0.0-20260417124207-7d523f7318df github.com/google/uuid v1.6.0 + golang.org/x/text v0.22.0 modernc.org/sqlite v1.32.0 ) diff --git a/go.sum b/go.sum index 9f90df7..a80e4b1 100644 --- a/go.sum +++ b/go.sum @@ -16,13 +16,17 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= -golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= -golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w= +golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw= -golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc= +golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= +golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= modernc.org/cc/v4 v4.21.4 h1:3Be/Rdo1fpr8GrQ7IVw9OHtplU4gWbb+wNgeoBMmGLQ= modernc.org/cc/v4 v4.21.4/go.mod h1:HM7VJTZbUCR3rV8EYBi9wxnJ0ZBRiGE5OeGXNA0IsLQ= modernc.org/ccgo/v4 v4.19.2 h1:lwQZgvboKD0jBwdaeVCTouxhxAyN6iawF3STraAal8Y= diff --git a/internal/server/config_handler.go b/internal/server/config_handler.go index 8ee2432..00aa237 100644 --- a/internal/server/config_handler.go +++ b/internal/server/config_handler.go @@ -46,6 +46,18 @@ func (s *Server) handleConfig( return } + err = s.store.SetConfig( + "locale", + normalizeLocale( + strings.TrimSpace(r.FormValue("locale")), + ), + ) + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + case "nav": labels := r.Form["nav_label"] @@ -149,6 +161,7 @@ func (s *Server) handleConfig( map[string]any{ "HomePath": homePath, "WikiName": wikiName, + "Locale": s.getLocale(), "Objects": recent, "NavItemsConfig": s.getNavItems(), }, diff --git a/internal/server/handlers.go b/internal/server/handlers.go index f964170..573b7f6 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -3,6 +3,7 @@ package server import ( "html/template" "net/http" + "strings" ) func (s *Server) handleHome( @@ -20,49 +21,74 @@ func (s *Server) handleHome( return } + homePath = + strings.TrimSpace( + homePath, + ) + if homePath != "" { + if !strings.HasPrefix(homePath, "/") { + homePath = "/" + homePath + } + objectUUID, err := s.store.ResolvePath( homePath, ) - if err == nil { + if err != nil { - latest, err := - s.store.GetLatestRevision( - objectUUID, - ) + http.Error( + w, + "configured home_path is not resolvable: "+homePath, + 500, + ) - if err == nil { - - rendered := - s.expandMacros( - latest.Body, - homePath, - ) - - s.render( - w, - "page.gohtml", - map[string]any{ - "Title": homePath, - "Path": homePath, - "ObjectUUID": objectUUID, - "RenderedHTML": template.HTML( - rendered, - ), - }, - ) - - return - } + return } + + latest, err := + s.store.GetLatestRevision( + objectUUID, + ) + + if err != nil { + + http.Error( + w, + err.Error(), + 500, + ) + + return + } + + rendered := + s.expandMacros( + latest.Body, + homePath, + ) + + s.render( + w, + "page.gohtml", + map[string]any{ + "Title": homePath, + "Path": homePath, + "ObjectUUID": objectUUID, + "RenderedHTML": template.HTML( + rendered, + ), + }, + ) + + return } rendered := s.expandMacros( - "{{children}}", + "{{children:/ exclude=/draft,/-,/_}}", "/", ) diff --git a/internal/server/i18n.go b/internal/server/i18n.go new file mode 100644 index 0000000..a3065ce --- /dev/null +++ b/internal/server/i18n.go @@ -0,0 +1,104 @@ +package server + +import ( + "golang.org/x/text/language" +) + +var translations = map[string]map[string]string{ + "en": { + "config.title": "Config", + "config.home": "Home", + "config.wiki_name": "Wiki name", + "config.locale": "Language", + "config.home_path": "Home semantic path", + "config.save": "Save", + "config.nav": "Navigation", + "config.objects": "Recent Objects", + "config.primary": "Primary Link", + "config.size": "Size", + "config.updated": "Updated", + "config.object": "Object", + "config.nav_label": "Label", + "config.nav_href": "Href", + "config.nav_order": "Order", + "config.nav_save": "Save Navigation", + }, + "ja": { + "config.title": "設定", + "config.home": "ホーム", + "config.wiki_name": "Wiki名", + "config.locale": "言語", + "config.home_path": "トップページ", + "config.save": "保存", + "config.nav": "ナビゲーション", + "config.objects": "最近のオブジェクト", + "config.primary": "主なリンク", + "config.size": "サイズ", + "config.updated": "最終更新", + "config.object": "オブジェクト", + "config.nav_label": "表示名", + "config.nav_href": "リンク先", + "config.nav_order": "順序", + "config.nav_save": "ナビゲーションを保存", + }, +} + +func normalizeLocale( + raw string, +) string { + + if raw == "" { + return "en" + } + + tag, err := + language.Parse(raw) + + if err != nil { + return "en" + } + + base, _ := + tag.Base() + + switch base.String() { + + case "ja": + return "ja" + + default: + return "en" + } +} + +func (s *Server) getLocale() string { + + raw, err := + s.store.GetConfig( + "locale", + ) + + if err != nil { + return "en" + } + + return normalizeLocale(raw) +} + +func (s *Server) T( + key string, +) string { + + locale := + s.getLocale() + + if v, ok := translations[locale][key]; ok { + return v + } + + if v, ok := translations["en"][key]; ok { + return v + } + + return key +} diff --git a/internal/server/macro_expand.go b/internal/server/macro_expand.go index 9f6934c..5b050da 100644 --- a/internal/server/macro_expand.go +++ b/internal/server/macro_expand.go @@ -7,7 +7,65 @@ import ( "strings" ) -var childrenMacroRe = regexp.MustCompile(`\{\{children(?::([^}\s]+))?(?:\s+exclude=([^}]+))?\}\}`) +var childrenMacroRe = regexp.MustCompile(`\{\{children([^}]*)\}\}`) + +func parseChildrenMacroArgs( + raw string, + currentPath string, +) (string, []string) { + + targetPath := currentPath + excludeRaw := "" + + raw = strings.TrimSpace(raw) + + if strings.HasPrefix(raw, ":") { + + raw = strings.TrimSpace( + strings.TrimPrefix(raw, ":"), + ) + + parts := + strings.Fields(raw) + + if len(parts) > 0 { + + targetPath = parts[0] + + for _, part := range parts[1:] { + + if strings.HasPrefix(part, "exclude=") { + + excludeRaw = + strings.TrimPrefix( + part, + "exclude=", + ) + } + } + } + + } else { + + for _, part := range strings.Fields(raw) { + + if strings.HasPrefix(part, "exclude=") { + + excludeRaw = + strings.TrimPrefix( + part, + "exclude=", + ) + } + } + } + + if !strings.HasPrefix(targetPath, "/") { + targetPath = "/" + targetPath + } + + return targetPath, splitExcludePatterns(excludeRaw) +} func splitExcludePatterns( raw string, @@ -38,13 +96,18 @@ func isExcludedPath( patterns []string, ) bool { + path = strings.TrimRight(path, "/") + for _, p := range patterns { + p = strings.TrimRight(p, "/") + + if p == "" { + continue + } + if path == p || - strings.HasPrefix( - path, - p+"/", - ) { + strings.HasPrefix(path, p+"/") { return true } @@ -65,34 +128,16 @@ func (s *Server) expandMacros( matches := childrenMacroRe.FindStringSubmatch(m) - targetPath := - currentPath + raw := "" - excludeRaw := "" - - if len(matches) >= 2 && - strings.TrimSpace(matches[1]) != "" { - - targetPath = - strings.TrimSpace( - matches[1], - ) + if len(matches) >= 2 { + raw = matches[1] } - if len(matches) >= 3 { - excludeRaw = - strings.TrimSpace( - matches[2], - ) - } - - if !strings.HasPrefix(targetPath, "/") { - targetPath = "/" + targetPath - } - - excludes := - splitExcludePatterns( - excludeRaw, + targetPath, excludes := + parseChildrenMacroArgs( + raw, + currentPath, ) children, err := @@ -114,10 +159,7 @@ func (s *Server) expandMacros( for _, child := range children { - if isExcludedPath( - child, - excludes, - ) { + if isExcludedPath(child, excludes) { continue } diff --git a/internal/server/render.go b/internal/server/render.go index 54d58d7..bae7e73 100644 --- a/internal/server/render.go +++ b/internal/server/render.go @@ -13,6 +13,7 @@ func mergeTemplateData( out := map[string]any{ "NavItems": nav, "WikiName": wikiName, + "Locale": "en", } if data == nil { diff --git a/internal/server/server.go b/internal/server/server.go index f22c532..9194582 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -99,6 +99,8 @@ func (s *Server) mustLoadTemplates() { "parentPath": parentPath, + "T": s.T, + "sub": func(a, b int) int { return a - b }, diff --git a/templates/pages/config.gohtml b/templates/pages/config.gohtml index 0cb730f..0bf90f0 100644 --- a/templates/pages/config.gohtml +++ b/templates/pages/config.gohtml @@ -80,7 +80,7 @@ Navigation > -Home semantic path +{{ T "config.locale" }} + + + + +
+
+ + -

Recent Objects

+

{{ T "config.objects" }}

- - - - + + + + @@ -176,7 +195,7 @@ Save class="config-section config-panel" > -

Navigation

+

{{ T "config.nav" }}

- - - + + +
Primary LinkSizeUpdatedObject{{ T "config.primary" }}{{ T "config.size" }}{{ T "config.updated" }}{{ T "config.object" }}
LabelHrefOrder{{ T "config.nav_label" }}{{ T "config.nav_href" }}{{ T "config.nav_order" }}