diff --git a/build.js b/build.js index 0d52ed6..65112bf 100644 --- a/build.js +++ b/build.js @@ -1,24 +1,26 @@ -var metalsmith = require('metalsmith') -var layouts = require('metalsmith-layouts') -var markdown = require('metalsmith-markdown') -var collections = require('metalsmith-collections') -var pagination = require('metalsmith-pagination') -var paths = require('metalsmith-paths') -var filemetadata = require('metalsmith-filemetadata') -var matters = require('metalsmith-matters') +var metalsmith = require('metalsmith') +var layouts = require('metalsmith-layouts') +var markdown = require('metalsmith-markdown') +var collections = require('metalsmith-collections') +var pagination = require('metalsmith-pagination') +var paths = require('metalsmith-paths') +var filemetadata = require('metalsmith-filemetadata') +var matters = require('metalsmith-matters') -var fs = require('fs'); -var moment = require('moment'); +var fs = require('fs'); +var moment = require('moment'); +var _ = require('underscore'); -var handlebars = require('handlebars'); -var hlayouts = require('handlebars-layouts'); +var handlebars = require('handlebars'); +var handlebars_layout = require('handlebars-layouts'); -handlebars.registerHelper(hlayouts(handlebars)); -handlebars.registerPartial('base', fs.readFileSync('templates/base.hbs', 'utf8')); -handlebars.registerPartial('index', fs.readFileSync('templates/index.hbs', 'utf8')); +moment.locale('fr'); + +//--------------------------- Handlebars -----------------------------// +handlebars.registerHelper(handlebars_layout(handlebars)); handlebars.registerHelper('formatDate', function(date) { - return moment(new Date(date)).format('MMMM D, YYYY HH:mm:ss'); + return moment(new Date(date)).format('DD MMMM YYYY'); }); handlebars.registerHelper('formatTimestamp', function(date) { @@ -54,60 +56,50 @@ function compareWith(v1, v2, operator) { return itm; } -handlebars.registerHelper('eachSorted', function(context, options) { - var ret = ''; - if (context == undefined) { - return ret; +handlebars.registerHelper('eachSorted', function(context, member, item, order, options) { + var tot = ''; + + var entries = _.sortBy(context, function(item){ + return item[member][item] === undefined? 0 : item[member][item]; + }) + + if (order === 'desc') { + entries = entries.reverse(); + } + + for (var i = 0; i < entries.length; i++) { + tot += options.fn(entries[i]); + } + + return tot; +}); + +handlebars.registerHelper('eachCenter', function(context, number, index, options) { + if (number > context.length) { + number = context.length; } - var keyList = Object.keys(context); + var start = index - number / 2; + if (start < 0) { + start = 0; + } - var sortFun = function(a,b) { - a = context[a]; - b = context[b]; - - if (!a && !b) { - return 0; - } - if (!a) { - return -1; - } - if (!b) { - return 1; - } - - a = a.data.title; - b = b.data.title; - - if (b > a) { - return -1; - } - if (a > b) { - return 1; - } - - return 0; - }; + var end = start + number; + if (end > context.length) { + end = context.length; + start = end - number; + } - keyList.sort(sortFun).forEach(function(key) { - if (key !== 'metadata') { - ret += options.fn(context[key]); - } - }); - return ret; -}) - -handlebars.registerHelper('eachCond', function(context, member, operator, cond, options) { var tot = ''; - for (var i = 0; i < context.length; i++) { - var itm = false; - var v1 = context[i][member]; - - - if (compareWith(v1, cond, operator)) { + + for (var i = start; i < end; i++) { + if (i === index) { + tot += options.inverse(context[i]); + } else { tot += options.fn(context[i]); } } + return tot; }); @@ -119,6 +111,7 @@ handlebars.registerHelper('ifCond', function (v1, operator, v2, options) { } }); +//--------------------------- Build pipeline -------------------------// metalsmith(__dirname) .source('recettes') .metadata(require('./config/metadata')) @@ -141,6 +134,10 @@ metalsmith(__dirname) } }) + +//--------------------- Helper functions -----------------------------// + +// Generates an epub file from the list of recettes. function epubGen() { var fs = require('fs'); var epubGenerator = require('epub-generator'); @@ -174,34 +171,9 @@ function epubGen() { return done(); } } - -var recetteSortByTitle = function(a, b) { - a = a.data.title; - b = b.data.title; - - if (!a && !b) { - return 0; - } - if (!a) { - return -1; - } - if (!b) { - return 1; - } - if (b > a) { - return -1; - } - if (a > b) { - return 1; - } - - return 0; -} function createCategories() { - return function(files, metalsmith, done) { - var tagList = {}; for (var fileName in files) { var data = files[fileName]; @@ -238,7 +210,7 @@ function createCategories() { for (var tag in tagList) { var posts = tagList[tag].map(function(fileName) { return files[fileName]; - }).sort(recetteSortByTitle); + }); var tagPage = files[tag + '/index.md']; @@ -259,9 +231,9 @@ function createCategories() { } } +// Adds the category to the recette entry based on the parent directory function addCategory() { return function(files, metalsmith, done) { - for (var f in files) { if (f.indexOf('.md') > 0 && f.indexOf('index.md') < 0) { var idx = f.indexOf('/'); @@ -271,37 +243,12 @@ function addCategory() { files[f].title = files[f].data.title; } } - - return done(); - } -} - -function debugCollection() { - return function(files, metalsmith, done) { - console.log(files); - for (var i in files) { - console.log(i + ' -> ' + ' (' + files[i].collection + ')'); - console.log(files[i]); - } - return done(); - } -} - -function debugMeta(metaname) { - return function(files, metalsmith, done) { - if (metaname !== undefined) { - console.log(metalsmith.metadata()[metaname]); - } else { - console.log(metalsmith.metadata()); - } return done(); } } function copyVendor() { - return function(files, metalsmith, done){ - var styleFiles = fs.readdirSync('styles'); for (var i = 0; i < styleFiles.length; i++) { var stylePath = 'styles/' + styleFiles[i]; @@ -323,4 +270,3 @@ function copyVendor() { return done(); }; } - diff --git a/config/collections.js b/config/collections.js index 862ff7f..e419de8 100644 --- a/config/collections.js +++ b/config/collections.js @@ -1,7 +1,31 @@ module.exports = { recettes: { pattern: '*/*.md', - sortBy: 'title' + sortBy: function(oa, ob) { + a = oa.data.publication; + b = ob.data.publication; + + if (!a && !b) { + return 0; + } + if (!a) { + return -1; + } + if (!b) { + return 1; + } + if (a < b) { + return -1; + } + if (a > b) { + return 1; + } + + ta = oa.data.title; + tb = ob.data.title; + return (ta < tb) ? -1 : 1; + }, + reverse: true }, categories: { pattern: '*/index.md', diff --git a/config/markdown.js b/config/markdown.js index de5df5e..0998bc0 100644 --- a/config/markdown.js +++ b/config/markdown.js @@ -1,32 +1,4 @@ -var highlightjs = require('highlight.js'); - -highlightjs.registerLanguage('procedurelog', function(hljs) { - - return { - case_insensitive: false, - keywords: { - keyword: 'return', - literal: 'true false null undefined NaN Infinity', - built_in: 'macroBreak callTask executeAction createNewPage', - assertSuccess: 'SUCCESS', - assertFail: 'FAIL', - info: 'PROCEDURE ENDPROCEDURE SCRIPT INIT', - action: 'ACTION ASSERT' - }, - contains: [ - hljs.APOS_STRING_MODE, - hljs.QUOTE_STRING_MODE - ] - }; -}); - -var highlighter = function (code) { - var hl = highlightjs.highlightAuto(code, ['procedurelog', 'xml']); - return hl.value; -} - module.exports = { gfm: true, - tables: true, - highlight: highlighter + tables: true } diff --git a/config/metadata.js b/config/metadata.js index e6d12b2..e79f67c 100644 --- a/config/metadata.js +++ b/config/metadata.js @@ -4,5 +4,11 @@ module.exports = { url: 'none', authors: 'Anne-Catherine Portmann, Thomas Schwery', language: 'fr-CH' + }, + partials: { + base: 'partials/base', + recetteList: 'partials/recetteList', + tagList: 'partials/tagList', + paginationList: 'partials/paginationList' } } diff --git a/config/pagination.js b/config/pagination.js index 90191ad..a095d3f 100644 --- a/config/pagination.js +++ b/config/pagination.js @@ -1,11 +1,8 @@ module.exports = { 'collections.recettes': { - perPage: 15, + perPage: 10, first: 'index.html', layout: 'index.hbs', path: 'page/:num/index.html', - groupBy: function(data) { - return data.title.normalize('NFD').charAt(0); - } } } diff --git a/package.json b/package.json index 699aaca..e9e5b38 100644 --- a/package.json +++ b/package.json @@ -3,36 +3,34 @@ "version": "0.1.0", "private": true, "dependencies": { - "bower": "^1.5.2", - "epub-generator": "^1.0.1", - "handlebars": "^3.0.3", - "handlebars-layouts": "^3.1.0", - "highlight.js": "^9.4.0", - "metalsmith": "^1.0.1", - "metalsmith-collections": "^0.7.0", - "metalsmith-collections-paginate": "^2.0.1", - "metalsmith-filemetadata": "^1.0.0", - "metalsmith-layouts": "^1.6.5", - "metalsmith-markdown": "^0.2.1", - "metalsmith-matters": "^1.2.0", - "metalsmith-pagination": "^1.0.0", - "metalsmith-paths": "^2.1.2", - "moment": "^2.6.0", - "serve": "^1.4.0" + "bower": ">=1.5.2", + "epub-generator": ">=1.0.1", + "handlebars": ">=3.0.3", + "handlebars-layouts": ">=3.1.0", + "metalsmith": ">=1.0.1", + "metalsmith-collections": ">=0.7.0", + "metalsmith-collections-paginate": ">=2.0.1", + "metalsmith-filemetadata": ">=1.0.0", + "metalsmith-layouts": ">=1.6.5", + "metalsmith-markdown": ">=0.2.1", + "metalsmith-matters": ">=1.2.0", + "metalsmith-pagination": ">=1.0.0", + "metalsmith-paths": ">=2.1.2", + "metalsmith-permalinks": "^0.5.0", + "moment": ">=2.6.0", + "underscore": "^1.8.3" }, "scripts": { - "serve": "serve build -p ${PORT:-3000}", "build": "node build.js", "watch": "npm-watch" }, "watch": { "build": { - "patterns": ["recettes/*/*.md"], - "extensions": "hbs,md" + "patterns": [ + "recettes/*/*.md" + ], + "extensions": "hbs,md" } }, - "license": "MIT", - "devDependencies": { - "npm-watch": "^0.1.4" - } + "license": "MIT" } diff --git a/templates/index.hbs b/templates/index.hbs index ff59b6d..da2c8e7 100644 --- a/templates/index.hbs +++ b/templates/index.hbs @@ -20,51 +20,22 @@ {{#content "content"}}