package caddyhugo import ( "encoding/base64" "fmt" "net/http" "os" "path" "path/filepath" "sort" "strings" "time" "git.stephensearles.com/stephen/acedoc" "github.com/mholt/caddy/caddyhttp/httpserver" ) func (t tmplData) Content() ([]string, error) { type file struct { name string modtime time.Time } var files = []file{} err := filepath.Walk(path.Join(t.Site.Root, "content"), func(name string, fi os.FileInfo, err error) error { if err != nil { return err } if fi.IsDir() { return nil } name, err = filepath.Rel(t.Site.Root, name) if err != nil { return err } files = append(files, file{name, fi.ModTime()}) return nil }) sort.Slice(files, func(i, j int) bool { return files[i].modtime.Before(files[j].modtime) }) if err != nil { fmt.Println(err) return nil, err } var filenames []string for _, file := range files { filenames = append(filenames, file.name) } return filenames, nil } 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) { 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 } type tmplData struct { Site *httpserver.SiteConfig R *http.Request CaddyHugo Doc *acedoc.Document docref *docref } func (t tmplData) LoadContent() (string, error) { return t.Doc.Contents(), nil } 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.name) ctype := baseNoExt(path.Dir(t.docref.name)) if ctype == "content" { return fmt.Sprintf("/hugo/draft/%s/%s/", base64.RawURLEncoding.EncodeToString([]byte(t.docref.name)), strings.Replace(name, " ", "-", -1)) } return fmt.Sprintf("/hugo/draft/%s/%s/%s/", base64.RawURLEncoding.EncodeToString([]byte(t.docref.name)), ctype, strings.Replace(name, " ", "-", -1)) } var EditPage = `
` var AdminPage = `not implemented` var AuthorPage = `

Create content:

Edit content:

` func UploadPage(elemName string) string { return fmt.Sprintf(` `, elemName, elemName, elemName, elemName, elemName, elemName) }