mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-02 10:56:25 -07:00
Add template loading to the templates manage action
This commit is contained in:
parent
a8436c5ee4
commit
7e07e24f16
6 changed files with 87 additions and 55 deletions
|
@ -165,8 +165,6 @@ type Post struct {
|
|||
DeletedAt time.Time // sql: `deleted_at`
|
||||
IsDeleted bool // sql: `is_deleted`
|
||||
BannedMessage string // sql: `banned_message`
|
||||
|
||||
sanitized bool
|
||||
}
|
||||
|
||||
// table: DBPREFIXreports
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package gctemplates
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"path"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
luar "layeh.com/gopher-luar"
|
||||
)
|
||||
|
@ -14,7 +17,7 @@ func PreloadModule(l *lua.LState) int {
|
|||
for i := 0; i < l.GetTop(); i++ {
|
||||
tmplPaths = append(tmplPaths, l.CheckString(i+1))
|
||||
}
|
||||
tmpl, err := loadTemplate(tmplPaths...)
|
||||
tmpl, err := template.New(path.Base(tmplPaths[0])).Funcs(funcMap).ParseFiles(tmplPaths...)
|
||||
l.Push(luar.New(l, tmpl))
|
||||
l.Push(luar.New(l, err))
|
||||
return 2
|
||||
|
|
|
@ -2,7 +2,6 @@ package gctemplates
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"os"
|
||||
"path"
|
||||
|
@ -144,10 +143,26 @@ var (
|
|||
type gochanTemplate struct {
|
||||
files []string
|
||||
tmpl *template.Template
|
||||
isOverride bool
|
||||
filePath string
|
||||
}
|
||||
|
||||
func (gt *gochanTemplate) Load() (err error) {
|
||||
gt.tmpl, err = loadTemplate(gt.files...)
|
||||
templateDir := config.GetSystemCriticalConfig().TemplateDir
|
||||
|
||||
var filePaths []string
|
||||
for _, file := range gt.files {
|
||||
|
||||
if _, err = os.Stat(path.Join(templateDir, "override", file)); os.IsNotExist(err) {
|
||||
filePaths = append(filePaths, path.Join(templateDir, file))
|
||||
} else if err == nil {
|
||||
filePaths = append(filePaths, path.Join(templateDir, "override", file))
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
gt.filePath = filePaths[0]
|
||||
gt.tmpl, err = template.New(gt.files[0]).Funcs(funcMap).ParseFiles(filePaths...)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -155,56 +170,52 @@ func (gt *gochanTemplate) Template() *template.Template {
|
|||
return gt.tmpl
|
||||
}
|
||||
|
||||
// IsOverride returns true if the base file is overriden (i.e., it exist in the overrides subdirectory
|
||||
// of the templates directory)
|
||||
func (gt *gochanTemplate) IsOverride() bool {
|
||||
return gt.isOverride
|
||||
}
|
||||
|
||||
// TemplatePath returns the path to the base template file
|
||||
func (gt *gochanTemplate) TemplatePath() string {
|
||||
return gt.filePath
|
||||
}
|
||||
|
||||
// GetTemplate takes the filename of the template and returns the template if it exists and
|
||||
// is already loaded, and attempts to load and then return it if it exists but isn't loaded
|
||||
func GetTemplate(name string) (*template.Template, error) {
|
||||
gctmpl, ok := templateMap[name]
|
||||
if !ok {
|
||||
fmt.Printf("Unrecognized template %q\n", name)
|
||||
return nil, ErrUnrecognizedTemplate
|
||||
}
|
||||
if gctmpl.tmpl != nil {
|
||||
return gctmpl.tmpl, nil
|
||||
}
|
||||
var err error
|
||||
gctmpl.tmpl, err = loadTemplate(gctmpl.files...)
|
||||
err := gctmpl.Load()
|
||||
return gctmpl.tmpl, err
|
||||
}
|
||||
|
||||
func loadTemplate(files ...string) (*template.Template, error) {
|
||||
var templates []string
|
||||
templateDir := config.GetSystemCriticalConfig().TemplateDir
|
||||
var foundFiles []string
|
||||
for i, file := range files {
|
||||
foundFiles = append(foundFiles, file)
|
||||
templates = append(templates, file)
|
||||
tmplPath := path.Join(templateDir, "override", file)
|
||||
|
||||
if _, err := os.Stat(tmplPath); err == nil {
|
||||
foundFiles[i] = tmplPath
|
||||
} else if os.IsNotExist(err) {
|
||||
foundFiles[i] = path.Join(templateDir, file)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
func GetTemplatePath(name string) (string, error) {
|
||||
gctmpl, ok := templateMap[name]
|
||||
if !ok {
|
||||
return "", ErrUnrecognizedTemplate
|
||||
}
|
||||
return gctmpl.filePath, nil
|
||||
}
|
||||
|
||||
tmpl, err := template.New(templates[0]).Funcs(funcMap).ParseFiles(foundFiles...)
|
||||
return tmpl, templateError(templates[0], err)
|
||||
// GetTemplateList returns a string array of all valid template filenames
|
||||
func GetTemplateList() []string {
|
||||
var templateList []string
|
||||
for t := range templateMap {
|
||||
templateList = append(templateList, t)
|
||||
}
|
||||
return templateList
|
||||
}
|
||||
|
||||
func ParseTemplate(name, tmplStr string) (*template.Template, error) {
|
||||
return template.New(name).Funcs(funcMap).Parse(tmplStr)
|
||||
}
|
||||
|
||||
func templateError(name string, err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
templateDir := config.GetSystemCriticalConfig().TemplateDir
|
||||
|
||||
return fmt.Errorf("failed loading template '%s: %s': %s",
|
||||
templateDir, name, err.Error())
|
||||
}
|
||||
|
||||
// InitTemplates loads the given templates by name. If no parameters are given,
|
||||
// all templates are (re)loaded
|
||||
func InitTemplates(which ...string) error {
|
||||
|
|
|
@ -418,6 +418,38 @@ func registerAdminPages() {
|
|||
return buffer.String(), nil
|
||||
},
|
||||
},
|
||||
Action{
|
||||
ID: "templates",
|
||||
Title: "Override templates",
|
||||
Permissions: AdminPerms,
|
||||
Callback: func(writer http.ResponseWriter, request *http.Request, staff *gcsql.Staff, wantsJSON bool, infoEv, errEv *zerolog.Event) (output interface{}, err error) {
|
||||
buf := bytes.NewBufferString("")
|
||||
|
||||
selectedTemplate := request.PostFormValue("templateselect")
|
||||
var templateStr string
|
||||
if selectedTemplate != "" {
|
||||
errEv.Str("selectedTemplate", selectedTemplate)
|
||||
|
||||
templatePath, err := gctemplates.GetTemplatePath(selectedTemplate)
|
||||
if err != nil {
|
||||
errEv.Err(err).Caller().Msg("unable to load selected template")
|
||||
return "", fmt.Errorf("template %q does not exist", selectedTemplate)
|
||||
}
|
||||
ba, err := os.ReadFile(templatePath)
|
||||
if err != nil {
|
||||
errEv.Err(err).Caller().Send()
|
||||
return "", fmt.Errorf("unable to load selected template %q", selectedTemplate)
|
||||
}
|
||||
templateStr = string(ba)
|
||||
}
|
||||
|
||||
serverutil.MinifyTemplate(gctemplates.ManageTemplates, map[string]any{
|
||||
"templates": gctemplates.GetTemplateList(),
|
||||
"selectedTemplate": selectedTemplate,
|
||||
"templateText": templateStr,
|
||||
}, buf, "text/html")
|
||||
return buf.String(), nil
|
||||
}},
|
||||
Action{
|
||||
ID: "rebuildfront",
|
||||
Title: "Rebuild front page",
|
||||
|
@ -577,19 +609,6 @@ func registerAdminPages() {
|
|||
outputStr += "Done building boards<hr />"
|
||||
return outputStr, nil
|
||||
}},
|
||||
Action{
|
||||
ID: "templates",
|
||||
Title: "Creat/Edit template overrides",
|
||||
Permissions: AdminPerms,
|
||||
Callback: func(writer http.ResponseWriter, request *http.Request, staff *gcsql.Staff, wantsJSON bool, infoEv, errEv *zerolog.Event) (output interface{}, err error) {
|
||||
buf := bytes.NewBufferString("")
|
||||
|
||||
serverutil.MinifyTemplate(gctemplates.ManageTemplates, map[string]any{
|
||||
"template": "manage_stuff.html",
|
||||
"templateText": "template goes here",
|
||||
}, buf, "text/html")
|
||||
return buf.String(), nil
|
||||
}},
|
||||
Action{
|
||||
ID: "wordfilters",
|
||||
Title: "Wordfilters",
|
||||
|
|
|
@ -41,7 +41,7 @@ The following are modules that can be loaded via `require("modulename")`. See [.
|
|||
|
||||
## gctemplates
|
||||
- **gctemplates.load_template(files...)**
|
||||
- Calls [gctemplates.LoadTemplate](https://pkg.go.dev/github.com/gochan-org/gochan/pkg/gctemplates#LoadTemplate) using the given `files` and returns a [Template](https://pkg.go.dev/html/template#Template) and an error object (or nil if there were no errors).
|
||||
- Loads the given file paths into a [Template](https://pkg.go.dev/html/template#Template), using the base filename as the name, and returns the template and an error if one occured.
|
||||
- **gctemplates.parse_template(template_name string, template_data string)**
|
||||
- Calls [gctemplates.ParseTemplate](https://pkg.go.dev/github.com/gochan-org/gochan/pkg/gctemplates#ParseTemplate) with the given template name and Go template data, and returns a [Template](https://pkg.go.dev/html/template#Template) and an error object (or nil if there were no errors).
|
||||
|
||||
|
|
|
@ -1,16 +1,17 @@
|
|||
<div style="text-align: center;">
|
||||
<form action="{{webPath "manage/templates"}}" method="POST" id="template-override">
|
||||
{{with $.templateText}}
|
||||
<b>Editing: {{$.currentTemplate}}</b>
|
||||
<textarea class="template-text" rows="16" spellcheck="false">{{$.templateText}}</textarea>
|
||||
<b>Editing: {{$.selectedTemplate}}</b>
|
||||
<textarea name="templatetext" class="template-text" rows="16" spellcheck="false">{{$.templateText}}</textarea>
|
||||
<input type="submit" name="dooverride" value="Submit" onsubmit="return prompt('Are you sure you want to override the template?')">
|
||||
<input type="submit" name="cancel" value="Cancel"/>
|
||||
{{else}}
|
||||
Select a template: <select name="templateselect">
|
||||
{{range $t, $template := .templates}}
|
||||
<option value="$template"></option>
|
||||
<option value="{{$template}}">{{$template}}</option>
|
||||
{{end}}
|
||||
</select>
|
||||
<input type="submit" name="dotemplatechoose" value="Select template" />
|
||||
{{end}}
|
||||
</form>
|
||||
</div>
|
Loading…
Add table
Add a link
Reference in a new issue