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> </button>
</div>`); </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"> document.querySelector('#editor .right').insertAdjacentHTML('afterbegin', ` <button id="schedule">
<span> <span>
<i class="material-icons">alarm</i> <i class="material-icons">alarm</i>
@ -71,11 +71,32 @@ document.addEventListener('editor', event => {
document.getElementById('schedule').addEventListener('click', event => { document.getElementById('schedule').addEventListener('click', event => {
event.preventDefault(); 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 => { document.getElementById('publish').addEventListener('click', event => {
console.log("Hey")
event.preventDefault(); event.preventDefault();
if (document.getElementById('draft')) { if (document.getElementById('draft')) {

52
hugo.go
View File

@ -7,12 +7,14 @@
package hugo package hugo
import ( import (
"io/ioutil"
"log" "log"
"mime" "mime"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/hacdias/caddy-filemanager" "github.com/hacdias/caddy-filemanager"
"github.com/hacdias/caddy-filemanager/assets" "github.com/hacdias/caddy-filemanager/assets"
@ -20,6 +22,8 @@ import (
"github.com/hacdias/caddy-filemanager/utils/variables" "github.com/hacdias/caddy-filemanager/utils/variables"
"github.com/hacdias/caddy-hugo/utils/commands" "github.com/hacdias/caddy-hugo/utils/commands"
"github.com/mholt/caddy/caddyhttp/httpserver" "github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/robfig/cron"
"github.com/spf13/cast"
) )
// Hugo is hugo // Hugo is hugo
@ -133,6 +137,54 @@ func RunHugo(c *Config, force bool) {
// Schedule schedules a post to be published later // Schedule schedules a post to be published later
func Schedule(w http.ResponseWriter, r *http.Request, c *Config) (int, error) { func Schedule(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
// TODO: this // 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 return http.StatusOK, nil
} }