frontmatter parsing better; edit not working
parent
3b0d8380cb
commit
234e2a1ec4
26
edit/edit.go
26
edit/edit.go
|
@ -7,13 +7,16 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hacdias/caddy-hugo/frontmatter"
|
||||
"github.com/hacdias/caddy-hugo/page"
|
||||
"github.com/spf13/hugo/commands"
|
||||
"github.com/spf13/hugo/parser"
|
||||
)
|
||||
|
||||
type fileInfo struct {
|
||||
Content string
|
||||
Name string
|
||||
type information struct {
|
||||
Name string
|
||||
Content string
|
||||
FrontMatter interface{}
|
||||
}
|
||||
|
||||
// Execute sth
|
||||
|
@ -36,21 +39,28 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
return 404, nil
|
||||
}
|
||||
|
||||
file, err := ioutil.ReadFile(filename)
|
||||
reader, err := os.Open(filename)
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
inf := new(fileInfo)
|
||||
inf.Content = string(file)
|
||||
inf.Name = filename
|
||||
file, err := parser.ReadFrom(reader)
|
||||
|
||||
inf := new(information)
|
||||
inf.Content = string(file.Content())
|
||||
inf.FrontMatter, err = frontmatter.Pretty(file.FrontMatter())
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
page := new(page.Page)
|
||||
page.Title = "Edit"
|
||||
page.Body = inf
|
||||
return page.Render(w, r, "edit")
|
||||
return page.Render(w, r, "edit", "frontmatter")
|
||||
}
|
||||
|
||||
return 200, nil
|
||||
|
|
|
@ -8,24 +8,15 @@ import (
|
|||
)
|
||||
|
||||
// Pretty creates a new FrontMatter object
|
||||
func Pretty(content []byte, language string) (interface{}, error) {
|
||||
var err error
|
||||
var c interface{}
|
||||
|
||||
if language == "yaml" {
|
||||
c, err = parser.HandleYAMLMetaData(content)
|
||||
} else if language == "json" {
|
||||
c, err = parser.HandleJSONMetaData(content)
|
||||
} else if language == "toml" {
|
||||
c, err = parser.HandleTOMLMetaData(content)
|
||||
}
|
||||
func Pretty(content []byte) (interface{}, error) {
|
||||
frontType := parser.DetectFrontMatter(rune(content[0]))
|
||||
front, err := frontType.Parse(content)
|
||||
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
//log.Print(c)
|
||||
return rawToPretty(c, ""), nil
|
||||
return rawToPretty(front, ""), nil
|
||||
}
|
||||
|
||||
type frontmatter struct {
|
||||
|
|
|
@ -59,7 +59,7 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
return 500, err
|
||||
}
|
||||
|
||||
f, err := frontmatter.Pretty(content, language)
|
||||
f, err := frontmatter.Pretty(content)
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
|
|
|
@ -1,76 +1,106 @@
|
|||
body {
|
||||
font-family: 'Roboto', sans-serif;
|
||||
color: #212121;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
font-family: 'Roboto', sans-serif;
|
||||
color: #212121;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
header {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
height: 3em;
|
||||
width: 100%;
|
||||
background-color: #212121;
|
||||
padding: 0 2em;
|
||||
box-sizing: border-box;
|
||||
z-index: 999;
|
||||
color: #fff;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
height: 3em;
|
||||
width: 100%;
|
||||
background-color: #EEE;
|
||||
padding: 0 2em;
|
||||
box-sizing: border-box;
|
||||
z-index: 999;
|
||||
color: #555;
|
||||
}
|
||||
header nav {} header nav ul {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
|
||||
header nav {}
|
||||
|
||||
header nav ul {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
display: -webkit-box;
|
||||
/* OLD - iOS 6-, Safari 3.1-6 */
|
||||
display: -moz-box;
|
||||
/* OLD - Firefox 19- (buggy but mostly works) */
|
||||
display: -ms-flexbox;
|
||||
/* TWEENER - IE 10 */
|
||||
display: -webkit-flex;
|
||||
/* NEW - Chrome */
|
||||
display: flex;
|
||||
}
|
||||
|
||||
header nav ul li {
|
||||
list-style-type: none;
|
||||
display: inline-block;
|
||||
vertical-align: middle;
|
||||
list-style-type: none;
|
||||
display: inline-block;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
header nav ul li:last-child {
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
header nav img {
|
||||
height: 2em;
|
||||
}
|
||||
|
||||
header nav ul li a {
|
||||
padding: 0.5em 0.5em;
|
||||
line-height: 2em;
|
||||
display: block;
|
||||
text-decoration: none;
|
||||
color: inherit;
|
||||
transition: .5s ease background-color;
|
||||
padding: 0.5em 0.5em;
|
||||
line-height: 2em;
|
||||
display: block;
|
||||
text-decoration: none;
|
||||
color: inherit;
|
||||
transition: .5s ease background-color;
|
||||
}
|
||||
|
||||
header nav ul li a:hover {
|
||||
background-color: rgba(255, 255, 255, 0.3);
|
||||
background-color: rgba(255, 255, 255, 0.57);
|
||||
}
|
||||
|
||||
main {
|
||||
top: 3em;
|
||||
position: relative;
|
||||
top: 3em;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.content {
|
||||
margin: 1.5em auto;
|
||||
width: 80%;
|
||||
max-width: 800px;
|
||||
margin: 1.5em auto;
|
||||
width: 80%;
|
||||
max-width: 800px;
|
||||
}
|
||||
|
||||
textarea {
|
||||
width: 100%;
|
||||
min-height: 50em;
|
||||
resize: vertical;
|
||||
border: 0;
|
||||
font-family: inherit;
|
||||
width: 100%;
|
||||
min-height: 50em;
|
||||
resize: vertical;
|
||||
border: 0;
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
|
||||
/* FORMS */
|
||||
|
||||
form {
|
||||
|
||||
}
|
||||
form {}
|
||||
|
||||
form input {
|
||||
|
||||
color: rgba(0, 0, 0, 0.41);width: 15em;line-height: 1.25em;margin: .5em 0;border: 1px solid #fff;transition: .5s ease-out all;}
|
||||
color: rgba(0, 0, 0, 0.41);
|
||||
width: 15em;
|
||||
line-height: 1.25em;
|
||||
margin: .5em 0;
|
||||
border: 1px solid #fff;
|
||||
transition: .5s ease-out all;
|
||||
}
|
||||
|
||||
form input:focus {
|
||||
color: inherit;
|
||||
outline: 0;
|
||||
border-bottom: 1px solid #2196F3;
|
||||
color: inherit;
|
||||
outline: 0;
|
||||
border-bottom: 1px solid #2196F3;
|
||||
}
|
||||
|
||||
form label {
|
||||
width: 10.5em;display: inline-block;}
|
||||
width: 10.5em;
|
||||
display: inline-block;
|
||||
}
|
|
@ -4,11 +4,11 @@ $(document).ready(function() {
|
|||
var url = $(this).attr('action')
|
||||
|
||||
$.ajax({
|
||||
type : 'POST',
|
||||
url : url,
|
||||
data : data,
|
||||
dataType : 'json',
|
||||
encode : true,
|
||||
type: 'POST',
|
||||
url: url,
|
||||
data: data,
|
||||
dataType: 'json',
|
||||
encode: true,
|
||||
}).done(function(data) {
|
||||
alert("it workss");
|
||||
}).fail(function(data) {
|
||||
|
@ -17,24 +17,42 @@ $(document).ready(function() {
|
|||
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
$("#logout").click(function(e) {
|
||||
e.preventDefault();
|
||||
jQuery.ajax({
|
||||
type: "GET",
|
||||
url: "/admin",
|
||||
async: false,
|
||||
username: "logmeout",
|
||||
password: "123456",
|
||||
headers: {
|
||||
"Authorization": "Basic xxx"
|
||||
}
|
||||
})
|
||||
.fail(function() {
|
||||
window.location = "/";
|
||||
});
|
||||
return false;
|
||||
});
|
||||
});
|
||||
|
||||
$.fn.serializeField = function() {
|
||||
var result = {};
|
||||
this.each(function() {
|
||||
$(this).find("> *").each(function() {
|
||||
var $this = $(this);
|
||||
var name = $this.attr("name");
|
||||
|
||||
if ($this.is("fieldset") && name) {
|
||||
result[name] = $this.serializeField();
|
||||
}
|
||||
else {
|
||||
$.each($this.serializeArray(), function() {
|
||||
result[this.name] = this.value;
|
||||
});
|
||||
}
|
||||
});
|
||||
$.fn.serializeField = function() {
|
||||
var result = {};
|
||||
this.each(function() {
|
||||
$(this).find("> *").each(function() {
|
||||
var $this = $(this);
|
||||
var name = $this.attr("name");
|
||||
|
||||
if ($this.is("fieldset") && name) {
|
||||
result[name] = $this.serializeField();
|
||||
} else {
|
||||
$.each($this.serializeArray(), function() {
|
||||
result[this.name] = this.value;
|
||||
});
|
||||
}
|
||||
});
|
||||
return result;
|
||||
};
|
||||
});
|
||||
return result;
|
||||
};
|
|
@ -1,29 +1,32 @@
|
|||
<!DOCTYPE html>
|
||||
<html class="no-js" lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="theme-color" content="#fff">
|
||||
<title>{{ .Title }}</title>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="theme-color" content="#fff">
|
||||
<title>{{ .Title }}</title>
|
||||
|
||||
<link href='https://fonts.googleapis.com/css?family=Roboto:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
|
||||
<link href='https://fonts.googleapis.com/css?family=Roboto:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
|
||||
|
||||
<link rel="stylesheet" href="/admin/static/css/normalize.css">
|
||||
<link rel="stylesheet" href="/admin/static/css/main.css">
|
||||
<link rel="stylesheet" href="/admin/static/css/normalize.css">
|
||||
<link rel="stylesheet" href="/admin/static/css/main.css">
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
|
||||
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
|
||||
<script src="/admin/static/js/app.js"></script>
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
|
||||
<script src="/admin/static/js/app.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<nav>
|
||||
<ul>
|
||||
<li><img src="/admin/static/hugo.png"></li>
|
||||
<li><a href="/admin/browse/content">Content</a></li>
|
||||
<li><a href="/admin/browse">Browse</a></li>
|
||||
<li><a href="/admin/settings">Settings</a></li>
|
||||
<li><a href="/"><i class="fa fa-home fa-lg"></i> Home</a></li>
|
||||
<li><a href="/admin/browse/content"><i class="fa fa-newspaper-o"></i> Content</a></li>
|
||||
<li><a href="/admin/browse"><i class="fa fa-folder-o"></i> Browse</a></li>
|
||||
<li><a href="/admin/settings"><i class="fa fa-cog"></i> Settings</a></li>
|
||||
<li><a id="logout" href="#logout"><i class="fa fa-sign-out"></i> Logout</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
</header>
|
||||
|
@ -34,4 +37,5 @@
|
|||
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
</html>
|
|
@ -1,16 +1,14 @@
|
|||
{{ define "content" }}
|
||||
{{ with .Body }}
|
||||
{{ define "content" }} {{ with .Body }}
|
||||
|
||||
<div class="content">
|
||||
|
||||
<h1>Editing {{ .Name }}</h1>
|
||||
|
||||
<form method="POST" action="">
|
||||
{{ template "frontmatter" .FrontMatter }}
|
||||
<textarea name="content">{{ .Content }}</textarea>
|
||||
<input type="submit" value="Save">
|
||||
</form>
|
||||
|
||||
</div>
|
||||
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }} {{ end }}
|
Loading…
Reference in New Issue