Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CLeaner models

  • Loading branch information...
commit 8c5ee29718a863d151e5ad528c343619c8cdc429 1 parent 0d2dce2
@craveytrain authored
Showing with 82 additions and 47 deletions.
  1. +8 −4 models/page.js
  2. +11 −5 models/post.js
  3. +63 −38 tasks/render.js
View
12 models/page.js
@@ -9,10 +9,14 @@ marked.aliases = {
module.exports = function(baseModel) {
return function(data) {
- var content = data.__content = data.__content.trim();
+ // trim the whitespace from the content, drop it back in the model
+ var content = data.__content.trim();
- data.body = marked(content);
+ var body = marked(content);
- return extend({}, baseModel, data);
- }
+ return extend({}, baseModel, data, {
+ content: content,
+ body: body
+ });
+ };
};
View
16 models/post.js
@@ -5,11 +5,17 @@ var excerpt = require('../lib/excerpt');
var extend = require('extend');
-module.exports = function(baseModel) {
+module.exports = function(siteModel) {
+ // initialize the page model with the base model content
+ var pageModel = require('./page')(siteModel);
+
return function(data) {
- var pageModel = require('../models/page')(baseModel, data);
- data.excerpt = marked(data.excerpt || excerpt(data.__content));
+ // pass the view specific data into the page model
+ var baseModel = pageModel(data);
- return extend({}, pageModel, data);
- }
+ return extend({}, baseModel, data, {
+ // create excerpt if non exists and mark it down
+ excerpt: marked(baseModel.excerpt || excerpt(baseModel.content))
+ });
+ };
};
View
101 tasks/render.js
@@ -12,7 +12,7 @@ var modelBuilder = require('../models');
var siteModel = grunt.file.readYAML('site.yml');
var models = modelBuilder(siteModel);
-function determinePageType(data, page, path) {
+function determinePageType (data, path) {
// if it's specified, go with it
if (data.pageType) return data.pageType;
@@ -23,23 +23,14 @@ function determinePageType(data, page, path) {
return 'page';
}
-var buildModel = function(page, path) {
- // split file contents into YAML front matter and content
- var data = yamlFront.loadFront(grunt.file.read(page));
- var modelFile;
-
- if (!data.pageType) {
- data.pageType = determinePageType(data, page, path);
- }
-
- modelFile = models[data.pageType] || models.page;
-
- grunt.log.debug('page type:', data.pageType);
+function buildModel (data) {
+ var modelFile = models[data.pageType] || models.page;
+ // build up the model and return it
return modelFile(data);
-};
+}
-var buildPath = function(file) {
+function buildPath (file) {
var baseName = path.basename(file, path.extname(file));
// trim first directory and filename
@@ -53,44 +44,78 @@ var buildPath = function(file) {
grunt.log.debug('path name:', pathName);
return pathName;
-};
+}
-var buildJadeData = function() {
- grunt.config.requires('render.content', 'render.target', 'render.templates');
- var config = grunt.config('render');
- var content = config.content;
- var target = config.target;
- var templates = config.templates;
- var jadeData = grunt.config('jade');
+function sortViews () {
+ grunt.config.requires('render.content');
+ var content = grunt.config('render').content;
+
+ // Blank queue to start
+ var viewQueue = [];
content.forEach(function(glob) {
grunt.file.expand(glob).forEach(function(file) {
grunt.log.debug('file:', file);
- var pathName = buildPath(file);
- var model = buildModel(file, pathName);
+ // define path
+ var path = buildPath(file);
- // Set model data
- jadeData[pathName] = {
- options: {
- data: model
- },
- files: {}
- };
+ // parse the file
+ var data = yamlFront.loadFront(grunt.file.read(file));
- // Set templates and output files
- jadeData[pathName].files[target + pathName + 'index.html'] = templates + path.sep + model.pageType + '.jade';
+ // find out the page type
+ var pageType = data.pageType = determinePageType(data, path);
+ grunt.log.debug('page type:', pageType);
- // // Set canonical url
- jadeData[pathName].options.data.canonical = 'http://' + siteModel.domain + pathName;
+ var view = {
+ pathName: path,
+ data: data
+ };
+ // rudimentary sorting
+ if (pageType === 'post') {
+ viewQueue.unshift(view);
+ } else {
+ viewQueue.push(view);
+ }
});
});
+ // process the queue
+ processQueue(viewQueue);
+}
+
+function processQueue (queue) {
+ grunt.config.requires( 'render.target', 'render.templates');
+ var target = grunt.config('render').target;
+ var templates = grunt.config('render').templates;
+
+ var jadeData = grunt.config('jade');
+
+ queue.forEach(function(view) {
+ var pathName = view.pathName;
+ var model = buildModel(view.data);
+
+ // Set model data
+ jadeData[pathName] = {
+ options: {
+ data: model
+ },
+ files: {}
+ };
+
+ // Set templates and output files
+ jadeData[pathName].files[target + pathName + 'index.html'] = templates + path.sep + model.pageType + '.jade';
+
+ // Set canonical url
+ jadeData[pathName].options.data.canonical = 'http://' + siteModel.domain + pathName;
+ });
+
+ // Store it all in the config for jade
grunt.config('jade', jadeData);
-};
+}
-grunt.registerTask('buildJadeData', 'Parse the markdown and render the markup from the template', buildJadeData);
+grunt.registerTask('buildJadeData', 'Parse the markdown and render the markup from the template', sortViews);
grunt.registerTask('render', ['buildJadeData', 'jade']);
Please sign in to comment.
Something went wrong with that request. Please try again.