From 886f396d8f6057cfa9c7df40cbf645f113419a79 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 22 Jun 2016 18:27:12 +0100 Subject: [PATCH] updates --- assets/public/js/application.js | 7 +++++++ assets/templates/editor.tmpl | 7 +++++-- assets/templates/frontmatter.tmpl | 16 ++++++---------- get.go | 2 ++ hugo.go | 29 +++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/assets/public/js/application.js b/assets/public/js/application.js index e69de29b..a85c926e 100644 --- a/assets/public/js/application.js +++ b/assets/public/js/application.js @@ -0,0 +1,7 @@ +"use strict"; + +document.addEventListener("DOMContentLoaded", function(event) { + //alert("hugo is working"); + + return false; +}); diff --git a/assets/templates/editor.tmpl b/assets/templates/editor.tmpl index 8916a5d4..337194de 100644 --- a/assets/templates/editor.tmpl +++ b/assets/templates/editor.tmpl @@ -1,4 +1,7 @@ {{ define "content" }} + + +
{{ if eq .Class "complete" }}

@@ -11,7 +14,7 @@ {{ if eq .Class "frontmatter-only" }}
- {{ template "frontmatter" .FrontMatter }} + {{ template "options" .FrontMatter }}

@@ -24,7 +27,7 @@ {{ else }}
- {{ template "frontmatter" .FrontMatter }} + {{ template "options" .FrontMatter}}

diff --git a/assets/templates/frontmatter.tmpl b/assets/templates/frontmatter.tmpl index f00f6af5..e37f9244 100644 --- a/assets/templates/frontmatter.tmpl +++ b/assets/templates/frontmatter.tmpl @@ -1,23 +1,19 @@ -{{ define "frontmatter" }} +{{ define "options" }} {{ range $key, $value := . }} {{ if or (eq $value.Type "object") (eq $value.Type "array") }}

{{ SplitCapitalize $value.Title }}

- - - - - {{ template "frontmatter" $value.Content }} + + + {{ template "options" $value.Content }}
{{ else }} {{ if not (eq $value.Parent.Type "array") }}
- - - + {{ end }} @@ -36,7 +32,7 @@ {{ if not (eq $value.Parent.Type "array") }}
{{ end }} {{ if eq $value.Parent.Type "array" }} -
+
{{ end }} {{ end }} diff --git a/get.go b/get.go index 36bb381f..44c8f55e 100644 --- a/get.go +++ b/get.go @@ -19,6 +19,7 @@ type editor struct { IsPost bool Mode string Content string + BaseURL string FrontMatter interface{} } @@ -54,6 +55,7 @@ func (h Hugo) GET(w http.ResponseWriter, r *http.Request, filename string) (int, data.Mode = strings.TrimPrefix(filepath.Ext(filename), ".") data.Name = strings.Replace(filename, h.Config.Root, "", 1) data.IsPost = false + data.BaseURL = h.Config.BaseURL data.Mode = sanitizeMode(data.Mode) var parserPage parser.Page diff --git a/hugo.go b/hugo.go index d6411fbc..d282973c 100644 --- a/hugo.go +++ b/hugo.go @@ -7,6 +7,7 @@ package hugo import ( + "mime" "net/http" "os" "path/filepath" @@ -16,6 +17,9 @@ import ( "github.com/mholt/caddy/caddyhttp/httpserver" ) +// AssetsURL is the base url of the assets +const AssetsURL = "/_hugointernal" + // Hugo contais the next middleware to be run and the configuration // of the current one. type Hugo struct { @@ -29,6 +33,11 @@ type Hugo struct { func (h Hugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { // Check if the current request if for this plugin if httpserver.Path(r.URL.Path).Matches(h.Config.BaseURL) { + // Check if we are asking for the assets + if httpserver.Path(r.URL.Path).Matches(h.Config.BaseURL + AssetsURL) { + return h.ServeAssets(w, r) + } + // If the url matches exactly with /{admin}/settings/, redirect // to the page of the configuration file if r.URL.Path == h.Config.BaseURL+"/settings/" { @@ -114,3 +123,23 @@ func (h Hugo) ShouldHandle(r *http.Request) bool { return false } + +// ServeAssets provides the needed assets for the front-end +func (h Hugo) ServeAssets(w http.ResponseWriter, r *http.Request) (int, error) { + // gets the filename to be used with Assets function + filename := strings.Replace(r.URL.Path, h.Config.BaseURL+AssetsURL, "public", 1) + file, err := Asset(filename) + if err != nil { + return http.StatusNotFound, nil + } + + // Get the file extension and its mimetype + extension := filepath.Ext(filename) + mediatype := mime.TypeByExtension(extension) + + // Write the header with the Content-Type and write the file + // content to the buffer + w.Header().Set("Content-Type", mediatype) + w.Write(file) + return 200, nil +}