Update template mode

pull/4/head
Leonid Maslakov 4 months ago
parent a99b934bd3
commit 1427cd9f8b
  1. 5
      README.md
  2. 18
      docs/page.md
  3. 18
      internal/cfg/cfg.go
  4. 2
      internal/handler/handler_htmlp.go
  5. 2
      internal/handler/handler_md.go
  6. 48
      internal/handler/page_part.go
  7. 4
      internal/handler/share.go
  8. 110
      internal/handler/template_mode.go

@ -78,7 +78,10 @@ Default:
"Sufix": ""
},
"AddToHead": [],
"EnableTemplateMode": true
"TemplateMode": {
"EnableForMainPage": true,
"EnableForPagePart": true
}
}
}
```

@ -36,9 +36,17 @@ Error page:
## Template mode
| Varible | Description | Example |
| --------------- | ------------------------------------------| ----------------------------------- |
| `{{.URL.Path}}` | Path without a domain name and protocol. | `/kb/my_page.md` |
| `{{.URL.Full}}` | Full path with domain name and protocol. | `https://example.org/kb/my_page.md` |
| Varible | Example |
| ------------------------------------ | -------------------------------------------------------------------- |
| `{{.Request.ClientIP}}` | `172.17.0.1` |
| `{{.Request.Method}}` | `GET` |
| `{{.Request.URL.Protocol}}` | `http` |
| `{{.Request.URL.Path}}` | `/kb/my_page.md` |
| `{{.Request.URL.RawQuery}}` | `v=11&1=99` |
| `{{.Request.URL.Full}}` | `https://example.org/kb/my_page.md` |
| `{{.Request.Header.AcceptLanguage}}` | `en` |
| `{{.Request.Header.Host}}` | `example.org` |
| `{{.Request.Header.Referer}}` | `https://yandex.ru` |
| `{{.Request.Header.UserAgent}}` | `Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0` |
If you want to escape built in variable, you do this: `\{{\{MY_VAR\}\}`.
If you want to escape built in variable, you do this: `\{\{MY_VAR\}\}`.

@ -59,9 +59,9 @@ type ConfigSiteMap struct {
}
type ConfigPage struct {
AutoTitle ConfigPageAutoTitle
AddToHead []string
EnableTemplateMode bool
AutoTitle ConfigPageAutoTitle
AddToHead []string
TemplateMode ConfigPageTemplateMode
}
type ConfigPageAutoTitle struct {
@ -70,6 +70,11 @@ type ConfigPageAutoTitle struct {
Sufix string
}
type ConfigPageTemplateMode struct {
EnableForMainPage bool
EnableForPagePart bool
}
func Read(path string) (Config, error) {
// Default
config := Config{
@ -99,8 +104,11 @@ func Read(path string) (Config, error) {
Prefix: "",
Sufix: "",
},
AddToHead: []string{},
EnableTemplateMode: true,
AddToHead: []string{},
TemplateMode: ConfigPageTemplateMode{
EnableForMainPage: true,
EnableForPagePart: true,
},
},
}

@ -32,7 +32,7 @@ func htmlpHand(rw http.ResponseWriter, req *http.Request, path string) {
}
// Template mode
if Config.Page.EnableTemplateMode == true {
if Config.Page.TemplateMode.EnableForMainPage == true {
page = useTemplate(page, req)
}

@ -33,7 +33,7 @@ func mdHand(rw http.ResponseWriter, req *http.Request, path string) {
}
// Template mode
if Config.Page.EnableTemplateMode == true {
if Config.Page.TemplateMode.EnableForMainPage == true {
page = useTemplate(page, req)
}

@ -19,55 +19,11 @@
package handler
import (
"bytes"
"github.com/lcomrade/md2html/v2"
"net/http"
"path/filepath"
"strings"
"text/template"
)
type templateData struct {
URL templateDataURL
}
type templateDataURL struct {
Path string
Full string
}
func useTemplate(text string, req *http.Request) string {
// Prepare template data
tmplData := templateData{
URL: templateDataURL{
Path: req.URL.Path,
Full: getFullURL(req),
},
}
// New template
tmpl, err := template.New("Template").Parse(text)
if err != nil {
return text
}
// Execute template
var buffer bytes.Buffer
err = tmpl.Execute(&buffer, tmplData)
if err != nil {
return text
}
text = buffer.String()
// Escape
text = strings.Replace(text, `\{`, "{", -1)
text = strings.Replace(text, `\}`, "}", -1)
return text
}
func pagePart(nameWithoutExt string, req *http.Request) string {
basePath := filepath.Join(Config.WebRoot, nameWithoutExt)
@ -75,7 +31,7 @@ func pagePart(nameWithoutExt string, req *http.Request) string {
page, err := readFile(basePath + ".htmlp")
if err == nil {
// Template mode
if Config.Page.EnableTemplateMode == true {
if Config.Page.TemplateMode.EnableForPagePart == true {
page = useTemplate(page, req)
}
@ -87,7 +43,7 @@ func pagePart(nameWithoutExt string, req *http.Request) string {
page, err = readFile(basePath + ".md")
if err == nil {
// Template mode
if Config.Page.EnableTemplateMode == true {
if Config.Page.TemplateMode.EnableForPagePart == true {
page = useTemplate(page, req)
}

@ -47,7 +47,3 @@ func getSiteURL(req *http.Request) string {
// Return
return protocol + "://" + hostname
}
func getFullURL(req *http.Request) string {
return getSiteURL(req) + req.URL.Path
}

@ -0,0 +1,110 @@
// Copyright (C) 2022 Leonid Maslakov.
// This file is part of wallblog.
// wallblog is free software: you can redistribute it
// and/or modify it under the terms of the
// GNU Affero Public License as published by the
// Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
// wallblog is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero Public License for more details.
// You should have received a copy of the GNU Affero Public License along with wallblog.
// If not, see <https://www.gnu.org/licenses/>.
package handler
import (
"bytes"
"net/http"
"strings"
"text/template"
)
type templateData struct {
Request templateDataRequest
}
type templateDataRequest struct {
ClientIP string
Method string
URL templateDataURL
Header templateDataHeader
}
type templateDataURL struct {
Protocol string
Path string
RawQuery string
Full string
}
type templateDataHeader struct {
AcceptLanguage string
Host string
Referer string
UserAgent string
}
func useTemplate(text string, req *http.Request) string {
// Prepare template data
tmplData := templateData{
Request: templateDataRequest{
ClientIP: strings.Split(req.RemoteAddr, ":")[0],
Method: req.Method,
URL: templateDataURL{
Protocol: req.URL.Scheme,
Path: req.URL.Path,
RawQuery: req.URL.RawQuery,
Full: getSiteURL(req) + req.URL.Path,
},
Header: templateDataHeader{
AcceptLanguage: strings.Split(req.Header.Get("Accept-Language"), ";")[0],
Host: req.Host,
Referer: req.Header.Get("Referer"),
UserAgent: req.Header.Get("User-Agent"),
},
},
}
// Get scheme
if tmplData.Request.URL.Protocol == "" {
if req.URL.Port() == "443" {
tmplData.Request.URL.Protocol = "https"
} else {
tmplData.Request.URL.Protocol = "http"
}
}
// Get full URL
if req.URL.RawQuery != "" {
tmplData.Request.URL.Full = tmplData.Request.URL.Full + "?" + req.URL.RawQuery
}
// New template
tmpl, err := template.New("Template").Parse(text)
if err != nil {
return err.Error()
}
// Execute template
var buffer bytes.Buffer
err = tmpl.Execute(&buffer, tmplData)
if err != nil {
return err.Error()
}
text = buffer.String()
// Escape
text = strings.Replace(text, `\{`, "{", -1)
text = strings.Replace(text, `\}`, "}", -1)
return text
}
Loading…
Cancel
Save