Hugo Updates

pull/185/head
Henrique Dias 2017-07-29 10:14:34 +01:00
parent b61a989958
commit b863033d7a
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
6 changed files with 32 additions and 2474 deletions

File diff suppressed because one or more lines are too long

View File

@ -3,7 +3,6 @@ package hugo
import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"net/http"
"os"
@ -12,16 +11,12 @@ import (
"strings"
"github.com/hacdias/filemanager"
"github.com/hacdias/filemanager/plugins"
"github.com/hacdias/fileutils"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
var (
errHugoNotFound = errors.New("It seems that tou don't have 'hugo' on your PATH")
errUnsupportedFileType = errors.New("The type of the provided file isn't supported for this action")
)
// setup configures a new FileManager middleware instance.
func setup(c *caddy.Controller) error {
configs, err := parse(c)
@ -120,7 +115,7 @@ func parse(c *caddy.Controller) ([]*filemanager.FileManager, error) {
}
// Initialize the default settings for Hugo.
hugo := &hugo{
hugo := &plugins.Hugo{
Root: directory,
Public: filepath.Join(directory, "public"),
Args: []string{},
@ -129,7 +124,7 @@ func parse(c *caddy.Controller) ([]*filemanager.FileManager, error) {
// Try to find the Hugo executable path.
if hugo.Exe, err = exec.LookPath("hugo"); err != nil {
return nil, errHugoNotFound
return nil, plugins.ErrHugoNotFound
}
err = m.RegisterPlugin("hugo", hugo)

View File

@ -80,8 +80,6 @@
if: function (data, route) {
return (data.store.state.req.kind === 'editor' &&
!data.store.state.loading &&
data.store.state.req.metadata !== undefined &&
data.store.state.req.metadata !== null &&
data.store.state.user.allowEdit &
data.store.state.user.permissions.allowPublish)
},

View File

@ -1,6 +1,7 @@
package hugo
package plugins
import (
"errors"
"log"
"net/http"
"os"
@ -14,7 +15,13 @@ import (
"github.com/robfig/cron"
)
type hugo struct {
var (
ErrHugoNotFound = errors.New("It seems that tou don't have 'hugo' on your PATH")
ErrUnsupportedFileType = errors.New("The type of the provided file isn't supported for this action")
)
// Hugo is a hugo (https://gohugo.io) plugin.
type Hugo struct {
// Website root
Root string `description:"The relative or absolute path to the place where your website is located."`
// Public folder
@ -25,11 +32,9 @@ type hugo struct {
Args []string `description:"The arguments to run when running Hugo"`
// Indicates if we should clean public before a new publish.
CleanPublic bool `description:"Indicates if the public folder should be cleaned before publishing the website."`
// TODO: admin interface to cgange options
}
func (h hugo) BeforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
func (h Hugo) BeforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
// If we are using the 'magic url' for the settings, we should redirect the
// request for the acutual path.
if r.URL.Path == "/settings/" || r.URL.Path == "/settings" {
@ -49,7 +54,6 @@ func (h hugo) BeforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r
}
r.URL.Path = "/config." + frontmatter
return 0, nil
}
// From here on, we only care about 'hugo' router so we can bypass
@ -78,7 +82,7 @@ func (h hugo) BeforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r
// If the request isn't for a markdown file, we can't
// handle it.
if ext != ".markdown" && ext != ".md" {
return http.StatusBadRequest, errUnsupportedFileType
return http.StatusBadRequest, ErrUnsupportedFileType
}
// Tries to create a new file based on this archetype.
@ -106,11 +110,11 @@ func (h hugo) BeforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r
}
// We only run undraft command if it is a file.
if !strings.HasSuffix(filename, "/") {
args := []string{"undraft", filename}
if err := Run(h.Exe, args, h.Root); err != nil && !strings.Contains(err.Error(), "not a Draft") {
if strings.HasSuffix(filename, ".md") && strings.HasSuffix(filename, ".markdown") {
if err := h.undraft(filename); err != nil {
return http.StatusInternalServerError, err
}
}
// Regenerates the file
@ -135,16 +139,16 @@ func (h hugo) BeforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r
return http.StatusNotFound, nil
}
func (h hugo) AfterAPI(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
func (h Hugo) AfterAPI(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
return 0, nil
}
func (h hugo) JavaScript() string {
func (h Hugo) JavaScript() string {
return rice.MustFindBox("./assets/").MustString("hugo.js")
}
// run runs Hugo with the define arguments.
func (h hugo) run(force bool) {
func (h Hugo) run(force bool) {
// If the CleanPublic option is enabled, clean it.
if h.CleanPublic {
os.RemoveAll(h.Public)
@ -167,7 +171,7 @@ func (h hugo) run(force bool) {
}
// schedule schedules a post to be published later.
func (h hugo) schedule(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
func (h Hugo) schedule(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
t, err := time.Parse("2006-01-02T15:04", r.Header.Get("Schedule"))
path := filepath.Join(string(c.User.FileSystem), r.URL.Path)
path = filepath.Clean(path)
@ -178,8 +182,7 @@ func (h hugo) schedule(c *filemanager.RequestContext, w http.ResponseWriter, r *
scheduler := cron.New()
scheduler.AddFunc(t.Format("05 04 15 02 01 *"), func() {
args := []string{"undraft", path}
if err := Run(h.Exe, args, h.Root); err != nil {
if err := h.undraft(path); err != nil {
log.Printf(err.Error())
return
}
@ -190,3 +193,12 @@ func (h hugo) schedule(c *filemanager.RequestContext, w http.ResponseWriter, r *
scheduler.Start()
return http.StatusOK, nil
}
func (h Hugo) undraft(file string) error {
args := []string{"undraft", file}
if err := Run(h.Exe, args, h.Root); err != nil && !strings.Contains(err.Error(), "not a Draft") {
return err
}
return nil
}

View File

@ -1,4 +1,4 @@
package hugo
package plugins
import (
"errors"

File diff suppressed because one or more lines are too long