update file structure [ci skip]

pull/68/head
Henrique Dias 2016-03-06 19:18:46 +00:00
parent ddcaccda0c
commit 3f36200862
23 changed files with 180 additions and 165 deletions

View File

@ -5,7 +5,7 @@ import (
"path/filepath"
"strings"
"github.com/hacdias/caddy-hugo/hugo"
"github.com/hacdias/caddy-hugo/tools/hugo"
"github.com/mholt/caddy/caddy/setup"
)

13
hugo.go
View File

@ -15,11 +15,12 @@ import (
"strings"
"github.com/hacdias/caddy-hugo/assets"
"github.com/hacdias/caddy-hugo/browse"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/editor"
"github.com/hacdias/caddy-hugo/git"
"github.com/hacdias/caddy-hugo/utils"
"github.com/hacdias/caddy-hugo/routes/browse"
"github.com/hacdias/caddy-hugo/routes/editor"
"github.com/hacdias/caddy-hugo/routes/git"
"github.com/hacdias/caddy-hugo/tools/commands"
"github.com/hacdias/caddy-hugo/tools/hugo"
"github.com/mholt/caddy/caddy/setup"
"github.com/mholt/caddy/middleware"
)
@ -46,14 +47,14 @@ func Setup(c *setup.Controller) (middleware.Middleware, error) {
}
if create {
err := utils.RunCommand(config.Hugo, []string{"new", "site", config.Path, "--force"}, ".")
err := commands.Run(config.Hugo, []string{"new", "site", config.Path, "--force"}, ".")
if err != nil {
log.Panic(err)
}
}
// Generates the Hugo website for the first time the plugin is activated.
go utils.Run(config, true)
go hugo.Run(config, true)
return func(next middleware.Handler) middleware.Handler {
return &CaddyHugo{Next: next, Config: config}

View File

@ -6,7 +6,7 @@ import (
"strings"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/utils"
"github.com/hacdias/caddy-hugo/tools/utils"
)
// DELETE handles the delete requests on browse pages

View File

@ -5,7 +5,7 @@ import (
"text/template"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/utils"
"github.com/hacdias/caddy-hugo/tools/templates"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/browse"
)
@ -14,11 +14,11 @@ import (
// the Browse Caddy middleware.
func GET(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
functions := template.FuncMap{
"CanBeEdited": utils.CanBeEdited,
"Defined": utils.Defined,
"CanBeEdited": templates.CanBeEdited,
"Defined": templates.Defined,
}
tpl, err := utils.GetTemplate(r, functions, "browse")
tpl, err := templates.Get(r, functions, "browse")
if err != nil {
return http.StatusInternalServerError, err

View File

@ -11,7 +11,8 @@ import (
"strings"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/utils"
"github.com/hacdias/caddy-hugo/tools/commands"
"github.com/hacdias/caddy-hugo/tools/utils"
)
// POST handles the POST method on browse page. It's used to create new files,
@ -65,7 +66,7 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
args = append(args, "--kind", archetype)
}
if err := utils.RunCommand(c.Hugo, args, c.Path); err != nil {
if err := commands.Run(c.Hugo, args, c.Path); err != nil {
return utils.RespondJSON(w, map[string]string{
"message": "Something went wrong.",
}, 500, err)

View File

@ -8,7 +8,7 @@ import (
"strings"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/utils"
"github.com/hacdias/caddy-hugo/tools/utils"
)
// PUT handles the HTTP PUT request for all /admin/browse related requests.

View File

@ -11,8 +11,8 @@ import (
"text/template"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/frontmatter"
"github.com/hacdias/caddy-hugo/utils"
"github.com/hacdias/caddy-hugo/tools/frontmatter"
"github.com/hacdias/caddy-hugo/tools/templates"
"github.com/spf13/hugo/parser"
)
@ -30,7 +30,7 @@ type editor struct {
func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
// Check if the file format is supported. If not, send a "Not Acceptable"
// header and an error
if !utils.CanBeEdited(filename) {
if !templates.CanBeEdited(filename) {
return http.StatusNotAcceptable, errors.New("File format not supported.")
}
@ -110,11 +110,11 @@ func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename stri
// Create the functions map, then the template, check for erros and
// execute the template if there aren't errors
functions := template.FuncMap{
"SplitCapitalize": utils.SplitCapitalize,
"Defined": utils.Defined,
"SplitCapitalize": templates.SplitCapitalize,
"Defined": templates.Defined,
}
tpl, err := utils.GetTemplate(r, functions, "editor", "frontmatter")
tpl, err := templates.Get(r, functions, "editor", "frontmatter")
if err != nil {
return http.StatusInternalServerError, err

View File

@ -11,7 +11,7 @@ import (
"time"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/utils"
"github.com/hacdias/caddy-hugo/tools/commands"
"github.com/robfig/cron"
"github.com/spf13/cast"
"github.com/spf13/hugo/parser"
@ -144,7 +144,7 @@ func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]int
return
}
go utils.Run(c, false)
go commands.Run(c, false)
})
scheduler.Start()
}

View File

@ -8,7 +8,7 @@ import (
"strings"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/utils"
"github.com/hacdias/caddy-hugo/tools/utils"
)
// POST handles the POST method on GIT page which is only an API.

View File

@ -0,0 +1,15 @@
package commands
import (
"os"
"os/exec"
)
// RunCommand executes an external command
func Run(command string, args []string, path string) error {
cmd := exec.Command(command, args...)
cmd.Dir = path
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
return cmd.Run()
}

30
tools/files/files.go Normal file
View File

@ -0,0 +1,30 @@
package files
import (
"io"
"os"
)
// CopyFile is used to copy a file
func CopyFile(old, new string) error {
// Open the file and create a new one
r, err := os.Open(old)
if err != nil {
return err
}
defer r.Close()
w, err := os.Create(new)
if err != nil {
return err
}
defer w.Close()
// Copy the content
_, err = io.Copy(w, r)
if err != nil {
return err
}
return nil
}

11
tools/files/files_test.go Normal file
View File

@ -0,0 +1,11 @@
package files
import "testing"
func TestCopyFile(t *testing.T) {
err := CopyFile("test_data/file_to_copy.txt", "test_data/copied_file.txt")
if err != nil {
t.Error("Can't copy the file.")
}
}

View File

@ -6,7 +6,7 @@ import (
"sort"
"strings"
"github.com/hacdias/caddy-hugo/utils"
"github.com/hacdias/caddy-hugo/tools/types"
"github.com/spf13/cast"
"github.com/spf13/hugo/parser"
)
@ -60,9 +60,9 @@ func rawToPretty(config interface{}, parent *frontmatter) interface{} {
}
for name, element := range cnf {
if utils.IsMap(element) {
if types.IsMap(element) {
objects = append(objects, handleObjects(element, parent, name))
} else if utils.IsSlice(element) {
} else if types.IsSlice(element) {
arrays = append(arrays, handleArrays(element, parent, name))
} else {
if name == "title" && parent.Name == mainName {

View File

@ -13,6 +13,7 @@ import (
"path/filepath"
"runtime"
"github.com/hacdias/caddy-hugo/config"
"github.com/mitchellh/go-homedir"
"github.com/pivotal-golang/archiver/extractor"
)
@ -44,6 +45,35 @@ var (
}
)
// Run is used to run the static website generator
func Run(c *config.Config, force bool) {
os.RemoveAll(c.Path + "public")
// Prevent running if watching is enabled
if b, pos := stringInSlice("--watch", c.Args); b && !force {
if len(c.Args) > pos && c.Args[pos+1] != "false" {
return
}
if len(c.Args) == pos+1 {
return
}
}
if err := RunCommand(c.Hugo, c.Args, c.Path); err != nil {
log.Panic(err)
}
}
func stringInSlice(a string, list []string) (bool, int) {
for i, b := range list {
if b == a {
return true, i
}
}
return false, 0
}
// GetPath retrives the Hugo path for the user or install it if it's not found
func GetPath() string {
initializeVariables()

View File

@ -1,20 +1,15 @@
package utils
package templates
import (
"encoding/json"
"errors"
"io"
"html/template"
"log"
"net/http"
"os"
"os/exec"
"reflect"
"strings"
"text/template"
"unicode"
"github.com/hacdias/caddy-hugo/assets"
"github.com/hacdias/caddy-hugo/config"
)
// CanBeEdited checks if the extension of a file is supported by the editor
@ -39,30 +34,6 @@ func CanBeEdited(filename string) bool {
return false
}
// CopyFile is used to copy a file
func CopyFile(old, new string) error {
// Open the file and create a new one
r, err := os.Open(old)
if err != nil {
return err
}
defer r.Close()
w, err := os.Create(new)
if err != nil {
return err
}
defer w.Close()
// Copy the content
_, err = io.Copy(w, r)
if err != nil {
return err
}
return nil
}
// Defined checks if variable is defined in a struct
func Defined(data interface{}, field string) bool {
t := reflect.Indirect(reflect.ValueOf(data)).Type()
@ -93,9 +64,9 @@ func Dict(values ...interface{}) (map[string]interface{}, error) {
return dict, nil
}
// GetTemplate is used to get a ready to use template based on the url and on
// Get is used to get a ready to use template based on the url and on
// other sent templates
func GetTemplate(r *http.Request, functions template.FuncMap, templates ...string) (*template.Template, error) {
func Get(r *http.Request, functions template.FuncMap, templates ...string) (*template.Template, error) {
// If this is a pjax request, use the minimal template to send only
// the main content
if r.Header.Get("X-PJAX") == "true" {
@ -134,74 +105,6 @@ func GetTemplate(r *http.Request, functions template.FuncMap, templates ...strin
return tpl, nil
}
// IsMap checks if some variable is a map
func IsMap(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Map
}
// IsSlice checks if some variable is a slice
func IsSlice(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Slice
}
// ParseComponents parses the components of an URL creating an array
func ParseComponents(r *http.Request) []string {
//The URL that the user queried.
path := r.URL.Path
path = strings.TrimSpace(path)
//Cut off the leading and trailing forward slashes, if they exist.
//This cuts off the leading forward slash.
if strings.HasPrefix(path, "/") {
path = path[1:]
}
//This cuts off the trailing forward slash.
if strings.HasSuffix(path, "/") {
cutOffLastCharLen := len(path) - 1
path = path[:cutOffLastCharLen]
}
//We need to isolate the individual components of the path.
components := strings.Split(path, "/")
return components
}
// Run is used to run the static website generator
func Run(c *config.Config, force bool) {
os.RemoveAll(c.Path + "public")
// Prevent running if watching is enabled
if b, pos := stringInSlice("--watch", c.Args); b && !force {
if len(c.Args) > pos && c.Args[pos+1] != "false" {
return
}
if len(c.Args) == pos+1 {
return
}
}
if err := RunCommand(c.Hugo, c.Args, c.Path); err != nil {
log.Panic(err)
}
}
// RunCommand executes an external command
func RunCommand(command string, args []string, path string) error {
cmd := exec.Command(command, args...)
cmd.Dir = path
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
return cmd.Run()
}
func stringInSlice(a string, list []string) (bool, int) {
for i, b := range list {
if b == a {
return true, i
}
}
return false, 0
}
var splitCapitalizeExceptions = map[string]string{
"youtube": "YouTube",
"github": "GitHub",
@ -237,20 +140,3 @@ func SplitCapitalize(name string) string {
return name
}
func RespondJSON(w http.ResponseWriter, message map[string]string, code int, err error) (int, error) {
msg, msgErr := json.Marshal(message)
if msgErr != nil {
return 500, msgErr
}
if code == 500 && err != nil {
err = errors.New(message["message"])
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(msg)
return 0, err
}

View File

@ -1,4 +1,4 @@
package utils
package templates
import "testing"
@ -38,14 +38,6 @@ func TestCanBeEdited(t *testing.T) {
}
}
func TestCopyFile(t *testing.T) {
err := CopyFile("test_data/file_to_copy.txt", "test_data/copied_file.txt")
if err != nil {
t.Error("Can't copy the file.")
}
}
type testDefinedData struct {
f1 string
f2 bool

13
tools/types/types.go Normal file
View File

@ -0,0 +1,13 @@
package types
import "reflect"
// IsMap checks if some variable is a map
func IsMap(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Map
}
// IsSlice checks if some variable is a slice
func IsSlice(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Slice
}

45
tools/utils/utils.go Normal file
View File

@ -0,0 +1,45 @@
package utils
import (
"encoding/json"
"errors"
"net/http"
"strings"
)
// ParseComponents parses the components of an URL creating an array
func ParseComponents(r *http.Request) []string {
//The URL that the user queried.
path := r.URL.Path
path = strings.TrimSpace(path)
//Cut off the leading and trailing forward slashes, if they exist.
//This cuts off the leading forward slash.
if strings.HasPrefix(path, "/") {
path = path[1:]
}
//This cuts off the trailing forward slash.
if strings.HasSuffix(path, "/") {
cutOffLastCharLen := len(path) - 1
path = path[:cutOffLastCharLen]
}
//We need to isolate the individual components of the path.
components := strings.Split(path, "/")
return components
}
func RespondJSON(w http.ResponseWriter, message map[string]string, code int, err error) (int, error) {
msg, msgErr := json.Marshal(message)
if msgErr != nil {
return 500, msgErr
}
if code == 500 && err != nil {
err = errors.New(message["message"])
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(msg)
return 0, err
}

View File

@ -1,9 +0,0 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin faucibus augue vel ex auctor molestie. Sed id lorem quis velit eleifend vestibulum. Etiam tincidunt nulla et metus dictum imperdiet. Suspendisse ac auctor risus. Donec tempus eros quis erat interdum fringilla. Duis ac tristique lorem, quis vulputate enim. In ut est elementum, dignissim tellus quis, ultrices diam. Aenean in efficitur velit, ut imperdiet felis. Suspendisse aliquet accumsan ligula, a iaculis sem luctus vel. Integer sagittis, orci viverra lacinia efficitur, eros magna sodales sapien, in tristique ante arcu ut nunc. Maecenas odio dolor, semper quis vehicula ut, malesuada sollicitudin massa. Integer dolor ligula, hendrerit convallis quam at, luctus dignissim nibh. Fusce interdum congue justo, at imperdiet lorem maximus in.
Curabitur dignissim id turpis ut eleifend. Pellentesque vehicula et purus et consectetur. Nulla facilisis vehicula nunc in imperdiet. Nam eget porta libero, vel dapibus leo. Maecenas iaculis, arcu quis vehicula sollicitudin, massa tortor mattis tortor, vitae venenatis erat neque et dui. Mauris eleifend vel urna ac interdum. Sed convallis, arcu et scelerisque suscipit, nulla urna laoreet mauris, quis aliquam dolor elit id mauris. Pellentesque ac pulvinar nibh. Duis vestibulum ligula orci, et mattis turpis facilisis mattis. Vivamus feugiat neque sed aliquet aliquet. Sed consequat augue sagittis eros imperdiet, ac lacinia erat tempor. Cras gravida mi a orci euismod feugiat. Mauris condimentum turpis id quam vulputate, et maximus dolor hendrerit.
Ut nisi urna, sollicitudin ac facilisis eu, rhoncus sed urna. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nulla eu maximus mi, sed commodo diam. Nunc scelerisque tincidunt ipsum eget mattis. Fusce id finibus nisi, sit amet tempus tortor. Fusce accumsan lectus at dignissim volutpat. Nullam nisl nulla, rhoncus id arcu vitae, eleifend posuere libero. Aliquam dictum bibendum bibendum. Integer non mattis neque. Phasellus fermentum et ex vel vehicula.
Proin eget ligula lorem. Ut vehicula quis augue sed aliquet. Nulla viverra turpis nulla. Morbi et ipsum odio. Nulla in nisi suscipit justo blandit tristique ac vitae ipsum. Donec viverra dictum arcu. Duis est elit, ultrices a massa non, lobortis maximus eros. Nulla porttitor rhoncus lectus in finibus. Duis convallis sapien porta, fringilla orci nec, ultricies nisl. Morbi pulvinar quam in nulla rutrum euismod.
Donec et accumsan lectus, consectetur posuere nunc. Quisque et quam hendrerit, vestibulum quam eget, luctus enim. In hac habitasse platea dictumst. Nunc et est scelerisque, bibendum neque quis, ornare elit. Aliquam erat volutpat. Nulla maximus nibh vitae ex interdum, a efficitur lacus eleifend. Nam vestibulum blandit aliquet. In hac habitasse platea dictumst. Maecenas interdum quam et lorem posuere interdum. Mauris id feugiat augue. Etiam vestibulum pharetra cursus.