Permalink
Browse files

Merge branch 'preview'

  • Loading branch information...
dhcole committed Mar 29, 2013
2 parents 2f58c9a + c356d1a commit cc418f9d58ad69dbcee4e942676cb6612b140a54
View
@@ -214,6 +214,22 @@ function getFiles(tree, path, searchstr) {
}
}
+// Load Config
+// -------
+//
+// Load _config.yml
+
+function loadConfig(user, reponame, branch, cb) {
+ var repo = getRepo(user, reponame);
+ repo.read(branch, "_config.yml", function(err, data) {
+ if (err) return cb(err);
+ app.state.jekyll = !err;
+ app.state.config = jsyaml.load(data);
+ cb();
+ });
+}
+
+
// Load Posts
// -------
//
@@ -222,15 +238,8 @@ function getFiles(tree, path, searchstr) {
function loadPosts(user, reponame, branch, path, cb) {
var repo = getRepo(user, reponame);
- function loadConfig(cb) {
- repo.read(branch, "_config.yml", function(err, data) {
- if (err) return cb(err);
- cb(null, jsyaml.load(data));
- });
- }
-
function load(repodata) {
- loadConfig(function(err, config) {
+ loadConfig(user, reponame, branch, function(err, config) {
app.state.jekyll = !err;
app.state.config = config;
@@ -243,6 +252,9 @@ function loadPosts(user, reponame, branch, path, cb) {
if (err) return cb("Branches couldn't be fetched");
app.state.path = path ? path : "";
app.state.branches = _.filter(branches, function(b) { return b !== branch });
+ repo.getSha(branch, app.state.path, function(err, sha) {
+ app.state.sha = sha;
+ });
cb(null, getFiles(tree, path, ""));
});
});
@@ -446,6 +458,7 @@ function loadPost(user, repo, branch, path, file, cb) {
var res = { raw_metadata: "", published: false, writeable: writeable() };
res.content = content.replace(/^(---\n)((.|\n)*?)\n---\n?/, function(match, dashes, frontmatter) {
res.raw_metadata = frontmatter;
+ res.metadata = jsyaml.load(frontmatter);
res.published = published(frontmatter);
return "";
}).trim();
@@ -43,6 +43,10 @@ routers.Application = Backbone.Router.extend({
app.instance.posts(url.user, url.repo, url.branch, url.path);
} else if (url.mode === "new") {
app.instance.newPost(url.user, url.repo, url.branch, url.path);
+ } else if (url.mode === "preview") {
+ var parts = _.extractFilename(url.path);
+ app.state.file = parts[1];
+ app.instance.preview(url.user, url.repo, url.branch, parts[0], parts[1], url.mode);
} else {
var parts = _.extractFilename(url.path);
app.state.file = parts[1];
View
@@ -238,3 +238,73 @@ _.chunkedPath = function(path) {
});
}
+/* Full layout preview */
+
+_.preview = function(view) {
+ var model = view.model,
+ q = queue(1),
+ p = {
+ site: app.state.config,
+ post: model.metadata,
+ page: model.metadata,
+ content: Liquid.parse(marked(model.content)).render({
+ site: app.state.config,
+ post: model.metadata,
+ page: model.metadata
+ }) || ''
+ };
+
+ if (p.site.prose && p.site.prose.site) {
+ _(p.site.prose.site).each(function(file, key) {
+ q.defer(function(cb){
+ $.ajax({
+ cache: true,
+ dataType: 'jsonp',
+ jsonp: false,
+ jsonpCallback: 'callback',
+ url: file,
+ success: function(d) {
+ p.site[key] = d;
+ cb();
+ }
+ });
+ });
+ });
+ }
+
+ q.defer(getLayout);
+ q.await(function() {
+ var content = p.content;
+
+ // Set base URL to public site
+ content = content.replace(/(<head(?:.*)>)/, function() {
+ return arguments[1] + '<base href="' + app.state.config.prose.siteurl + '">';
+ });
+
+ document.write(content);
+ document.close();
+ });
+
+ function getLayout(cb) {
+ var file = p.page.layout;
+
+ model.repo.read(app.state.branch, '_layouts/' + file + '.html', function(err, d) {
+ if (err) return cb(err);
+ var meta = (d.split('---')[1]) ? jsyaml.load(d.split('---')[1]) : {},
+ content = (d.split('---')[2]) ? d.split('---')[2] : d,
+ template = Liquid.parse(content);
+ p.page = _(p.page).extend(meta);
+ p.content = template.render({
+ site: p.site,
+ post: p.post,
+ page: p.page,
+ content: p.content
+ });
+ if (meta.layout) q.defer(getLayout);
+ cb();
+ });
+
+ }
+
+}
+
View
@@ -15,24 +15,26 @@
//
// I'm not proud of this and neither should you be if you were responsible for the XMLHttpRequest spec.
- function _request(method, path, data, cb, raw) {
+ function _request(method, path, data, cb, raw, sync) {
function getURL() {
var url = API_URL + path;
return url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime();
}
var xhr = new XMLHttpRequest();
- if (!raw) {xhr.dataType = "json"}
-
- xhr.open(method, getURL());
- xhr.onreadystatechange = function () {
- if (this.readyState == 4) {
- if (this.status >= 200 && this.status < 300 || this.status === 304) {
- cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true);
- } else {
- cb({request: this, error: this.status});
+ if (!raw) {xhr.dataType = "json";}
+
+ xhr.open(method, getURL(), !sync);
+ if (!sync) {
+ xhr.onreadystatechange = function () {
+ if (this.readyState == 4) {
+ if (this.status >= 200 && this.status < 300 || this.status === 304) {
+ cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true);
+ } else {
+ cb({request: this, error: this.status});
+ }
}
- }
+ };
}
xhr.setRequestHeader('Accept','application/vnd.github.raw');
xhr.setRequestHeader('Content-Type','application/json');
@@ -46,6 +48,7 @@
);
}
data ? xhr.send(JSON.stringify(data)) : xhr.send();
+ if (sync) return xhr.response;
}
// User API
@@ -80,10 +83,12 @@
// -------
this.show = function(username, cb) {
- _request("GET", "/users/"+username, null, function(err, res) {
+ var command = username ? "/users/"+username : "/user";
+
+ _request("GET", command, null, function(err, res) {
cb(err, res);
});
- }
+ };
// List user repositories
// -------
@@ -111,6 +116,24 @@
cb(err, res);
});
};
+
+ // Follow user
+ // -------
+
+ this.follow = function(username, cb) {
+ _request("PUT", "/user/following/"+username, null, function(err, res) {
+ cb(err, res);
+ });
+ };
+
+ // Unfollow user
+ // -------
+
+ this.unfollow = function(username, cb) {
+ _request("DELETE", "/user/following/"+username, null, function(err, res) {
+ cb(err, res);
+ });
+ };
};
@@ -305,8 +328,12 @@
// Get contents
// --------
- this.contents = function(path, cb) {
- _request("GET", repoPath + "/contents", { path: path }, cb);
+ this.contents = function(branch, path, cb) {
+ _request("GET", repoPath + "/contents?ref=" + branch, { path: path }, cb);
+ };
+
+ this.contentsSync = function(branch, path) {
+ return _request("GET", repoPath + "/contents/" + path + "?ref=" + branch, null, null, 'raw', true);
};
// Fork repository
@@ -342,7 +369,7 @@
updateTree(branch, function(err, latestCommit) {
that.getTree(latestCommit+"?recursive=true", function(err, tree) {
// Update Tree
- var newTree = _.reject(tree, function(ref) { return ref.path === path });
+ var newTree = _.reject(tree, function(ref) { return ref.path === path; });
_.each(newTree, function(ref) {
if (ref.type === "tree") delete ref.sha;
});
@@ -406,7 +433,6 @@
Github.Gist = function(options) {
var id = options.id;
- var that = this;
var gistPath = "/gists/"+id;
// Read the gist
@@ -418,6 +444,21 @@
});
};
+ // Create the gist
+ // --------
+ // {
+ // "description": "the description for this gist",
+ // "public": true,
+ // "files": {
+ // "file1.txt": {
+ // "content": "String file contents"
+ // }
+ // }
+ // }
+
+ this.create = function(options, cb){
+ _request("POST","/gists", options, cb);
+ };
// Delete the gist
// --------
Oops, something went wrong.

0 comments on commit cc418f9

Please sign in to comment.