update file structure [ci skip]
This commit is contained in:
142
tools/templates/templates.go
Normal file
142
tools/templates/templates.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/hacdias/caddy-hugo/assets"
|
||||
)
|
||||
|
||||
// CanBeEdited checks if the extension of a file is supported by the editor
|
||||
func CanBeEdited(filename string) bool {
|
||||
extensions := [...]string{
|
||||
"md", "markdown", "mdown", "mmark",
|
||||
"asciidoc", "adoc", "ad",
|
||||
"rst",
|
||||
".json", ".toml", ".yaml",
|
||||
".css", ".sass", ".scss",
|
||||
".js",
|
||||
".html",
|
||||
".txt",
|
||||
}
|
||||
|
||||
for _, extension := range extensions {
|
||||
if strings.HasSuffix(filename, extension) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
_, 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")
|
||||
}
|
||||
dict[key] = values[i+1]
|
||||
}
|
||||
|
||||
return dict, nil
|
||||
}
|
||||
|
||||
// Get is used to get a ready to use template based on the url and on
|
||||
// other sent templates
|
||||
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" {
|
||||
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
|
||||
}
|
||||
|
||||
var splitCapitalizeExceptions = map[string]string{
|
||||
"youtube": "YouTube",
|
||||
"github": "GitHub",
|
||||
"googleplus": "Google Plus",
|
||||
"linkedin": "LinkedIn",
|
||||
}
|
||||
|
||||
// SplitCapitalize splits a string by its uppercase letters and capitalize the
|
||||
// first letter of the string
|
||||
func SplitCapitalize(name string) string {
|
||||
if val, ok := splitCapitalizeExceptions[strings.ToLower(name)]; ok {
|
||||
return val
|
||||
}
|
||||
|
||||
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])
|
||||
name = strings.ToUpper(string(name[0])) + name[1:]
|
||||
|
||||
return name
|
||||
}
|
||||
105
tools/templates/templates_test.go
Normal file
105
tools/templates/templates_test.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package templates
|
||||
|
||||
import "testing"
|
||||
|
||||
type canBeEdited struct {
|
||||
file string
|
||||
result bool
|
||||
}
|
||||
|
||||
var canBeEditedPairs = []canBeEdited{
|
||||
{"file.markdown", true},
|
||||
{"file.md", true},
|
||||
{"file.json", true},
|
||||
{"file.toml", true},
|
||||
{"file.yaml", true},
|
||||
{"file.css", true},
|
||||
{"file.sass", true},
|
||||
{"file.scss", true},
|
||||
{"file.js", true},
|
||||
{"file.html", true},
|
||||
{"file.git", false},
|
||||
{"file.log", false},
|
||||
{"file.sh", false},
|
||||
{"file.png", false},
|
||||
{"file.jpg", false},
|
||||
}
|
||||
|
||||
func TestCanBeEdited(t *testing.T) {
|
||||
for _, pair := range canBeEditedPairs {
|
||||
v := CanBeEdited(pair.file)
|
||||
if v != pair.result {
|
||||
t.Error(
|
||||
"For", pair.file,
|
||||
"expected", pair.result,
|
||||
"got", v,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type testDefinedData struct {
|
||||
f1 string
|
||||
f2 bool
|
||||
f3 int
|
||||
f4 func()
|
||||
}
|
||||
|
||||
type testDefined struct {
|
||||
data interface{}
|
||||
field string
|
||||
result bool
|
||||
}
|
||||
|
||||
var testDefinedCases = []testDefined{
|
||||
{testDefinedData{}, "f1", true},
|
||||
{testDefinedData{}, "f2", true},
|
||||
{testDefinedData{}, "f3", true},
|
||||
{testDefinedData{}, "f4", true},
|
||||
{testDefinedData{}, "f5", false},
|
||||
{[]string{}, "", false},
|
||||
{map[string]int{"oi": 4}, "", false},
|
||||
{"asa", "", false},
|
||||
{"int", "", false},
|
||||
}
|
||||
|
||||
func TestDefined(t *testing.T) {
|
||||
for _, pair := range testDefinedCases {
|
||||
v := Defined(pair.data, pair.field)
|
||||
if v != pair.result {
|
||||
t.Error(
|
||||
"For", pair.data,
|
||||
"expected", pair.result,
|
||||
"got", v,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type testSplitCapitalize struct {
|
||||
name string
|
||||
result string
|
||||
}
|
||||
|
||||
var testSplitCapitalizeCases = []testSplitCapitalize{
|
||||
{"loremIpsum", "Lorem ipsum"},
|
||||
{"LoremIpsum", "Lorem ipsum"},
|
||||
{"loremipsum", "Loremipsum"},
|
||||
{"YouTube", "YouTube"},
|
||||
{"GitHub", "GitHub"},
|
||||
{"GooglePlus", "Google Plus"},
|
||||
{"Facebook", "Facebook"},
|
||||
}
|
||||
|
||||
func TestSplitCapitalize(t *testing.T) {
|
||||
for _, pair := range testSplitCapitalizeCases {
|
||||
v := SplitCapitalize(pair.name)
|
||||
if v != pair.result {
|
||||
t.Error(
|
||||
"For", pair.name,
|
||||
"expected", pair.result,
|
||||
"got", v,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user