From 511ffafbf0aeaa3647ab456a47a7b203bef973b8 Mon Sep 17 00:00:00 2001 From: benweet Date: Sun, 8 Dec 2013 18:05:49 +0000 Subject: [PATCH] Support for published and date variables in YAML front matter. Fixes #222, fixes #220, fixes #146, fixes #72 --- public/res/helpers/googleHelper.js | 41 ++++++++++++++++++++--- public/res/helpers/tumblrHelper.js | 4 ++- public/res/helpers/wordpressHelper.js | 4 ++- public/res/providers/bloggerProvider.js | 4 ++- public/res/providers/tumblrProvider.js | 4 ++- public/res/providers/wordpressProvider.js | 4 ++- 6 files changed, 51 insertions(+), 10 deletions(-) diff --git a/public/res/helpers/googleHelper.js b/public/res/helpers/googleHelper.js index 5d661aa6..cf477b3f 100644 --- a/public/res/helpers/googleHelper.js +++ b/public/res/helpers/googleHelper.js @@ -720,7 +720,7 @@ define([ task.enqueue(); }; - googleHelper.uploadBlogger = function(blogUrl, blogId, postId, labelList, title, content, callback) { + googleHelper.uploadBlogger = function(blogUrl, blogId, postId, labelList, isDraft, publishDate, title, content, callback) { var task = new AsyncTask(); connect(task); authenticate(task, 'blogger'); @@ -730,7 +730,7 @@ define([ if(token) { headers.Authorization = "Bearer " + token.access_token; } - function publish() { + function uploadPost() { var url = "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/posts/"; var data = { kind: "blogger#post", @@ -758,7 +758,7 @@ define([ timeout: constants.AJAX_TIMEOUT }).done(function(post) { postId = post.id; - task.chain(); + task.chain(publish); }).fail(function(jqXHR) { var error = { code: jqXHR.status, @@ -771,9 +771,40 @@ define([ handleError(error, task); }); } + function publish() { + var url = "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/posts/" + postId; + if(isDraft) { + url += "/revert"; + } + else { + url += "/publish"; + if(publishDate) { + url += '?publishDate=' + publishDate.toISOString(); + } + } + $.ajax({ + url: url, + headers: headers, + type: 'POST', + dataType: "json", + timeout: constants.AJAX_TIMEOUT + }).done(function(post) { + task.chain(); + }).fail(function(jqXHR) { + var error = { + code: jqXHR.status, + message: jqXHR.statusText + }; + // Handle error + if(error.code === 404) { + error = 'Post ' + postId + ' not found on Blogger.|removePublish'; + } + handleError(error, task); + }); + } function getBlogId() { if(blogId !== undefined) { - task.chain(publish); + task.chain(uploadPost); return; } $.ajax({ @@ -786,7 +817,7 @@ define([ timeout: constants.AJAX_TIMEOUT }).done(function(blog) { blogId = blog.id; - task.chain(publish); + task.chain(uploadPost); }).fail(function(jqXHR) { var error = { code: jqXHR.status, diff --git a/public/res/helpers/tumblrHelper.js b/public/res/helpers/tumblrHelper.js index 330ffbef..90d516ff 100644 --- a/public/res/helpers/tumblrHelper.js +++ b/public/res/helpers/tumblrHelper.js @@ -110,7 +110,7 @@ define([ }); } - tumblrHelper.upload = function(blogHostname, postId, tags, format, title, content, callback) { + tumblrHelper.upload = function(blogHostname, postId, tags, format, state, date, title, content, callback) { var task = new AsyncTask(); connect(task); authenticate(task); @@ -120,6 +120,8 @@ define([ post_id: postId, tags: tags, format: format, + state: state, + date: date, title: title, content: content }, oauthParams); diff --git a/public/res/helpers/wordpressHelper.js b/public/res/helpers/wordpressHelper.js index 1c4288af..c493f18f 100644 --- a/public/res/helpers/wordpressHelper.js +++ b/public/res/helpers/wordpressHelper.js @@ -94,7 +94,7 @@ define([ }); } - wordpressHelper.upload = function(site, postId, tags, title, content, callback) { + wordpressHelper.upload = function(site, postId, tags, status, date, title, content, callback) { var task = new AsyncTask(); connect(task); authenticate(task); @@ -105,6 +105,8 @@ define([ site: site, postId: postId, tags: tags, + status: status, + date: date, title: title, content: content }; diff --git a/public/res/providers/bloggerProvider.js b/public/res/providers/bloggerProvider.js index 71b9d1da..e9dee557 100644 --- a/public/res/providers/bloggerProvider.js +++ b/public/res/providers/bloggerProvider.js @@ -17,7 +17,9 @@ define([ frontMatter.tags !== undefined && (labelList = frontMatter.tags); } _.isString(labelList) && (labelList = _.compact(labelList.split(/[\s,]/))); - googleHelper.uploadBlogger(publishAttributes.blogUrl, publishAttributes.blogId, publishAttributes.postId, labelList, title, content, function(error, blogId, postId) { + var isDraft = frontMatter && frontMatter.published === false; + var publishDate = frontMatter && frontMatter.date; + googleHelper.uploadBlogger(publishAttributes.blogUrl, publishAttributes.blogId, publishAttributes.postId, labelList, isDraft, publishDate, title, content, function(error, blogId, postId) { if(error) { callback(error); return; diff --git a/public/res/providers/tumblrProvider.js b/public/res/providers/tumblrProvider.js index 6e6cc031..63d5fa9e 100644 --- a/public/res/providers/tumblrProvider.js +++ b/public/res/providers/tumblrProvider.js @@ -32,7 +32,9 @@ define([ return 'markdown'; })(); - tumblrHelper.upload(publishAttributes.blogHostname, publishAttributes.postId, labelList.join(','), format, title, content, function(error, postId) { + var state = (frontMatter && frontMatter.published === false) ? 'draft' : 'published'; + var date = frontMatter && frontMatter.date; + tumblrHelper.upload(publishAttributes.blogHostname, publishAttributes.postId, labelList.join(','), format, state, date, title, content, function(error, postId) { if(error) { callback(error); return; diff --git a/public/res/providers/wordpressProvider.js b/public/res/providers/wordpressProvider.js index be5fe21d..2f3863ec 100644 --- a/public/res/providers/wordpressProvider.js +++ b/public/res/providers/wordpressProvider.js @@ -16,8 +16,10 @@ define([ if(frontMatter) { frontMatter.tags !== undefined && (labelList = frontMatter.tags); } + var status = (frontMatter && frontMatter.published === false) ? 'draft' : 'publish'; + var date = frontMatter && frontMatter.date; _.isString(labelList) && (labelList = _.compact(labelList.split(/[\s,]/))); - wordpressHelper.upload(publishAttributes.site, publishAttributes.postId, labelList.join(','), title, content, function(error, postId) { + wordpressHelper.upload(publishAttributes.site, publishAttributes.postId, labelList.join(','), status, date, title, content, function(error, postId) { if(error) { callback(error); return;