Support for Blogger

This commit is contained in:
benweet 2013-04-15 22:15:15 +01:00
parent c5b775f576
commit ded5975c14
8 changed files with 207 additions and 117 deletions

View File

@ -120,7 +120,7 @@ code {
}
h1 {
margin: 50px 0 20px;
margin: 40px 0 20px;
}
h2 {

View File

@ -88,6 +88,8 @@
<ul class="dropdown-menu">
<li><a href="#" class="action-publish-github"><i
class="icon-github"></i> GitHub</a></li>
<li><a href="#" class="action-publish-blogger"><i
class="icon-blogger"></i> Blogger</a></li>
</ul></li>
<li><a href="#" data-toggle="modal"
data-target="#modal-manage-publish" class="action-reset-input"><i
@ -344,7 +346,7 @@
is not published.
</p>
<p class="muted"><b>NOTE:</b> You can add locations using
sub-menu "Publish on".</p>
"Publish on" sub-menu.</p>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>
@ -381,13 +383,13 @@
</div>
</div>
<div class="control-group">
<label class="control-label"
for="input-settings-converter-type">Converter</label>
<label class="control-label" for="input-settings-converter-type">Converter</label>
<div class="controls">
<select id="input-settings-converter-type">
<option value="markdown">Markdown</option>
<option value="markdown-extra">Markdown Extra</option>
<option value="markdown-extra-prettify">Markdown Extra + Prettify</option>
<option value="markdown-extra-prettify">Markdown Extra
+ Prettify</option>
</select>
</div>
</div>
@ -412,7 +414,9 @@
</div>
<div class="control-group">
<label class="control-label"
for="textarea-settings-publish-template">Template (<a href="#" class="tooltip-template">?</a>)</label>
for="textarea-settings-publish-template">Template <a
href="#" class="tooltip-template">(?)</a>
</label>
<div class="controls">
<textarea id="textarea-settings-publish-template"></textarea>
</div>
@ -494,12 +498,19 @@
</dd>
<dd>
<a target="_blank" href="https://code.google.com/p/pagedown/">PageDown</a>
/ <a target="_blank"
href="https://github.com/jmcmanus/pagedown-extra/">Pagedown-extra</a>
</dd>
<dd>
<a target="_blank"
href="https://code.google.com/p/google-code-prettify/">Prettify</a>
</dd>
<dd>
<a target="_blank" href="http://requirejs.org/">RequireJS</a>
</dd>
<dd>
<a target="_blank" href="http://layout.jquery-dev.net/">UI Layout</a>
<a target="_blank" href="http://layout.jquery-dev.net/">UI
Layout</a>
</dd>
<dd>
<a target="_blank" href="http://underscorejs.org/">Underscore.js</a>

45
js/blogger-provider.js Normal file
View File

@ -0,0 +1,45 @@
define(["jquery", "google-helper"], function($, googleHelper) {
// Dependencies
var core = undefined;
var bloggerProvider = {
providerType: PROVIDER_TYPE_PUBLISH_FLAG,
providerId: PROVIDER_BLOGGER,
providerName: "Blogger",
defaultPublishFormat: "html"
};
bloggerProvider.publish = function(publishAttributes, title, content, callback) {
googleHelper.uploadBlogger(publishAttributes.blogUrl,
publishAttributes.blogId, publishAttributes.postId, title, content,
function(blogId, postId) {
if(blogId === undefined || postId === undefined) {
callback(true);
return;
}
publishAttributes.blogId = blogId;
publishAttributes.postId = postId;
callback();
});
};
bloggerProvider.newPublishAttributes = function(event) {
var publishAttributes = {};
publishAttributes.blogUrl = core.getInputValue($("#input-publish-blogger-url"), event);
var postId = $("#input-publish-blogger-postid").val();
if(postId) {
publishAttributes.postId = postId;
}
if(event.isPropagationStopped()) {
return undefined;
}
return publishAttributes;
};
bloggerProvider.init = function(coreModule) {
core = coreModule;
};
return bloggerProvider;
});

View File

@ -370,49 +370,6 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni
});
}
// Initialize the "New publication" dialog
var newPublishProvider = undefined;
function initNewPublish(provider, defaultPublishFormat) {
defaultPublishFormat = defaultPublishFormat || "markdown";
newPublishProvider = provider;
// Show/hide controls depending on provider
$('div[class*=" modal-publish-"]').hide().filter(".modal-publish-" + provider).show();
// Reset fields
core.resetModalInputs();
$("input:radio[name=radio-publish-format][value=" + defaultPublishFormat + "]").prop("checked", true);
// Open dialog box
$("#modal-publish").modal();
}
// Create a new publication on GitHub
function newPublishGithub(event) {
var publishAttributes = {};
publishAttributes.repository = core.getInputValue($("#input-publish-github-reponame"), event);
publishAttributes.branch = core.getInputValue($("#input-publish-github-branch"), event);
publishAttributes.path = core.getInputValue($("#input-publish-github-path"), event);
publishAttributes.provider = newPublishProvider;
if(event.isPropagationStopped()) {
return;
}
publisher.newLocation(publishAttributes);
}
// Create a new publication on Blogger
function newPublishBlogger(event) {
var blogUrl = core.getInputValue($("#input-publish-blogger-url"), event);
if(event.isPropagationStopped()) {
return;
}
googleHelper.getBlogByUrl(blogUrl, function(blog) {
console.log(blog);
});
}
fileManager.init = function(coreModule) {
core = coreModule;
@ -497,22 +454,6 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni
var path = core.getInputValue($("#manual-dropbox-path"), event);
manualDropbox(path);
});
// Publish actions
$(".action-publish-github").click(function() {
initNewPublish(PROVIDER_GITHUB);
});
$(".action-publish-blogger").click(function() {
initNewPublish(PROVIDER_BLOGGER, "html");
});
$(".action-process-publish").click(function(e) {
if(newPublishProvider == PROVIDER_GITHUB) {
newPublishGithub(e);
}
else if(newPublishProvider == PROVIDER_BLOGGER) {
newPublishBlogger(e);
}
});
};
return fileManager;

View File

@ -161,7 +161,9 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
callback(error);
};
asyncTask.onError = function() {
if(error !== undefined) {
console.error(error);
}
var errorMsg = "Could not publish on GitHub.";
if(error === 401 || error === 403) {
github = undefined;

View File

@ -3,21 +3,32 @@ define(["jquery", "github-helper"], function($, githubHelper) {
// Dependencies
var core = undefined;
var publishGithub = {
var githubProvider = {
providerType: PROVIDER_TYPE_PUBLISH_FLAG,
providerId: PROVIDER_GITHUB,
providerName: "GitHub"
};
publishGithub.publish = function(publishAttributes, title, content, callback) {
githubProvider.publish = function(publishAttributes, title, content, callback) {
var commitMsg = core.settings.commitMsg;
githubHelper.upload(publishAttributes.repository, publishAttributes.branch,
publishAttributes.path, content, commitMsg, callback);
};
publishGithub.init = function(coreModule) {
githubProvider.newPublishAttributes = function(event) {
var publishAttributes = {};
publishAttributes.repository = core.getInputValue($("#input-publish-github-reponame"), event);
publishAttributes.branch = core.getInputValue($("#input-publish-github-branch"), event);
publishAttributes.path = core.getInputValue($("#input-publish-github-path"), event);
if(event.isPropagationStopped()) {
return undefined;
}
return publishAttributes;
};
githubProvider.init = function(coreModule) {
core = coreModule;
};
return publishGithub;
return githubProvider;
});

View File

@ -355,7 +355,8 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
});
};
function handleError(error, asyncTask, callback) {
function handleError(error, asyncTask, callback, serviceName) {
serviceName = serviceName || "Google Drive";
var errorMsg = undefined;
asyncTask.onError = function() {
if (errorMsg !== undefined) {
@ -370,19 +371,19 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
errorMsg = error;
}
else if (error.code >= 500 && error.code < 600) {
errorMsg = "Google Drive is not accessible.";
errorMsg = serviceName + " is not accessible.";
// Retry as described in Google's best practices
asyncTask.retry();
return;
} else if (error.code === 401 || error.code === 403) {
authenticated = false;
errorMsg = "Access to Google Drive is not authorized.";
errorMsg = "Access to " + serviceName + " is not authorized.";
} else if (error.code <= 0) {
connected = false;
authenticated = false;
core.setOffline();
} else {
errorMsg = "Google Drive error (" + error.code + ": "
errorMsg = serviceName + " error (" + error.code + ": "
+ error.message + ").";
}
}
@ -493,28 +494,68 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
});
};
googleHelper.getBlogByUrl = function(url, callback) {
googleHelper.uploadBlogger = function(blogUrl, blogId, postId, title, content, callback) {
authenticate(function() {
if (connected === false) {
callback();
return;
}
var result = undefined;
var asyncTask = {};
asyncTask.run = function() {
var token = gapi.auth.getToken();
var headers = {
Authorization : token ? "Bearer " + token.access_token: null
};
function getBlogId(localCallback) {
$.ajax({
url : "https://www.googleapis.com/blogger/v3/blogs/byurl",
data: { url: url },
data: { url: blogUrl },
headers : headers,
dataType : "json",
timeout : AJAX_TIMEOUT
}).done(function(blog, textStatus, jqXHR) {
result = blog;
blogId = blog.id;
localCallback();
}).fail(function(jqXHR) {
var error = {
code: jqXHR.status,
message: jqXHR.statusText
};
// Handle error
if(error.code === 404) {
error = 'Blog "' + blogUrl + '" not found on Blogger.';
}
handleError(error, asyncTask, callback, "Blogger");
});
}
function publish() {
var url = "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/posts/";
var data = {
kind: "blogger#post",
blog: { id: blogId },
title: title,
content: content
};
var type = "POST";
// If it's an update
if(postId !== undefined) {
url += postId;
data.id = postId;
type = "PUT";
}
$.ajax({
url : url,
data: JSON.stringify(data),
headers : headers,
type: type,
contentType: "application/json",
dataType : "json",
timeout : AJAX_TIMEOUT
}).done(function(post, textStatus, jqXHR) {
postId = post.id;
asyncTask.success();
}).fail(function(jqXHR) {
var error = {
@ -522,11 +563,22 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
message: jqXHR.statusText
};
// Handle error
handleError(error, asyncTask, callback);
if(error.code === 404 && postId !== undefined) {
error = 'Post ' + postId + ' not found on Blogger.';
}
handleError(error, asyncTask, callback, "Blogger");
});
}
if(blogId === undefined) {
getBlogId(publish);
}
else {
publish();
}
};
asyncTask.onSuccess = function() {
callback(result);
callback(blogId, postId);
};
asyncTask.onError = function() {
callback();

View File

@ -1,4 +1,4 @@
define(["jquery", "github-provider", "underscore"], function($) {
define(["jquery", "github-provider", "blogger-provider", "underscore"], function($) {
// Dependencies
var core = undefined;
@ -91,7 +91,7 @@ define(["jquery", "github-provider", "underscore"], function($) {
// Call the provider
var provider = providerMap[publishAttributes.provider];
provider.publish(publishAttributes, publishTitle, content, function(error) {
publishLocation(callback, errorFlag);
publishLocation(callback, errorFlag || error );
});
}
@ -126,21 +126,43 @@ define(["jquery", "github-provider", "underscore"], function($) {
localStorage[fileIndex + ".publish"] += publishIndex + ";";
}
// Initialize the "New publication" dialog
var newLocationProvider = undefined;
function initNewLocation(provider) {
var defaultPublishFormat = provider.defaultPublishFormat || "markdown";
newLocationProvider = provider;
// Show/hide controls depending on provider
$('div[class*=" modal-publish-"]').hide().filter(".modal-publish-" + provider.providerId).show();
// Reset fields
core.resetModalInputs();
$("input:radio[name=radio-publish-format][value=" + defaultPublishFormat + "]").prop("checked", true);
// Open dialog box
$("#modal-publish").modal();
}
// Add a new publish location to a local document
publisher.newLocation = function(publishAttributes) {
function performNewLocation(event) {
var provider = newLocationProvider;
var publishAttributes = provider.newPublishAttributes(event);
if(publishAttributes === undefined) {
return;
}
var fileIndex = fileManager.getCurrentFileIndex();
var title = localStorage[fileIndex + ".title"];
var content = getPublishContent(publishAttributes);
var provider = providerMap[publishAttributes.provider];
provider.publish(publishAttributes, title, content, function(error) {
if(error === undefined) {
publishAttributes.provider = provider.providerId;
createPublishIndex(fileIndex, publishAttributes);
publisher.notifyPublish();
core.showMessage('"' + title
+ '" will now be published on GitHub.');
+ '" is now published on ' + provider.providerName + '.');
}
});
};
}
// Used to populate the "Manage publication" dialog
var lineTemplate = ['<div class="input-prepend input-append">',
@ -162,7 +184,7 @@ define(["jquery", "github-provider", "underscore"], function($) {
_.each(publishIndexList, function(publishIndex) {
var serializedObject = localStorage[publishIndex];
var publishAttributes = JSON.parse(serializedObject);
var publishDesc = JSON.stringify(_.omit(publishAttributes, 'provider')).replace(/{|}|"/g, "");
var publishDesc = JSON.stringify(publishAttributes).replace(/{|}|"/g, "");
lineElement = $(_.template(lineTemplate, {
provider: providerMap[publishAttributes.provider],
publishDesc: publishDesc
@ -178,10 +200,16 @@ define(["jquery", "github-provider", "underscore"], function($) {
core = coreModule;
fileManager = fileManagerModule;
// Init providers
// Init each provider
_.each(providerMap, function(provider) {
provider.init(core);
// Publish provider button
$(".action-publish-" + provider.providerId).click(function() {
initNewLocation(provider);
});
});
$(".action-process-publish").click(performNewLocation);
$(".action-force-publish").click(function() {
if(!$(this).hasClass("disabled")) {