You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
caddy-hugo2/templates.go

119 lines
2.6 KiB

8 years ago
package caddyhugo
import (
2 years ago
"bytes"
"encoding/base64"
8 years ago
"fmt"
2 years ago
"html/template"
8 years ago
"net/http"
"os"
"path"
"path/filepath"
"strings"
2 years ago
"sync"
8 years ago
"git.stephensearles.com/stephen/acedoc"
2 years ago
"git.stephensearles.com/stephen/caddy-hugo2/frontend"
8 years ago
)
func (t *tmplData) Content() ([]Content, error) {
return GetContent(t.Site.Root, t.HugoSites)
8 years ago
}
func (t *tmplData) ContentTypes() ([]string, error) {
nameMap := map[string]struct{}{"default": struct{}{}}
names, err := t.contentTypes(path.Join(t.Site.Root, "archetypes"))
if err != nil {
return nil, err
}
for _, name := range names {
nameMap[name] = struct{}{}
}
names, err = t.contentTypes(path.Join(t.Site.Root, "themes", "hugo-theme-minos", "archetypes"))
if err != nil {
return nil, err
}
for _, name := range names {
nameMap[name] = struct{}{}
}
var out []string
for name := range nameMap {
out = append(out, name[:len(name)-len(filepath.Ext(name))])
}
return out, nil
}
func (t *tmplData) contentTypes(dir string) ([]string, error) {
8 years ago
layoutDir, err := os.Open(path.Join(t.Site.Root, "archetypes"))
if err != nil {
fmt.Println("opening layout dir", err)
return nil, err
}
defer layoutDir.Close()
names, err := layoutDir.Readdirnames(0)
if err != nil {
fmt.Println("reading dir", err)
return nil, err
}
return names, nil
8 years ago
}
type tmplData struct {
Site SiteConfig
8 years ago
R *http.Request
*CaddyHugo
Doc *acedoc.Document
docref *editSession
8 years ago
}
func (t *tmplData) LoadContent() (string, error) {
return t.Doc.Contents(), nil
8 years ago
}
func baseNoExt(name string) string {
base := path.Base(name)
return base[:len(base)-len(path.Ext(base))]
}
func (t *tmplData) IframeSource() string {
name := baseNoExt(t.docref.docname)
ctype := baseNoExt(path.Dir(t.docref.docname))
if ctype == "content" {
return fmt.Sprintf("/hugo/draft/%s/%s/", base64.RawURLEncoding.EncodeToString([]byte(t.docref.docname)), strings.Replace(name, " ", "-", -1))
}
return fmt.Sprintf("/hugo/draft/%s/%s/%s/", base64.RawURLEncoding.EncodeToString([]byte(t.docref.docname)), ctype, strings.Replace(name, " ", "-", -1))
}
2 years ago
var EditPage = frontend.EditPage()
2 years ago
var AdminPage = frontend.AdminPage()
2 years ago
var AuthorPage = frontend.AuthorPage()
2 years ago
var uploadTmpl *template.Template
var uploadTmplOnce sync.Once
func UploadPage(elemName string) string {
2 years ago
uploadTmplOnce.Do(func() {
p := frontend.UploadPage()
t, err := template.New("").Parse(p)
if err != nil {
panic(err)
}
2 years ago
uploadTmpl = t
})
2 years ago
var buf bytes.Buffer
err := uploadTmpl.Execute(&buf, struct{ ElemName string }{elemName})
if err != nil {
panic(err)
}
2 years ago
return buf.String()
}