issue #64 completed for editor route
This commit is contained in:
parent
3fd60e487a
commit
3301b60485
File diff suppressed because one or more lines are too long
|
@ -92,7 +92,7 @@ $(document).on('page:editor', function() {
|
||||||
var button = $(this).find("input[type=submit]:focus");
|
var button = $(this).find("input[type=submit]:focus");
|
||||||
var data = {
|
var data = {
|
||||||
content: $(this).serializeJSON(),
|
content: $(this).serializeJSON(),
|
||||||
type: button.data("type"),
|
contentType: button.data("type"),
|
||||||
schedule: button.data("schedule"),
|
schedule: button.data("schedule"),
|
||||||
regenerate: button.data("regenerate")
|
regenerate: button.data("regenerate")
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@
|
||||||
<input type="submit" data-type="{{ .Class }}" data-regenerate="false" data-schedule="false" data-message="{{ if eq .Class "frontmatter-only" }}The fields were put on their way.{{ else if eq .Class "content-only" }}Every byte was saved.{{ else }}Post saved with pomp and circumstance.{{ end }}" value="Save">
|
<input type="submit" data-type="{{ .Class }}" data-regenerate="false" data-schedule="false" data-message="{{ if eq .Class "frontmatter-only" }}The fields were put on their way.{{ else if eq .Class "content-only" }}Every byte was saved.{{ else }}Post saved with pomp and circumstance.{{ end }}" value="Save">
|
||||||
<span class="right">
|
<span class="right">
|
||||||
{{ if and (eq .Class "complete") ( .IsPost ) }}<input type="submit" data-type="{{ .Class }}" data-schedule="true" data-regenerate="false" data-message="Post scheduled." value="Schedule"> {{ end }}
|
{{ if and (eq .Class "complete") ( .IsPost ) }}<input type="submit" data-type="{{ .Class }}" data-schedule="true" data-regenerate="false" data-message="Post scheduled." value="Schedule"> {{ end }}
|
||||||
<input type="submit" data-type="{{ .Class }}" data-regenerate="true" data-schedule="true" data-message="{{ if eq .Class "frontmatter-only" }}Saved and regenerated.{{ else if eq .Class "content-only" }}Done. What do you want more?{{ else }}Post published. Go and share it!{{ end }}" class="default" value="Publish">
|
<input type="submit" data-type="{{ .Class }}" data-regenerate="true" data-schedule="false" data-message="{{ if eq .Class "frontmatter-only" }}Saved and regenerated.{{ else if eq .Class "content-only" }}Done. What do you want more?{{ else }}Post published. Go and share it!{{ end }}" class="default" value="Publish">
|
||||||
</span>
|
</span>
|
||||||
</p>
|
</p>
|
||||||
</main>
|
</main>
|
||||||
|
|
|
@ -8,16 +8,22 @@ import (
|
||||||
"github.com/hacdias/caddy-hugo/config"
|
"github.com/hacdias/caddy-hugo/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
filename string
|
||||||
|
conf *config.Config
|
||||||
|
)
|
||||||
|
|
||||||
// ServeHTTP serves the editor page
|
// ServeHTTP serves the editor page
|
||||||
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
||||||
filename := strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
|
conf = c
|
||||||
|
filename = strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
|
||||||
filename = c.Path + filename
|
filename = c.Path + filename
|
||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "POST":
|
case "POST":
|
||||||
return POST(w, r, c, filename)
|
return POST(w, r)
|
||||||
case "GET":
|
case "GET":
|
||||||
return GET(w, r, c, filename)
|
return GET(w, r)
|
||||||
default:
|
default:
|
||||||
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
|
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ type editor struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET handles the GET method on editor page
|
// GET handles the GET method on editor page
|
||||||
func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
|
func GET(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
// Check if the file format is supported. If not, send a "Not Acceptable"
|
// Check if the file format is supported. If not, send a "Not Acceptable"
|
||||||
// header and an error
|
// header and an error
|
||||||
if !templates.CanBeEdited(filename) {
|
if !templates.CanBeEdited(filename) {
|
||||||
|
@ -55,8 +55,8 @@ func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename stri
|
||||||
// Create a new editor variable and set the extension
|
// Create a new editor variable and set the extension
|
||||||
page := new(editor)
|
page := new(editor)
|
||||||
page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".")
|
page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".")
|
||||||
page.Name = strings.Replace(filename, c.Path, "", 1)
|
page.Name = strings.Replace(filename, conf.Path, "", 1)
|
||||||
page.Config = c
|
page.Config = conf
|
||||||
page.IsPost = false
|
page.IsPost = false
|
||||||
|
|
||||||
// Sanitize the extension
|
// Sanitize the extension
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hacdias/caddy-hugo/config"
|
|
||||||
"github.com/hacdias/caddy-hugo/tools/hugo"
|
"github.com/hacdias/caddy-hugo/tools/hugo"
|
||||||
"github.com/hacdias/caddy-hugo/tools/server"
|
"github.com/hacdias/caddy-hugo/tools/server"
|
||||||
"github.com/robfig/cron"
|
"github.com/robfig/cron"
|
||||||
|
@ -18,54 +17,34 @@ import (
|
||||||
"github.com/spf13/hugo/parser"
|
"github.com/spf13/hugo/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
var schedule, contentType, regenerate string
|
var data info
|
||||||
|
|
||||||
|
type info struct {
|
||||||
|
ContentType string
|
||||||
|
Schedule bool
|
||||||
|
Regenerate bool
|
||||||
|
Content map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
// POST handles the POST method on editor page
|
// POST handles the POST method on editor page
|
||||||
func POST(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
|
func POST(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
// Get the JSON information sent using a buffer
|
// Get the JSON information sent using a buffer
|
||||||
rawBuffer := new(bytes.Buffer)
|
rawBuffer := new(bytes.Buffer)
|
||||||
rawBuffer.ReadFrom(r.Body)
|
rawBuffer.ReadFrom(r.Body)
|
||||||
|
err := json.Unmarshal(rawBuffer.Bytes(), &data)
|
||||||
|
|
||||||
// Creates the data map using the JSON
|
if err != nil {
|
||||||
var data map[string]interface{}
|
|
||||||
json.Unmarshal(rawBuffer.Bytes(), &data)
|
|
||||||
|
|
||||||
// Checks if all the all data is defined
|
|
||||||
if _, ok := data["type"]; !ok {
|
|
||||||
return server.RespondJSON(w, map[string]string{
|
return server.RespondJSON(w, map[string]string{
|
||||||
"message": "Content type not set.",
|
"message": "Error decrypting json.",
|
||||||
}, http.StatusBadRequest, nil)
|
}, http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := data["content"]; !ok {
|
|
||||||
return server.RespondJSON(w, map[string]string{
|
|
||||||
"message": "Content not sent.",
|
|
||||||
}, http.StatusBadRequest, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := data["schedule"]; !ok {
|
|
||||||
return server.RespondJSON(w, map[string]string{
|
|
||||||
"message": "Schedule information not sent.",
|
|
||||||
}, http.StatusBadRequest, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := data["regenerate"]; !ok {
|
|
||||||
return server.RespondJSON(w, map[string]string{
|
|
||||||
"message": "Regenerate information not sent.",
|
|
||||||
}, http.StatusBadRequest, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
rawFile := data["content"].(map[string]interface{})
|
|
||||||
contentType = data["type"].(string)
|
|
||||||
schedule = data["schedule"].(string)
|
|
||||||
regenerate = data["regenerate"].(string)
|
|
||||||
|
|
||||||
// Initializes the file content to write
|
// Initializes the file content to write
|
||||||
var file []byte
|
var file []byte
|
||||||
|
|
||||||
switch contentType {
|
switch data.ContentType {
|
||||||
case "frontmatter-only":
|
case "frontmatter-only":
|
||||||
f, code, err := parseFrontMatterOnlyFile(rawFile, filename)
|
f, code, err := parseFrontMatterOnlyFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return server.RespondJSON(w, map[string]string{
|
return server.RespondJSON(w, map[string]string{
|
||||||
"message": err.Error(),
|
"message": err.Error(),
|
||||||
|
@ -75,12 +54,12 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config, filename str
|
||||||
file = f
|
file = f
|
||||||
case "content-only":
|
case "content-only":
|
||||||
// The main content of the file
|
// The main content of the file
|
||||||
mainContent := rawFile["content"].(string)
|
mainContent := data.Content["content"].(string)
|
||||||
mainContent = strings.TrimSpace(mainContent)
|
mainContent = strings.TrimSpace(mainContent)
|
||||||
|
|
||||||
file = []byte(mainContent)
|
file = []byte(mainContent)
|
||||||
case "complete":
|
case "complete":
|
||||||
f, code, err := parseCompleteFile(r, c, rawFile, filename)
|
f, code, err := parseCompleteFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return server.RespondJSON(w, map[string]string{
|
return server.RespondJSON(w, map[string]string{
|
||||||
"message": err.Error(),
|
"message": err.Error(),
|
||||||
|
@ -95,7 +74,7 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config, filename str
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the file
|
// Write the file
|
||||||
err := ioutil.WriteFile(filename, file, 0666)
|
err = ioutil.WriteFile(filename, file, 0666)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return server.RespondJSON(w, map[string]string{
|
return server.RespondJSON(w, map[string]string{
|
||||||
|
@ -103,14 +82,14 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config, filename str
|
||||||
}, http.StatusInternalServerError, err)
|
}, http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if regenerate == "true" {
|
if data.Regenerate {
|
||||||
go hugo.Run(c, false)
|
go hugo.Run(conf, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
return server.RespondJSON(w, map[string]string{}, http.StatusOK, nil)
|
return server.RespondJSON(w, nil, http.StatusOK, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFrontMatterOnlyFile(rawFile map[string]interface{}, filename string) ([]byte, int, error) {
|
func parseFrontMatterOnlyFile() ([]byte, int, error) {
|
||||||
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
|
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
|
||||||
var mark rune
|
var mark rune
|
||||||
|
|
||||||
|
@ -125,7 +104,7 @@ func parseFrontMatterOnlyFile(rawFile map[string]interface{}, filename string) (
|
||||||
return []byte{}, http.StatusBadRequest, errors.New("Can't define the frontmatter.")
|
return []byte{}, http.StatusBadRequest, errors.New("Can't define the frontmatter.")
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := parser.InterfaceToFrontMatter(rawFile, mark)
|
f, err := parser.InterfaceToFrontMatter(data.Content, mark)
|
||||||
fString := string(f)
|
fString := string(f)
|
||||||
|
|
||||||
// If it's toml or yaml, strip frontmatter identifier
|
// If it's toml or yaml, strip frontmatter identifier
|
||||||
|
@ -148,25 +127,25 @@ func parseFrontMatterOnlyFile(rawFile map[string]interface{}, filename string) (
|
||||||
return f, http.StatusOK, nil
|
return f, http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]interface{}, filename string) ([]byte, int, error) {
|
func parseCompleteFile() ([]byte, int, error) {
|
||||||
// The main content of the file
|
// The main content of the file
|
||||||
mainContent := rawFile["content"].(string)
|
mainContent := data.Content["content"].(string)
|
||||||
mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"
|
mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"
|
||||||
|
|
||||||
// Removes the main content from the rest of the frontmatter
|
// Removes the main content from the rest of the frontmatter
|
||||||
delete(rawFile, "content")
|
delete(data.Content, "content")
|
||||||
|
|
||||||
// Schedule the post
|
// Schedule the post
|
||||||
if schedule == "true" {
|
if data.Schedule {
|
||||||
t := cast.ToTime(rawFile["date"].(string))
|
t := cast.ToTime(data.Content["date"].(string))
|
||||||
|
|
||||||
scheduler := cron.New()
|
scheduler := cron.New()
|
||||||
scheduler.AddFunc(t.In(time.Now().Location()).Format("05 04 15 02 01 *"), func() {
|
scheduler.AddFunc(t.In(time.Now().Location()).Format("05 04 15 02 01 *"), func() {
|
||||||
// Set draft to false
|
// Set draft to false
|
||||||
rawFile["draft"] = false
|
data.Content["draft"] = false
|
||||||
|
|
||||||
// Converts the frontmatter in JSON
|
// Converts the frontmatter in JSON
|
||||||
jsonFrontmatter, err := json.Marshal(rawFile)
|
jsonFrontmatter, err := json.Marshal(data.Content)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -187,13 +166,13 @@ func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]int
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go hugo.Run(c, false)
|
go hugo.Run(conf, false)
|
||||||
})
|
})
|
||||||
scheduler.Start()
|
scheduler.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts the frontmatter in JSON
|
// Converts the frontmatter in JSON
|
||||||
jsonFrontmatter, err := json.Marshal(rawFile)
|
jsonFrontmatter, err := json.Marshal(data.Content)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte{}, http.StatusInternalServerError, err
|
return []byte{}, http.StatusInternalServerError, err
|
||||||
|
|
|
@ -6,8 +6,12 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RespondJSON
|
// RespondJSON used to send JSON responses to the web server
|
||||||
func RespondJSON(w http.ResponseWriter, message map[string]string, code int, err error) (int, error) {
|
func RespondJSON(w http.ResponseWriter, message map[string]string, code int, err error) (int, error) {
|
||||||
|
if message == nil {
|
||||||
|
message = map[string]string{}
|
||||||
|
}
|
||||||
|
|
||||||
msg, msgErr := json.Marshal(message)
|
msg, msgErr := json.Marshal(message)
|
||||||
|
|
||||||
if msgErr != nil {
|
if msgErr != nil {
|
||||||
|
|
Loading…
Reference in New Issue