need to finish schedule; build is going to fail

This commit is contained in:
Henrique Dias 2016-06-29 20:39:29 +01:00
parent b278faf383
commit f206220527
2 changed files with 76 additions and 3 deletions

View File

@ -61,7 +61,7 @@ document.addEventListener('editor', event => {
</button>
</div>`);
if (document.getElementById('date') || document.getElementById('publishdate')) {
if ((document.getElementById('date') || document.getElementById('publishdate')) && document.getElementById('editor').dataset.kind == "complete") {
document.querySelector('#editor .right').insertAdjacentHTML('afterbegin', ` <button id="schedule">
<span>
<i class="material-icons">alarm</i>
@ -71,11 +71,32 @@ document.addEventListener('editor', event => {
document.getElementById('schedule').addEventListener('click', event => {
event.preventDefault();
let date = document.getElementById('date').value;
if (document.getElementById('publishDate')) {
date = document.getElementById('publishDate').value;
}
let container = document.getElementById('editor');
let kind = container.dataset.kind;
let button = document.querySelector('#schedule span:first-child');
let data = form2js(document.querySelector('form'));
let html = button.changeToLoading();
let request = new XMLHttpRequest();
request.open("PUT", window.location);
request.setRequestHeader('Kind', kind);
request.setRequestHeader('Schedule', date);
request.send(JSON.stringify(data));
request.onreadystatechange = function() {
if (request.readyState == 4) {
button.changeToDone((request.status != 200), html);
}
}
});
}
document.getElementById('publish').addEventListener('click', event => {
console.log("Hey")
event.preventDefault();
if (document.getElementById('draft')) {
@ -98,4 +119,4 @@ document.addEventListener('editor', event => {
}
}
});
});
});

52
hugo.go
View File

@ -7,12 +7,14 @@
package hugo
import (
"io/ioutil"
"log"
"mime"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/hacdias/caddy-filemanager"
"github.com/hacdias/caddy-filemanager/assets"
@ -20,6 +22,8 @@ import (
"github.com/hacdias/caddy-filemanager/utils/variables"
"github.com/hacdias/caddy-hugo/utils/commands"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/robfig/cron"
"github.com/spf13/cast"
)
// Hugo is hugo
@ -133,6 +137,54 @@ func RunHugo(c *Config, force bool) {
// Schedule schedules a post to be published later
func Schedule(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
// TODO: this
t := cast.ToTime(r.Header.Get("Date"))
scheduler := cron.New()
scheduler.AddFunc(t.In(time.Now().Location()).Format("05 04 15 02 01 *"), func() {
filename := r.URL.Path
filename = strings.Replace(filename, c.BaseURL, c.Root, 1)
filename = filepath.Clean(filename)
raw, err := ioutil.ReadFile(filename)
if err != nil {
log.Println(err)
return
}
})
scheduler.Start()
/*
// Set draft to false
data.Content["draft"] = false
// Converts the frontmatter in JSON
jsonFrontmatter, err := json.Marshal(data.Content)
if err != nil {
return
}
// Indents the json
frontMatterBuffer := new(bytes.Buffer)
json.Indent(frontMatterBuffer, jsonFrontmatter, "", " ")
// Generates the final file
f := new(bytes.Buffer)
f.Write(frontMatterBuffer.Bytes())
f.Write([]byte(mainContent))
file := f.Bytes()
// Write the file
if err = ioutil.WriteFile(filename, file, 0666); err != nil {
return
}
go hugo.Run(c, false)
*/
return http.StatusOK, nil
}