From 192183464d89e61e882d9b1deb5f6acb9185b42f Mon Sep 17 00:00:00 2001 From: levym Date: Wed, 29 Jul 2015 18:45:43 +0100 Subject: [PATCH] Add slug as optional page or post variable. --- README.md | 5 +- gulp/tests/compile-pages.spec.js | 304 +++++++++++++++++++++++-------- package.json | 102 +++++------ 3 files changed, 276 insertions(+), 135 deletions(-) diff --git a/README.md b/README.md index e50e6c9..2db661c 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,6 @@ Pages and posts must created in the `src/content/pages` and `src/content/posts` Pages and posts are Markdown files with a YAML front-matter block. The front matter must be the first thing in the file and must take the form of valid YAML set between triple-dashed lines. Here is a basic example: --- - slug: index title: Home template: index.hbs --- @@ -119,9 +118,9 @@ Pages and posts are Markdown files with a YAML front-matter block. The front mat Between these triple-dashed lines, you can set predefined variables (see below). These variables will then be available to you in any Handlebars templates or includes that the page or post in question relies on. -* slug (required) - the URL slug which is used as the directory name when the page or post is built * title (required) - the title of the page or post * template (required) - the Handlebars template to use to compile the page or post +* slug (optional) - the URL slug which is used as the directory name when the page or post is built * date (optional) - used for posts and in the format YYYY-MM-DD * author (optional) - used for posts and the author key in the `site.json` file * status (optional) - set to 'draft' to ignore the page or post when running the generator @@ -161,4 +160,4 @@ Helpers are available to your Handlebar templates and partials, these are: The example site is a good place to start and shows a basic structure of a site with templates and content. -If you encounter any issues or have any feedback, please [send me a tweet](http://twitter.com/ducksoupdev) or raise a bug on the issue tracker. \ No newline at end of file +If you encounter any issues or have any feedback, please [send me a tweet](http://twitter.com/ducksoupdev) or raise a bug on the issue tracker. diff --git a/gulp/tests/compile-pages.spec.js b/gulp/tests/compile-pages.spec.js index e9bed45..75f7e4d 100644 --- a/gulp/tests/compile-pages.spec.js +++ b/gulp/tests/compile-pages.spec.js @@ -1,4 +1,4 @@ -(function () { +(function() { 'use strict'; var compilePages = require('../lib/compile-pages'), @@ -13,7 +13,7 @@ var rootPath = '/tmp/compile-pages'; var doneStub, errorStub; - before(function () { + before(function() { doneStub = sinon.stub(); errorStub = sinon.stub(); @@ -31,57 +31,150 @@ '/build/content', '/build/content/pages', '/build/content/posts' - ].forEach(function (dir) { - if (!fs.existsSync(rootPath + dir)) { - fs.mkdirSync(rootPath + dir); - } - }); + ].forEach(function(dir) { + if (!fs.existsSync(rootPath + dir)) { + fs.mkdirSync(rootPath + dir); + } + }); // set-up files: - fs.writeFileSync(rootPath + '/site.json', '{"title":"Test site"}', {encoding: 'utf8'}); - fs.writeFileSync(rootPath + '/src/templates/page.hbs', '

{{post.title}}

{{{post.body}}}
', {encoding: 'utf8'}); - fs.writeFileSync(rootPath + '/src/templates/post.hbs', '

{{post.title}}

{{{post.body}}}
', {encoding: 'utf8'}); + fs.writeFileSync(rootPath + '/site.json', + '{"title":"Test site"}', { + encoding: 'utf8' + }); + fs.writeFileSync(rootPath + + '/src/templates/page.hbs', + '

{{post.title}}

{{{post.body}}}
', { + encoding: 'utf8' + }); + fs.writeFileSync(rootPath + + '/src/templates/post.hbs', + '

{{post.title}}

{{{post.body}}}
', { + encoding: 'utf8' + }); }); - after(function () { + after(function() { removeDir(rootPath); }); - describe('When compiling a page', function () { - before(function (done) { - fs.writeFileSync(rootPath + '/build/content/pages/test-page.json', '{"slug":"test-page","title":"Test page","template":"page.hbs","body":"

Test page content

"}', {encoding: 'utf8'}); - compilePages.run(rootPath, done, errorStub); + describe('When compiling a page', function() { + before(function(done) { + fs.writeFileSync(rootPath + + '/build/content/pages/test-page.json', + '{"slug":"test-page","title":"Test page","template":"page.hbs","body":"

Test page content

"}', { + encoding: 'utf8' + }); + compilePages.run(rootPath, done, + errorStub); }); - it('Should create the static page', function () { - expect(fs.existsSync(rootPath + '/build/test-page/index.html')).to.be.true; + it('Should create the static page', function() { + expect(fs.existsSync(rootPath + + '/build/test-page/index.html' + )).to.be.true; }); - it('Should have the correct page content', function () { - expect(fs.readFileSync(rootPath + '/build/test-page/index.html', 'utf8')).to.equal('

Test page

Test page content

'); + it('Should have the correct page content', + function() { + expect(fs.readFileSync(rootPath + + '/build/test-page/index.html', + 'utf8')).to.equal( + '

Test page

Test page content

' + ); + }); + }); + + describe('When compiling a page with no slug', function() { + before(function(done) { + fs.writeFileSync(rootPath + + '/build/content/pages/test-page-no-slug.json', + '{"title":"Test page no slug","template":"page.hbs","body":"

Test page content

"}', { + encoding: 'utf8' + }); + compilePages.run(rootPath, done, + errorStub); }); + + it('Should create the static page', function() { + expect(fs.existsSync(rootPath + + '/build/test-page-no-slug/index.html' + )).to.be.true; + }); + + it('Should have the correct page content', + function() { + expect(fs.readFileSync(rootPath + + '/build/test-page-no-slug/index.html', + 'utf8')).to.equal( + '

Test page no slug

Test page content

' + ); + }); }); - describe('When compiling a post', function () { - before(function (done) { - fs.writeFileSync(rootPath + '/build/content/posts/test-post.json', '{"slug":"test-post","title":"Test post","date":"2014-06-11","template":"post.hbs","body":"

Test post content

"}', {encoding: 'utf8'}); - compilePages.run(rootPath, done, errorStub); + describe('When compiling a post', function() { + before(function(done) { + fs.writeFileSync(rootPath + + '/build/content/posts/test-post.json', + '{"slug":"test-post","title":"Test post","date":"2014-06-11","template":"post.hbs","body":"

Test post content

"}', { + encoding: 'utf8' + }); + compilePages.run(rootPath, done, + errorStub); }); - it('Should create the static post', function () { - expect(fs.existsSync(rootPath + '/build/test-post/index.html')).to.be.true; + it('Should create the static post', function() { + expect(fs.existsSync(rootPath + + '/build/test-post/index.html' + )).to.be.true; + }); + + it('Should have the correct post content', + function() { + expect(fs.readFileSync(rootPath + + '/build/test-post/index.html', + 'utf8')).to.equal( + '

Test post

Test post content

' + ); + }); + }); + + describe('When compiling a post with no slug', function() { + before(function(done) { + fs.writeFileSync(rootPath + + '/build/content/posts/test-post-no-slug.json', + '{"title":"Test post no slug","date":"2014-06-11","template":"post.hbs","body":"

Test post content

"}', { + encoding: 'utf8' + }); + compilePages.run(rootPath, done, + errorStub); }); - it('Should have the correct post content', function () { - expect(fs.readFileSync(rootPath + '/build/test-post/index.html', 'utf8')).to.equal('

Test post

Test post content

'); + it('Should create the static post', function() { + expect(fs.existsSync(rootPath + + '/build/test-post-no-slug/index.html' + )).to.be.true; }); + + it('Should have the correct post content', + function() { + expect(fs.readFileSync(rootPath + + '/build/test-post-no-slug/index.html', + 'utf8')).to.equal( + '

Test post no slug

Test post content

' + ); + }); }); describe('When an error occurs with the promises', function() { var promisesListStub, newCompilePages; beforeEach(function(done) { - fs.writeFileSync(rootPath + '/build/content/pages/test-page.json', '{"slug":"test-page","title":"Test page","template":"page.hbs","body":"

Test page content

"}', {encoding: 'utf8'}); + fs.writeFileSync(rootPath + + '/build/content/pages/test-page.json', + '{"slug":"test-page","title":"Test page","template":"page.hbs","body":"

Test page content

"}', { + encoding: 'utf8' + }); mockery.enable({ warnOnReplace: false, @@ -93,20 +186,27 @@ promisesListStub = { filter: function() { - return [Promise.reject('An error occurred')]; + return [Promise.reject( + 'An error occurred' + )]; } }; - mockery.registerMock('../lib/promises', promisesListStub); + mockery.registerMock( + '../lib/promises', + promisesListStub); - newCompilePages = require('../lib/compile-pages'); + newCompilePages = require( + '../lib/compile-pages'); - newCompilePages.run(rootPath, function() { - done(); - }, function(err) { - errorStub(err); - done(); - }); + newCompilePages.run(rootPath, + function() { + done(); + }, + function(err) { + errorStub(err); + done(); + }); }); @@ -115,52 +215,83 @@ }); afterEach(function() { - mockery.deregisterMock('../lib/promises'); + mockery.deregisterMock( + '../lib/promises'); mockery.disable(); }); }); - describe('When compiling posts and excluding draft templates', function () { - before(function (done) { - fs.writeFileSync(rootPath + '/build/content/posts/test-draft-post.json', '{"slug":"test-draft-post","title":"Test draft post","date":"2014-06-11","template":"post.hbs","status":"draft","body":"

Test draft post content

"}', {encoding: 'utf8'}); - compilePages.run(rootPath, done, errorStub); - }); + describe( + 'When compiling posts and excluding draft templates', + function() { + before(function(done) { + fs.writeFileSync(rootPath + + '/build/content/posts/test-draft-post.json', + '{"slug":"test-draft-post","title":"Test draft post","date":"2014-06-11","template":"post.hbs","status":"draft","body":"

Test draft post content

"}', { + encoding: 'utf8' + }); + compilePages.run(rootPath, done, + errorStub); + }); - it('Should not create the static post', function () { - expect(fs.existsSync(rootPath + '/build/test-draft-post/index.html')).to.be.false; + it('Should not create the static post', + function() { + expect(fs.existsSync(rootPath + + '/build/test-draft-post/index.html' + )).to.be.false; + }); }); - }); - describe('When compiling posts and including tags', function () { - before(function (done) { - fs.writeFileSync(rootPath + '/src/templates/post.hbs', '

{{post.title}}

{{{post.body}}}
', {encoding: 'utf8'}); - fs.writeFileSync(rootPath + '/build/content/posts/test-tagged-post.json', '{"slug":"test-tagged-post","title":"Test tagged post","date":"2014-06-11","template":"post.hbs","tags":"tag1 tag2","body":"

Test tagged post content

"}', {encoding: 'utf8'}); - compilePages.run(rootPath, done, errorStub); - }); + describe('When compiling posts and including tags', + function() { + before(function(done) { + fs.writeFileSync(rootPath + + '/src/templates/post.hbs', + '

{{post.title}}

{{{post.body}}}
', { + encoding: 'utf8' + }); + fs.writeFileSync(rootPath + + '/build/content/posts/test-tagged-post.json', + '{"slug":"test-tagged-post","title":"Test tagged post","date":"2014-06-11","template":"post.hbs","tags":"tag1 tag2","body":"

Test tagged post content

"}', { + encoding: 'utf8' + }); + compilePages.run(rootPath, done, + errorStub); + }); - it('Should have the correct post content', function () { - expect(fs.readFileSync(rootPath + '/build/test-tagged-post/index.html', 'utf8')).to.equal('

Test tagged post

Test tagged post content

'); + it('Should have the correct post content', + function() { + expect(fs.readFileSync(rootPath + + '/build/test-tagged-post/index.html', + 'utf8')).to.equal( + '

Test tagged post

Test tagged post content

' + ); + }); }); - }); - describe('When there are no posts or pages to compile', function() { - beforeEach(function(done) { - removeDir(rootPath); - fs.mkdirSync(rootPath); - fs.writeFileSync(rootPath + '/site.json', '{"title":"Test site"}', {encoding: 'utf8'}); - compilePages.run(rootPath, function() { - doneStub(); - done(); - }, function() { - errorStub(); - done(); + describe('When there are no posts or pages to compile', + function() { + beforeEach(function(done) { + removeDir(rootPath); + fs.mkdirSync(rootPath); + fs.writeFileSync(rootPath + + '/site.json', + '{"title":"Test site"}', { + encoding: 'utf8' + }); + compilePages.run(rootPath, function() { + doneStub(); + done(); + }, function() { + errorStub(); + done(); + }); }); - }); - it('Should call done', function() { - expect(doneStub.called).to.be.true; + it('Should call done', function() { + expect(doneStub.called).to.be.true; + }); }); - }); describe('When a glob error occurs', function() { var globStub, newCompilePages; @@ -168,7 +299,11 @@ beforeEach(function(done) { removeDir(rootPath); fs.mkdirSync(rootPath); - fs.writeFileSync(rootPath + '/site.json', '{"title":"Test site"}', {encoding: 'utf8'}); + fs.writeFileSync(rootPath + + '/site.json', + '{"title":"Test site"}', { + encoding: 'utf8' + }); mockery.enable({ warnOnReplace: false, @@ -178,22 +313,27 @@ mockery.deregisterAll(); - globStub = function(paths, options, callback) { + globStub = function(paths, options, + callback) { callback({ message: 'I threw an error' }, null); }; - mockery.registerMock('glob', globStub); + mockery.registerMock('glob', + globStub); - newCompilePages = require('../lib/compile-pages'); + newCompilePages = require( + '../lib/compile-pages'); - newCompilePages.run(rootPath, function() { - done(); - }, function(err) { - errorStub(err); - done(); - }); + newCompilePages.run(rootPath, + function() { + done(); + }, + function(err) { + errorStub(err); + done(); + }); }); it('Should throw a glob error', function() { @@ -201,7 +341,9 @@ }); it('Should throw a specific error', function() { - expect(errorStub.calledWith({ message: 'I threw an error' })).to.be.true; + expect(errorStub.calledWith({ + message: 'I threw an error' + })).to.be.true; }); afterEach(function() { diff --git a/package.json b/package.json index 2a70f53..36ce086 100644 --- a/package.json +++ b/package.json @@ -1,53 +1,53 @@ { - "name": "gulp-site-generator", - "version": "0.1.12", - "description": "Static site generator using Gulp", - "main": "gulpfile.js", - "scripts": { - "test": "gulp test" - }, - "keywords": [ - "gulp", - "static", - "generator" - ], - "author": "Matt Levy ", - "license": "ISC", - "dependencies": { - "bluebird": "^2.9.33", - "downsize": "0.0.8", - "glob": "^5.0.13", - "globby": "^2.1.0", - "gulp": "^3.9.0", - "gulp-compile-handlebars": "^0.5.0", - "gulp-concat": "^2.6.0", - "gulp-connect": "^2.2.0", - "gulp-if": "^1.2.5", - "gulp-markdown-to-json": "^0.2.1", - "gulp-minify-css": "^1.2.0", - "gulp-minify-html": "^1.0.3", - "gulp-rename": "^1.2.2", - "gulp-sass": "^2.0.3", - "gulp-uglify": "^1.2.0", - "gulp-uncss": "^1.0.2", - "handlebars": "^3.0.3", - "imagemin-jpegoptim": "^4.0.0", - "imagemin-optipng": "^4.3.0", - "imagemin-pngquant": "^4.1.0", - "lodash": "^3.10.0", - "minimist": "^1.1.1", - "moment": "^2.10.3", - "mout": "^0.11.0", - "require-dir": "^0.3.0", - "rss": "^1.1.1" - }, - "devDependencies": { - "chai": "^3.0.0", - "gulp-coveralls": "^0.1.4", - "gulp-istanbul": "^0.10.0", - "gulp-mocha": "^2.1.2", - "mocha": "^2.2.5", - "mockery": "^1.4.0", - "sinon": "^1.15.4" - } + "name": "gulp-site-generator", + "version": "0.1.12", + "description": "Static site generator using Gulp", + "main": "gulpfile.js", + "scripts": { + "test": "gulp test" + }, + "keywords": [ + "gulp", + "static", + "generator" + ], + "author": "Matt Levy ", + "license": "ISC", + "dependencies": { + "bluebird": "^2.9.33", + "downsize": "0.0.8", + "glob": "^5.0.13", + "globby": "^2.1.0", + "gulp": "^3.9.0", + "gulp-compile-handlebars": "^0.5.0", + "gulp-concat": "^2.6.0", + "gulp-connect": "^2.2.0", + "gulp-if": "^1.2.5", + "gulp-markdown-to-json": "^0.2.1", + "gulp-minify-css": "^1.2.0", + "gulp-minify-html": "^1.0.3", + "gulp-rename": "^1.2.2", + "gulp-sass": "^2.0.3", + "gulp-uglify": "^1.2.0", + "gulp-uncss": "^1.0.2", + "handlebars": "^3.0.3", + "imagemin-jpegoptim": "^4.0.0", + "imagemin-optipng": "^4.3.0", + "imagemin-pngquant": "^4.1.0", + "lodash": "^3.10.0", + "minimist": "^1.1.1", + "moment": "^2.10.3", + "mout": "^0.11.0", + "require-dir": "^0.3.0", + "rss": "^1.1.1" + }, + "devDependencies": { + "chai": "^3.0.0", + "gulp-coveralls": "^0.1.4", + "gulp-istanbul": "^0.10.0", + "gulp-mocha": "^2.1.2", + "mocha": "^2.2.5", + "mockery": "^1.4.0", + "sinon": "^1.15.4" + } }