filebrowser/utils/utils.go

227 lines
4.9 KiB
Go
Raw Normal View History

2015-09-14 09:46:31 +00:00
package utils
import (
"errors"
2015-09-27 19:50:17 +00:00
"fmt"
2015-09-20 19:42:22 +00:00
"io"
2015-09-18 12:52:10 +00:00
"log"
"net/http"
2015-09-20 19:42:22 +00:00
"os"
2015-09-27 12:20:05 +00:00
"os/exec"
2015-09-14 09:46:31 +00:00
"reflect"
"strings"
2015-09-18 12:52:10 +00:00
"text/template"
2015-09-14 09:46:31 +00:00
"unicode"
2015-09-18 12:52:10 +00:00
2015-09-27 19:01:44 +00:00
"github.com/hacdias/caddy-cms/assets"
"github.com/hacdias/caddy-cms/config"
2015-09-20 21:00:25 +00:00
"github.com/spf13/hugo/commands"
2015-09-14 09:46:31 +00:00
)
2015-09-20 21:20:37 +00:00
// CanBeEdited checks if a filename has a supported extension
func CanBeEdited(filename string) bool {
extensions := [...]string{".markdown", ".md",
".json", ".toml", ".yaml",
".css", ".sass", ".scss",
".js",
".html",
}
for _, extension := range extensions {
if strings.HasSuffix(filename, extension) {
return true
}
}
return false
2015-09-20 21:00:25 +00:00
}
2015-09-20 19:42:22 +00:00
// 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
}
2015-09-20 21:20:37 +00:00
// Defined checks if variable is defined in a struct
func Defined(data interface{}, field string) bool {
t := reflect.Indirect(reflect.ValueOf(data)).Type()
if t.Kind() != reflect.Struct {
log.Print("Non-struct type not allowed.")
return false
2015-09-20 08:15:21 +00:00
}
2015-09-20 21:20:37 +00:00
_, b := t.FieldByName(field)
return b
}
// Dict allows to send more than one variable into a template
func Dict(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
2015-09-20 08:15:21 +00:00
}
2015-09-20 21:20:37 +00:00
dict[key] = values[i+1]
2015-09-20 08:15:21 +00:00
}
2015-09-20 21:20:37 +00:00
return dict, nil
2015-09-20 08:15:21 +00:00
}
2015-09-18 12:52:10 +00:00
// GetTemplate 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) {
// If this is a pjax request, use the minimal template to send only
// the main content
if r.Header.Get("X-PJAX") == "true" {
templates = append(templates, "base_minimal")
} else {
templates = append(templates, "base_full")
}
var tpl *template.Template
// For each template, add it to the the tpl variable
for i, t := range templates {
// Get the template from the assets
page, err := assets.Asset("templates/" + t + ".tmpl")
// Check if there is some error. If so, the template doesn't exist
if err != nil {
log.Print(err)
return new(template.Template), err
}
// If it's the first iteration, creates a new template and add the
// functions map
if i == 0 {
tpl, err = template.New(t).Funcs(functions).Parse(string(page))
} else {
tpl, err = tpl.Parse(string(page))
}
if err != nil {
log.Print(err)
return new(template.Template), err
}
}
return tpl, nil
}
2015-09-18 08:47:02 +00:00
// IsMap checks if some variable is a map
2015-09-14 09:46:31 +00:00
func IsMap(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Map
}
2015-09-18 08:47:02 +00:00
// IsSlice checks if some variable is a slice
2015-09-15 10:59:48 +00:00
func IsSlice(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Slice
}
2015-09-20 21:20:37 +00:00
// 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
}
2015-09-27 12:20:05 +00:00
// Run is used to run the static website generator
func Run(c *config.Config) {
2015-09-30 21:03:28 +00:00
cwd, err := os.Getwd()
if err != nil {
log.Print("Can't get working directory.")
}
err = os.Chdir(c.Path)
if err != nil {
log.Print("Can't get working directory.")
}
if !c.Hugo {
out, err := exec.Command(c.Command).Output()
2015-09-27 19:50:17 +00:00
fmt.Print(string(out))
2015-09-27 12:20:05 +00:00
if err != nil {
log.Panic("Can't execute the commands defined on Caddyfile.")
}
return
}
2015-09-30 21:03:28 +00:00
args := strings.Split(c.Command, " ")
for index, element := range args {
args[index] = strings.Replace(element, "\"", "", -1)
}
commands.HugoCmd.ParseFlags(args)
2015-09-20 21:20:37 +00:00
commands.HugoCmd.Run(commands.HugoCmd, make([]string, 0))
2015-09-30 21:03:28 +00:00
err = os.Chdir(cwd)
if err != nil {
log.Print("Can't get working directory.")
}
2015-09-20 21:20:37 +00:00
}
2015-09-18 08:47:02 +00:00
// SplitCapitalize splits a string by its uppercase letters and capitalize the
// first letter of the string
2015-09-14 09:46:31 +00:00
func SplitCapitalize(name string) string {
var words []string
l := 0
for s := name; s != ""; s = s[l:] {
l = strings.IndexFunc(s[1:], unicode.IsUpper) + 1
if l <= 0 {
l = len(s)
}
words = append(words, s[:l])
}
name = ""
for _, element := range words {
name += element + " "
}
name = strings.ToLower(name[:len(name)-1])
2015-09-26 20:39:27 +00:00
name = strings.ToUpper(string(name[0])) + name[1:]
2015-09-14 09:46:31 +00:00
return name
}