Hugo Updates
parent
b61a989958
commit
b863033d7a
File diff suppressed because one or more lines are too long
|
@ -3,7 +3,6 @@ package hugo
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -12,16 +11,12 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hacdias/filemanager"
|
"github.com/hacdias/filemanager"
|
||||||
|
"github.com/hacdias/filemanager/plugins"
|
||||||
"github.com/hacdias/fileutils"
|
"github.com/hacdias/fileutils"
|
||||||
"github.com/mholt/caddy"
|
"github.com/mholt/caddy"
|
||||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
"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.
|
// setup configures a new FileManager middleware instance.
|
||||||
func setup(c *caddy.Controller) error {
|
func setup(c *caddy.Controller) error {
|
||||||
configs, err := parse(c)
|
configs, err := parse(c)
|
||||||
|
@ -120,7 +115,7 @@ func parse(c *caddy.Controller) ([]*filemanager.FileManager, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the default settings for Hugo.
|
// Initialize the default settings for Hugo.
|
||||||
hugo := &hugo{
|
hugo := &plugins.Hugo{
|
||||||
Root: directory,
|
Root: directory,
|
||||||
Public: filepath.Join(directory, "public"),
|
Public: filepath.Join(directory, "public"),
|
||||||
Args: []string{},
|
Args: []string{},
|
||||||
|
@ -129,7 +124,7 @@ func parse(c *caddy.Controller) ([]*filemanager.FileManager, error) {
|
||||||
|
|
||||||
// Try to find the Hugo executable path.
|
// Try to find the Hugo executable path.
|
||||||
if hugo.Exe, err = exec.LookPath("hugo"); err != nil {
|
if hugo.Exe, err = exec.LookPath("hugo"); err != nil {
|
||||||
return nil, errHugoNotFound
|
return nil, plugins.ErrHugoNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
err = m.RegisterPlugin("hugo", hugo)
|
err = m.RegisterPlugin("hugo", hugo)
|
||||||
|
|
|
@ -80,8 +80,6 @@
|
||||||
if: function (data, route) {
|
if: function (data, route) {
|
||||||
return (data.store.state.req.kind === 'editor' &&
|
return (data.store.state.req.kind === 'editor' &&
|
||||||
!data.store.state.loading &&
|
!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.allowEdit &
|
||||||
data.store.state.user.permissions.allowPublish)
|
data.store.state.user.permissions.allowPublish)
|
||||||
},
|
},
|
|
@ -1,6 +1,7 @@
|
||||||
package hugo
|
package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -14,7 +15,13 @@ import (
|
||||||
"github.com/robfig/cron"
|
"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
|
// Website root
|
||||||
Root string `description:"The relative or absolute path to the place where your website is located."`
|
Root string `description:"The relative or absolute path to the place where your website is located."`
|
||||||
// Public folder
|
// Public folder
|
||||||
|
@ -25,11 +32,9 @@ type hugo struct {
|
||||||
Args []string `description:"The arguments to run when running Hugo"`
|
Args []string `description:"The arguments to run when running Hugo"`
|
||||||
// Indicates if we should clean public before a new publish.
|
// 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."`
|
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
|
// If we are using the 'magic url' for the settings, we should redirect the
|
||||||
// request for the acutual path.
|
// request for the acutual path.
|
||||||
if r.URL.Path == "/settings/" || r.URL.Path == "/settings" {
|
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
|
r.URL.Path = "/config." + frontmatter
|
||||||
return 0, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// From here on, we only care about 'hugo' router so we can bypass
|
// 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
|
// If the request isn't for a markdown file, we can't
|
||||||
// handle it.
|
// handle it.
|
||||||
if ext != ".markdown" && ext != ".md" {
|
if ext != ".markdown" && ext != ".md" {
|
||||||
return http.StatusBadRequest, errUnsupportedFileType
|
return http.StatusBadRequest, ErrUnsupportedFileType
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tries to create a new file based on this archetype.
|
// 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.
|
// We only run undraft command if it is a file.
|
||||||
if !strings.HasSuffix(filename, "/") {
|
if strings.HasSuffix(filename, ".md") && strings.HasSuffix(filename, ".markdown") {
|
||||||
args := []string{"undraft", filename}
|
if err := h.undraft(filename); err != nil {
|
||||||
if err := Run(h.Exe, args, h.Root); err != nil && !strings.Contains(err.Error(), "not a Draft") {
|
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regenerates the file
|
// Regenerates the file
|
||||||
|
@ -135,16 +139,16 @@ func (h hugo) BeforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r
|
||||||
return http.StatusNotFound, nil
|
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
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h hugo) JavaScript() string {
|
func (h Hugo) JavaScript() string {
|
||||||
return rice.MustFindBox("./assets/").MustString("hugo.js")
|
return rice.MustFindBox("./assets/").MustString("hugo.js")
|
||||||
}
|
}
|
||||||
|
|
||||||
// run runs Hugo with the define arguments.
|
// 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 the CleanPublic option is enabled, clean it.
|
||||||
if h.CleanPublic {
|
if h.CleanPublic {
|
||||||
os.RemoveAll(h.Public)
|
os.RemoveAll(h.Public)
|
||||||
|
@ -167,7 +171,7 @@ func (h hugo) run(force bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// schedule schedules a post to be published later.
|
// 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"))
|
t, err := time.Parse("2006-01-02T15:04", r.Header.Get("Schedule"))
|
||||||
path := filepath.Join(string(c.User.FileSystem), r.URL.Path)
|
path := filepath.Join(string(c.User.FileSystem), r.URL.Path)
|
||||||
path = filepath.Clean(path)
|
path = filepath.Clean(path)
|
||||||
|
@ -178,8 +182,7 @@ func (h hugo) schedule(c *filemanager.RequestContext, w http.ResponseWriter, r *
|
||||||
|
|
||||||
scheduler := cron.New()
|
scheduler := cron.New()
|
||||||
scheduler.AddFunc(t.Format("05 04 15 02 01 *"), func() {
|
scheduler.AddFunc(t.Format("05 04 15 02 01 *"), func() {
|
||||||
args := []string{"undraft", path}
|
if err := h.undraft(path); err != nil {
|
||||||
if err := Run(h.Exe, args, h.Root); err != nil {
|
|
||||||
log.Printf(err.Error())
|
log.Printf(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -190,3 +193,12 @@ func (h hugo) schedule(c *filemanager.RequestContext, w http.ResponseWriter, r *
|
||||||
scheduler.Start()
|
scheduler.Start()
|
||||||
return http.StatusOK, nil
|
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
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package hugo
|
package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
2406
rice-box.go
2406
rice-box.go
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue