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); background-color: rgba(255, 255, 255, 0.57);
} }
.main { #main {
position: fixed; position: fixed;
top: 3em; top: 3em;
left: 0; left: 0;

View File

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

File diff suppressed because one or more lines are too long

View File

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

View File

@ -4,8 +4,10 @@ import (
"log" "log"
"net/http" "net/http"
"strings" "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"
"github.com/mholt/caddy/middleware/browse" "github.com/mholt/caddy/middleware/browse"
) )
@ -23,7 +25,11 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
r.URL.Path = "/" 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 { if err != nil {
log.Print(err) log.Print(err)

View File

@ -8,13 +8,14 @@ import (
"net/http" "net/http"
"os" "os"
"strings" "strings"
"text/template"
"github.com/hacdias/caddy-hugo/frontmatter" "github.com/hacdias/caddy-hugo/frontmatter"
"github.com/hacdias/caddy-hugo/page" "github.com/hacdias/caddy-hugo/utils"
"github.com/spf13/hugo/parser" "github.com/spf13/hugo/parser"
) )
type information struct { type editor struct {
Name string Name string
Content string Content string
FrontMatter interface{} FrontMatter interface{}
@ -81,7 +82,7 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
file, err := parser.ReadFrom(reader) file, err := parser.ReadFrom(reader)
inf := new(information) inf := new(editor)
inf.Content = strings.TrimSpace(string(file.Content())) inf.Content = strings.TrimSpace(string(file.Content()))
inf.FrontMatter, err = frontmatter.Pretty(file.FrontMatter()) inf.FrontMatter, err = frontmatter.Pretty(file.FrontMatter())
@ -90,12 +91,37 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
return 500, err return 500, err
} }
page := new(page.Page) functions := template.FuncMap{
page.Name = "Editor" "splitCapitalize": utils.SplitCapitalize,
page.Class = "editor" }
page.Body = inf
return page.Render(w, r, "edit", "frontmatter") tpl, err := utils.GetTemplate(r, functions, "edit", "frontmatter")
if err != nil {
log.Print(err)
return 500, err
}
tpl.Execute(w, inf)
} }
return 200, nil 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" "log"
"net/http" "net/http"
"os" "os"
"text/template"
"github.com/hacdias/caddy-hugo/frontmatter" "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 // Execute the page
func Execute(w http.ResponseWriter, r *http.Request) (int, error) { func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
language := getConfigFrontMatter() language := getConfigFrontMatter()
@ -60,11 +66,22 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
return 500, err return 500, err
} }
page := new(page.Page) functions := template.FuncMap{
page.Name = "Settings" "splitCapitalize": utils.SplitCapitalize,
page.Class = "settings" }
page.Body = f
return page.Render(w, r, "settings", "frontmatter") 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 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> <li><a id="logout" href="#logout"><i class="fa fa-sign-out"></i> Logout</a></li>
</ul> </ul>
</nav> </nav>
<div class="main" id="container"> <div id="main">
{{ template "content" . }} {{ template "content" . }}
</div> </div>
</body> </body>

View File

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

View File

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

View File

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

View File

@ -2,12 +2,57 @@ package utils
import ( import (
"errors" "errors"
"log"
"net/http" "net/http"
"reflect" "reflect"
"strings" "strings"
"text/template"
"unicode" "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 // Dict allows to send more than one variable into a template
func Dict(values ...interface{}) (map[string]interface{}, error) { func Dict(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 { if len(values)%2 != 0 {
@ -34,36 +79,6 @@ func IsSlice(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Slice 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 // SplitCapitalize splits a string by its uppercase letters and capitalize the
// first letter of the string // first letter of the string
func SplitCapitalize(name string) string { func SplitCapitalize(name string) string {