Allow publish to Blogger Pages

This commit is contained in:
wiibaa 2014-01-19 14:31:36 +01:00
parent 6558d73425
commit 1c16921f3f
5 changed files with 156 additions and 2 deletions

View File

@ -942,6 +942,96 @@ define([
});
task.enqueue();
};
googleHelper.uploadBloggerPage = function(blogUrl, blogId, pageId, isDraft, publishDate, title, content, callback) {
var accountId = 'google.blogger0';
var task = new AsyncTask();
connect(task);
authenticate(task, 'blogger', accountId);
task.onRun(function() {
var headers = {};
var authorizationMgr = authorizationMgrMap[accountId];
if(authorizationMgr && authorizationMgr.token) {
headers.Authorization = "Bearer " + authorizationMgr.token.access_token;
}
function uploadPage() {
var url = "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/pages/";
var data = {
kind: "blogger#page",
blog: {
id: blogId
},
title: title,
content: content
};
var type = "POST";
// If it's an update
if(pageId !== undefined) {
url += pageId;
data.id = pageId;
type = "PUT";
}
$.ajax({
url: url,
data: JSON.stringify(data),
headers: headers,
type: type,
contentType: "application/json",
dataType: "json",
timeout: constants.AJAX_TIMEOUT
}).done(function(page) {
pageId = page.id;
task.chain();
}).fail(function(jqXHR) {
var error = {
code: jqXHR.status,
message: jqXHR.statusText
};
// Handle error
if(error.code === 404 && pageId !== undefined) {
error = 'Page ' + pageId + ' not found on Blogger.|removePublish';
}
handleError(error, task);
});
}
function getBlogId() {
if(blogId !== undefined) {
task.chain(uploadPage);
return;
}
$.ajax({
url: "https://www.googleapis.com/blogger/v3/blogs/byurl",
data: {
url: blogUrl
},
headers: headers,
dataType: "json",
timeout: constants.AJAX_TIMEOUT
}).done(function(blog) {
blogId = blog.id;
task.chain(uploadPage);
}).fail(function(jqXHR) {
var error = {
code: jqXHR.status,
message: jqXHR.statusText
};
// Handle error
if(error.code === 404) {
error = 'Blog "' + blogUrl + '" not found on Blogger.|removePublish';
}
handleError(error, task);
});
}
task.chain(getBlogId);
});
task.onSuccess(function() {
callback(undefined, blogId, pageId);
});
task.onError(function(error) {
callback(error);
});
task.enqueue();
};
// Use by Google's client.js
window.delayedFunction = undefined;

View File

@ -683,7 +683,7 @@
</div>
</div>
</div>
<div class="form-group modal-publish-blogger">
<div class="form-group modal-publish-blogger modal-publish-bloggerpage">
<label class="col-lg-4 control-label"
for="input-publish-blogger-url">Blog URL</label>
<div class="col-lg-7">
@ -720,6 +720,15 @@
class="form-control">
</div>
</div>
<div
class="form-group modal-publish-bloggerpage">
<label class="col-lg-4 control-label" for="input-publish-pageid">Update
existing page ID (optional)</label>
<div class="col-lg-7">
<input type="text" id="input-publish-pageid" placeholder="PageID"
class="form-control">
</div>
</div>
<div class="form-group modal-publish-dropbox">
<label class="col-lg-4 control-label"
for="input-publish-dropbox-path">File path</label>
@ -799,6 +808,18 @@
target="_blank">YAML front matter</a> to specify the title and the tags/labels of your publication.</p>
<p><b>Interpreted variables:</b> <code>title</code>, <code>tags</code>, <code>published</code>, <code>date</code>.</p>
</blockquote>
<blockquote class="front-matter-info modal-publish-bloggerpage">
<p><b>Tip:</b> You can use a
<a href="http://jekyllrb.com/docs/frontmatter/"
target="_blank">YAML front matter</a> to specify the title of your page.</p>
<p><b>Interpreted variables:</b> <code>title</code>.</p>
</blockquote>
<blockquote class="url-info modal-publish-bloggerpage">
<p><b>About URL:</b> For newly created page , Blogger API will append a generated number to the url like <code>about-me-1234.html</code>, if you deeply care about your URL naming, you should first create the page on Blogger and then update them with StackEdit specifying the pageId when publishing.
</p>
<p><b>About page visibility:</b> Blogger API does not respect published status for pages.When publishing the page to Blogger, the page will be <strong>live</strong> but not added to the page listing. You should arrange the page listing from Blogger dashboard.
</p>
</blockquote>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-default" data-dismiss="modal">Cancel</a>

View File

@ -0,0 +1,42 @@
define([
"underscore",
"utils",
"classes/Provider",
"helpers/googleHelper"
], function(_, utils, Provider, googleHelper) {
var bloggerPageProvider = new Provider("bloggerpage", "Blogger Page");
bloggerPageProvider.defaultPublishFormat = "html";
bloggerPageProvider.publishPreferencesInputIds = [
"blogger-url"
];
bloggerPageProvider.publish = function(publishAttributes, frontMatter, title, content, callback) {
var isDraft = frontMatter && frontMatter.published === false;
var publishDate = frontMatter && frontMatter.date;
googleHelper.uploadBloggerPage(publishAttributes.blogUrl, publishAttributes.blogId, publishAttributes.pageId, isDraft, publishDate, title, content, function(error, blogId, pageId) {
if(error) {
callback(error);
return;
}
publishAttributes.blogId = blogId;
publishAttributes.pageId = pageId;
callback();
});
};
bloggerPageProvider.newPublishAttributes = function(event) {
var publishAttributes = {};
var blogUrl = utils.getInputTextValue("#input-publish-blogger-url", event);
if(blogUrl !== undefined) {
publishAttributes.blogUrl = utils.checkUrl(blogUrl);
}
publishAttributes.pageId = utils.getInputTextValue("#input-publish-pageid");
if(event.isPropagationStopped()) {
return undefined;
}
return publishAttributes;
};
return bloggerPageProvider;
});

View File

@ -12,6 +12,7 @@ define([
"classes/Provider",
"classes/AsyncTask",
"providers/bloggerProvider",
"providers/bloggerPageProvider",
"providers/dropboxProvider",
"providers/gistProvider",
"providers/githubProvider",

View File

@ -346,7 +346,7 @@ kbd {
background-position: -54px 0;
}
.icon-provider-blogger {
.icon-provider-blogger, .icon-provider-bloggerpage {
background-position: -72px 0;
}