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
@ -317,8 +319,8 @@
</label> <label class="radio"> <input type="radio"
name="radio-publish-format" value="html"> HTML
</label> <label class="radio"> <input type="radio"
name="radio-publish-format" value="template"> Template
</label>
name="radio-publish-format" value="template"> Template
</label>
</div>
</div>
</div>
@ -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>
@ -380,17 +382,17 @@
</label>
</div>
</div>
<div class="control-group">
<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>
</select>
</div>
</div>
<div class="control-group">
<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>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label"
for="input-settings-editor-font-size">Editor font size</label>
@ -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,16 +498,23 @@
</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>
</dd>
<dd>
<a target="_blank" href="http://underscorejs.org/">Underscore.js</a>
</dd>
<dd>
<a target="_blank" href="http://layout.jquery-dev.net/">UI
Layout</a>
</dd>
<dd>
<a target="_blank" href="http://underscorejs.org/">Underscore.js</a>
</dd>
</dl>
<p>Copyright 2013 <a target="_blank"
href="http://www.benoitschweblin.com">Benoit Schweblin</a><br />

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() {
console.error(error);
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,40 +494,91 @@ 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
};
$.ajax({
url : "https://www.googleapis.com/blogger/v3/blogs/byurl",
data: { url: url },
headers : headers,
dataType : "json",
timeout : AJAX_TIMEOUT
}).done(function(blog, textStatus, jqXHR) {
result = blog;
asyncTask.success();
}).fail(function(jqXHR) {
var error = {
code: jqXHR.status,
message: jqXHR.statusText
function getBlogId(localCallback) {
$.ajax({
url : "https://www.googleapis.com/blogger/v3/blogs/byurl",
data: { url: blogUrl },
headers : headers,
dataType : "json",
timeout : AJAX_TIMEOUT
}).done(function(blog, textStatus, jqXHR) {
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
};
// Handle error
handleError(error, asyncTask, callback);
});
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 = {
code: jqXHR.status,
message: jqXHR.statusText
};
// Handle error
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,11 +200,17 @@ 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")) {
publisher.publish();