improvements

This commit is contained in:
Henrique Dias 2015-09-18 13:52:10 +01:00
parent abb9c4efe1
commit f7227520ea
14 changed files with 125 additions and 142 deletions

File diff suppressed because one or more lines are too long

View File

@ -2856,7 +2856,7 @@ nav ul li a:hover {
background-color: rgba(255, 255, 255, 0.57);
}
.main {
#main {
position: fixed;
top: 3em;
left: 0;

View File

@ -63,7 +63,7 @@ nav ul li a:hover {
background-color: rgba(255, 255, 255, 0.57);
}
.main {
#main {
position: fixed;
top : 3em;
left : 0;

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
$(document).ready(function() {
$(document).pjax('a', '#container');
$(document).pjax('a', '#main');
});
$(document).on('ready pjax:success', function() {

View File

@ -4,8 +4,10 @@ import (
"log"
"net/http"
"strings"
"text/template"
"github.com/hacdias/caddy-hugo/page"
"github.com/hacdias/caddy-hugo/edit"
"github.com/hacdias/caddy-hugo/utils"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/browse"
)
@ -23,7 +25,11 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
r.URL.Path = "/"
}
tpl, err := page.GetTemplate(r, "browse")
functions := template.FuncMap{
"canBeEdited": edit.CanBeEdited,
}
tpl, err := utils.GetTemplate(r, functions, "browse")
if err != nil {
log.Print(err)

View File

@ -8,13 +8,14 @@ import (
"net/http"
"os"
"strings"
"text/template"
"github.com/hacdias/caddy-hugo/frontmatter"
"github.com/hacdias/caddy-hugo/page"
"github.com/hacdias/caddy-hugo/utils"
"github.com/spf13/hugo/parser"
)
type information struct {
type editor struct {
Name string
Content string
FrontMatter interface{}
@ -81,7 +82,7 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
file, err := parser.ReadFrom(reader)
inf := new(information)
inf := new(editor)
inf.Content = strings.TrimSpace(string(file.Content()))
inf.FrontMatter, err = frontmatter.Pretty(file.FrontMatter())
@ -90,12 +91,37 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
return 500, err
}
page := new(page.Page)
page.Name = "Editor"
page.Class = "editor"
page.Body = inf
return page.Render(w, r, "edit", "frontmatter")
functions := template.FuncMap{
"splitCapitalize": utils.SplitCapitalize,
}
tpl, err := utils.GetTemplate(r, functions, "edit", "frontmatter")
if err != nil {
log.Print(err)
return 500, err
}
tpl.Execute(w, inf)
}
return 200, nil
}
// CanBeEdited checks if a file 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
}

View File

@ -1,80 +0,0 @@
package page
import (
"log"
"net/http"
"text/template"
"github.com/hacdias/caddy-hugo/assets"
"github.com/hacdias/caddy-hugo/utils"
)
const (
templateExtension = ".tmpl"
)
var funcMap = template.FuncMap{
"splitCapitalize": utils.SplitCapitalize,
"isMarkdown": utils.IsMarkdownFile,
}
// Page type
type Page struct {
Name string
Class string
Body interface{}
}
// Render the page
func (p *Page) Render(w http.ResponseWriter, r *http.Request, templates ...string) (int, error) {
tpl, err := GetTemplate(r, templates...)
if err != nil {
log.Print(err)
return 500, err
}
tpl.Execute(w, p)
return 200, nil
}
// GetTemplate is used to get a ready to use template based on the url and on
// other sent templates
func GetTemplate(r *http.Request, 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 + templateExtension)
// 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(funcMap).Parse(string(page))
} else {
tpl, err = tpl.Parse(string(page))
}
if err != nil {
log.Print(err)
return new(template.Template), err
}
}
return tpl, nil
}

View File

@ -7,11 +7,17 @@ import (
"log"
"net/http"
"os"
"text/template"
"github.com/hacdias/caddy-hugo/frontmatter"
"github.com/hacdias/caddy-hugo/page"
"github.com/hacdias/caddy-hugo/utils"
)
type page struct {
Name string
Settings interface{}
}
// Execute the page
func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
language := getConfigFrontMatter()
@ -60,11 +66,22 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
return 500, err
}
page := new(page.Page)
page.Name = "Settings"
page.Class = "settings"
page.Body = f
return page.Render(w, r, "settings", "frontmatter")
functions := template.FuncMap{
"splitCapitalize": utils.SplitCapitalize,
}
tpl, err := utils.GetTemplate(r, functions, "settings", "frontmatter")
if err != nil {
log.Print(err)
return 500, err
}
p := new(page)
p.Name = "settings"
p.Settings = f
tpl.Execute(w, p)
}
return 200, nil
}

View File

@ -22,7 +22,7 @@
<li><a id="logout" href="#logout"><i class="fa fa-sign-out"></i> Logout</a></li>
</ul>
</nav>
<div class="main" id="container">
<div id="main">
{{ template "content" . }}
</div>
</body>

View File

@ -52,7 +52,7 @@
{{if .IsDir}}
<i class="fa fa-folder"></i> <a href="{{.URL}}">{{.Name}}</a>
{{else}}
{{ if isMarkdown .URL }}
{{ if canBeEdited .URL }}
<i class="fa fa-file"></i> <a class="file" href="/admin/edit{{ $path }}{{.URL}}">{{.Name}}</a>
{{ else }}
<i class="fa fa-file"></i> {{.Name}}

View File

@ -1,17 +1,18 @@
{{ define "content" }} {{ with .Body }}
{{ define "content" }}
<main class="editor">
<form method="POST" action="">
<div class="sidebar scroll data">
<h2>Metadata</h2>
{{ template "frontmatter" .FrontMatter }}
</div>
<div class="container data">
<textarea id="content-area" name="content" class="scroll">{{ .Content }}</textarea>
<div id="preview-area" class="scroll hidden"></div>
</div>
<div class="sidebar scroll data">
<h2>Metadata</h2>
{{ template "frontmatter" .FrontMatter }}
</div>
<div class="action-bar">
<button id="preview" class="left">Preview</button>
<input type="submit" data-message="Post saved." data-regenerate="false" value="Save">
@ -20,4 +21,4 @@
</form>
</main>
{{ end }} {{ end }}
{{ end }}

View File

@ -6,14 +6,12 @@
</header>
<main>
{{ with .Body }}
<div class="content">
<form method="POST" action="/admin/settings">
{{ template "frontmatter" . }}
{{ template "frontmatter" .Settings }}
<input type="submit" data-message="Settings updated." data-regenerate="true" value="Save">
</form>
</div>
{{ end }}
</main>
{{ end }}

View File

@ -2,12 +2,57 @@ package utils
import (
"errors"
"log"
"net/http"
"reflect"
"strings"
"text/template"
"unicode"
"github.com/hacdias/caddy-hugo/assets"
)
// 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
}
// Dict allows to send more than one variable into a template
func Dict(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
@ -34,36 +79,6 @@ func IsSlice(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Slice
}
// IsArray checks if some variable is an array
func IsArray(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Array
}
// IsString checks if some variable is a string
func IsString(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.String
}
// IsInt checks if some variable is an integer
func IsInt(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Int
}
// IsBool checks if some variable is a boolean
func IsBool(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Bool
}
// IsInterface checks if some variable is an interface
func IsInterface(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Interface
}
// IsMarkdownFile checks if a filename belongs to a markdown file
func IsMarkdownFile(filename string) bool {
return strings.HasSuffix(filename, ".markdown") || strings.HasSuffix(filename, ".md")
}
// SplitCapitalize splits a string by its uppercase letters and capitalize the
// first letter of the string
func SplitCapitalize(name string) string {