Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit 69c9d06065bdbf1ad4e2c59a7bcac8ec7fabdaf2 @mixu committed Oct 2, 2014
@@ -0,0 +1,2 @@
+node_modules/
+tmp/
@@ -0,0 +1,13 @@
+var fs = require('fs'),
+ hbs = require('express-hbs'),
+ getConfig = require('./lib/config-shim'),
+ getHelpers = require('./lib/ghost/helpers/helpers'),
+ path = require('path'),
+ miniq = require('miniq'),
+ through = require('through2'),
+ extend = require('xtend');
+
+exports.Theme = require('./lib/theme');
+exports.Pipe = require('./lib/pipe');
+
+
@@ -0,0 +1,177 @@
+var moment = require('moment'),
+ _ = require('lodash');
+
+// A shim for the Ghost config object, which is a global, which makes rendering
+// with different settings hard given that concurrent renders are possible and should not
+// interact with each other.
+
+module.exports = function getConfig(meta) {
+ var config = {
+ url: meta.blog.url,
+ urlSSL: meta.blog.url,
+
+ paths: {
+ subdir: '', // if the blog is not hosted at the root
+ availableThemes: {
+ casper: {
+ 'package.json': { name: 'Casper', version: '1.0.0' }
+ }
+ }
+ },
+ theme: {
+ url: meta.blog.url.replace(/\/$/, ''),
+ title: meta.blog.title,
+ description: meta.blog.description
+ },
+
+ fileStorage: false,
+ apps: false
+ };
+
+ // ## urlFor
+ // Synchronous url creation for a given context
+ // Can generate a url for a named path, given path, or known object (post)
+ // Determines what sort of context it has been given, and delegates to the correct generation method,
+ // Finally passing to createUrl, to ensure any subdirectory is honoured, and the url is absolute if needed
+ // Usage:
+ // urlFor('home', true) -> http://my-ghost-blog.com/
+ // E.g. /blog/ subdir
+ // urlFor({relativeUrl: '/my-static-page/') -> /blog/my-static-page/
+ // E.g. if post object represents welcome post, and slugs are set to standard
+ // urlFor('post', {...}) -> /welcome-to-ghost/
+ // E.g. if post object represents welcome post, and slugs are set to date
+ // urlFor('post', {...}) -> /2014/01/01/welcome-to-ghost/
+ // Parameters:
+ // - context - a string, or json object describing the context for which you need a url
+ // - data (optional) - a json object containing data needed to generate a url
+ // - absolute (optional, default:false) - boolean whether or not the url should be absolute
+ // This is probably not the right place for this, but it's the best place for now
+ config.urlFor = function urlFor(context, data, absolute) {
+ var urlPath = '/',
+ secure,
+ knownObjects = ['post', 'tag', 'author'],
+
+ // this will become really big
+ knownPaths = {
+ home: '/',
+ rss: '/rss/',
+ api: '/ghost/api/v0.1'
+ };
+
+ // Make data properly optional
+ if (_.isBoolean(data)) {
+ absolute = data;
+ data = null;
+ }
+
+ // Can pass 'secure' flag in either context or data arg
+ secure = (context && context.secure) || (data && data.secure);
+
+ if (_.isObject(context) && context.relativeUrl) {
+ urlPath = context.relativeUrl;
+ } else if (_.isString(context) && _.indexOf(knownObjects, context) !== -1) {
+ // trying to create a url for an object
+ if (context === 'post' && data.post && data.permalinks) {
+ urlPath = urlPathForPost(data.post, data.permalinks);
+ secure = data.post.secure;
+ } else if (context === 'tag' && data.tag) {
+ urlPath = '/tag/' + data.tag.slug + '/';
+ secure = data.tag.secure;
+ } else if (context === 'author' && data.author) {
+ urlPath = '/author/' + data.author.slug + '/';
+ secure = data.author.secure;
+ }
+ // other objects are recognised but not yet supported
+ } else if (_.isString(context) && _.indexOf(_.keys(knownPaths), context) !== -1) {
+ // trying to create a url for a named path
+ urlPath = knownPaths[context] || '/';
+ }
+
+ return createUrl(urlPath, absolute, secure);
+ };
+
+ // ## createUrl
+ // Simple url creation from a given path
+ // Ensures that our urls contain the subdirectory if there is one
+ // And are correctly formatted as either relative or absolute
+ // Usage:
+ // createUrl('/', true) -> http://my-ghost-blog.com/
+ // E.g. /blog/ subdir
+ // createUrl('/welcome-to-ghost/') -> /blog/welcome-to-ghost/
+ // Parameters:
+ // - urlPath - string which must start and end with a slash
+ // - absolute (optional, default:false) - boolean whether or not the url should be absolute
+ // - secure (optional, default:false) - boolean whether or not to use urlSSL or url config
+ // Returns:
+ // - a URL which always ends with a slash
+ function createUrl(urlPath, absolute, secure) {
+ urlPath = urlPath || '/';
+ absolute = absolute || false;
+
+ var output = '', baseUrl;
+
+ // create base of url, always ends without a slash
+ if (absolute) {
+ baseUrl = (secure && config.urlSSL) ? config.urlSSL : config.url;
+ output += baseUrl.replace(/\/$/, '');
+ } else {
+ output += config.paths.subdir;
+ }
+
+ // append the path, always starts and ends with a slash
+ output += urlPath;
+
+ return output;
+ }
+
+ // ## urlPathForPost
+ // Always sync
+ // Creates the url path for a post, given a post and a permalink
+ // Parameters:
+ // - post - a json object representing a post
+ // - permalinks - a json object containing the permalinks setting
+ function urlPathForPost(post, permalinks) {
+ var output = '',
+ tags = {
+ year: function () { return moment(post.published_at).format('YYYY'); },
+ month: function () { return moment(post.published_at).format('MM'); },
+ day: function () { return moment(post.published_at).format('DD'); },
+ slug: function () { return post.slug; },
+ id: function () { return post.id; }
+ };
+
+ if (post.page) {
+ output += '/:slug/';
+ } else {
+ output += permalinks.value;
+ }
+
+ // replace tags like :slug or :year with actual values
+ output = output.replace(/(:[a-z]+)/g, function (match) {
+ if (_.has(tags, match.substr(1))) {
+ return tags[match.substr(1)]();
+ }
+ });
+
+ return output;
+ }
+
+ // ## urlForPost
+ // This method is async as we have to fetch the permalinks
+ // Get the permalink setting and then get a URL for the given post
+ // Parameters
+ // - settings - passed reference to api.settings
+ // - post - a json object representing a post
+ // - absolute (optional, default:false) - boolean whether or not the url should be absolute
+
+ config.urlForPost = function urlForPost(settings, post, absolute) {
+ //return settings.read('permalinks').then(function (response) {
+ // var permalinks = response.settings[0];
+ var permalinks = { key: 'permalinks', value: '/:year/:month/:day/:slug.html' }; // default
+
+ return config.urlFor('post', {post: post, permalinks: permalinks}, absolute);
+ //});
+ };
+
+ return config;
+};
@@ -0,0 +1,26 @@
+var path = require('path'),
+ fs = require('fs'),
+ through = require('through2'),
+ mkdirp = require('mkdirp');
+
+// gulp's dest does too much, as the remapping from input to output is not optional
+// whatever mapping is desired should be up to the user, not built into the file writing logic
+function dest() {
+ var seen = {};
+
+ return through.obj(function (file, enc, onDone) {
+ var writeDir = path.dirname(file.path);
+ (seen[writeDir] ? function(a, onDone) { onDone(null); } : mkdirp)(
+ writeDir, function(err) {
+ if (err) {
+ return onDone(err);
+ }
+ seen[writeDir] = true;
+ fs.writeFileSync(file.path, file.contents);
+ onDone();
+ }
+ );
+ });
+}
+
+module.exports = dest;
@@ -0,0 +1,20 @@
+function isPost(jsonData) {
+ return jsonData.hasOwnProperty('html') && jsonData.hasOwnProperty('markdown') &&
+ jsonData.hasOwnProperty('title') && jsonData.hasOwnProperty('slug');
+}
+
+function isTag(jsonData) {
+ return jsonData.hasOwnProperty('name') && jsonData.hasOwnProperty('slug') &&
+ jsonData.hasOwnProperty('description') && jsonData.hasOwnProperty('parent');
+}
+
+function isUser(jsonData) {
+ return jsonData.hasOwnProperty('bio') && jsonData.hasOwnProperty('website') &&
+ jsonData.hasOwnProperty('status') && jsonData.hasOwnProperty('location');
+}
+
+module.exports.checks = {
+ isPost: isPost,
+ isTag: isTag,
+ isUser: isUser
+};
Oops, something went wrong.

0 comments on commit 69c9d06

Please sign in to comment.