Update build script, template organization, cleanup of the general configs

This commit is contained in:
Thomas Schwery 2016-11-12 15:06:07 +01:00
parent 4dffc26b9c
commit eb6a17ff03
13 changed files with 191 additions and 226 deletions

154
build.js
View file

@ -9,16 +9,18 @@ var matters = require('metalsmith-matters')
var fs = require('fs');
var moment = require('moment');
var _ = require('underscore');
var handlebars = require('handlebars');
var hlayouts = require('handlebars-layouts');
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 keyList = Object.keys(context);
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;
};
keyList.sort(sortFun).forEach(function(key) {
if (key !== 'metadata') {
ret += options.fn(context[key]);
}
});
return ret;
var entries = _.sortBy(context, function(item){
return item[member][item] === undefined? 0 : item[member][item];
})
handlebars.registerHelper('eachCond', function(context, member, operator, cond, options) {
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 start = index - number / 2;
if (start < 0) {
start = 0;
}
var end = start + number;
if (end > context.length) {
end = context.length;
start = end - number;
}
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');
@ -175,33 +172,8 @@ function epubGen() {
}
}
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();
};
}

View file

@ -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',

View file

@ -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
}

View file

@ -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'
}
}

View file

@ -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);
}
}
}

View file

@ -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"],
"patterns": [
"recettes/*/*.md"
],
"extensions": "hbs,md"
}
},
"license": "MIT",
"devDependencies": {
"npm-watch": "^0.1.4"
}
"license": "MIT"
}

View file

@ -20,51 +20,22 @@
{{#content "content"}}
<div class="content row">
<div class="list-recette list col-sm-8">
{{#eachSorted pagination.files}}
<article class="content">
<header class="header">
<h3 class="list-content-title">
<a href="{{ path.href }}">
{{#each pagination.files }}
{{#ifCond type '==' 'recette'}}
<span class="glyphicon glyphicon-apple"></span>
{{else}}
<span class="glyphicon glyphicon-list"></span>
{{> recetteList }}
{{/ifCond}}
{{ data.title }}
</a>
</h3>
</header>
<section class="content-article">
{{{ data.description }}}
</section>
</article>
{{/ eachSorted}}
{{/ each}}
</div>
<div class="list-tags list col-sm-4">
<ul class="list-group">
{{#each tags}}
<a href="/{{ @key }}/" class="list-group-item {{#ifCond @key '==' ../tag }}active{{/ifCond }}">
<span class="badge">{{ posts.length }}</span>
{{ data.title }}
</a>
{{> tagList currentTag=../tag}}
{{/each}}
</ul>
</div>
</div>
<nav class="navigation cf">
{{#if pagination.previous }}
<a href="{{ pagination.previous.path.href }}" class="btn pull-right">
<i class="glyphicon glyphicon-chevron-right">
</i>Newer&nbsp;&nbsp;</a>
{{/ if}}
{{#each pagination.pages}}
<a href="{{ path.href }}" class="btn">{{ pagination.name }}</a>
{{/each}}
{{#if pagination.next }}
<a href="{{ pagination.next.path.href }}" class="btn pull-left">
<i class="glyphicon glyphicon-chevron-left">
</i>&nbsp;&nbsp;Older</a>
{{/ if}}
{{> paginationList currentPage=pagination.index }}
</nav>
{{/ content}}

View file

@ -0,0 +1,29 @@
{{#if pagination.previous }}
<a href="{{ pagination.previous.path.href }}" class="btn pull-left">
<i class="glyphicon glyphicon-chevron-left"></i>
&nbsp;&nbsp;Newer
</a>
{{else }}
<a href="{{ pagination.previous.path.href }}" class="btn pull-left disabled">
<i class="glyphicon glyphicon-chevron-left"></i>
&nbsp;&nbsp;Newer
</a>
{{/ if}}
{{#eachCenter pagination.pages 10 currentPage}}
<a href="{{ path.href }}" class="btn">{{ pagination.name }}</a>
{{else }}
<a href="{{ path.href }}" class="btn disabled btn-primary">{{ pagination.name }}</a>
{{/eachCenter}}
{{#if pagination.next }}
<a href="{{ pagination.next.path.href }}" class="btn pull-right">
Older&nbsp;&nbsp;
<i class="glyphicon glyphicon-chevron-right"></i>
</a>
{{else }}
<a href="{{ pagination.next.path.href }}" class="btn pull-right disabled">
Older&nbsp;&nbsp;
<i class="glyphicon glyphicon-chevron-right"></i>
</a>
{{/ if}}

View file

@ -0,0 +1,30 @@
<article class="content">
<header class="header">
<div class="header-container row">
<h3 class="list-content-title">
<a href="{{ path.href }}">
<span class="glyphicon glyphicon-apple"></span>
{{ data.title }}
</a>
<small class="header-metadata">
<time datetime="{{ data.publication }}" class="timestamp" title="{{formatDate data.publication }}">{{formatDate data.publication }}</time>
{{ data.time }}
</small>
</h3>
</div>
</header>
<section class="content-article">
<div class="header-container row">
<div class="col-sm-4">
{{{ data.description }}}
</div>
<div class="col-sm-4">
{{#if data.image }}
<div class="recipe-image">
<img src="/{{ category }}/{{ data.image }}" />
</div>
{{/if }}
</div>
</div>
</section>
</article>

View file

@ -0,0 +1,4 @@
<a href="/{{ @key }}/" class="list-group-item {{#ifCond @key '==' currentTag }}active{{/ifCond }}">
<span class="badge">{{ posts.length }}</span>
{{ data.title }}
</a>

View file

@ -51,7 +51,7 @@
{{/each }}
{{#if data.image }}
<div class="recipe-image">
<img src="{{ data.image }}" />
<img src="/{{ category }}/{{ data.image }}" />
</div>
{{/if }}

View file

@ -1,4 +1,4 @@
{{#extend "index"}}
{{#extend "base"}}
{{#content "title"}}
Recettes • {{ title }}
@ -11,26 +11,14 @@
{{{ contents }}}
{{/if }}
<h2>Recettes</h2>
{{#each posts}}
<article class="content">
<header class="header">
<h3 class="list-content-title">
<a href="{{ path.href }}"><span class="glyphicon glyphicon-apple"></span>{{ data.title }}</a>
</h3>
</header>
<section class="content-article">
{{{ data.description }}}
</section>
</article>
{{/ each}}
{{#eachSorted posts 'data' 'title' 'asc' }}
{{> recetteList }}
{{/ eachSorted}}
</div>
<div class="list-tags list col-sm-4">
<ul class="list-group">
{{#each tags}}
<a href="/{{ @key }}/" class="list-group-item {{#ifCond @key '==' ../tag }}active{{/ifCond }}">
<span class="badge">{{ posts.length }}</span>
{{ data.title }}
</a>
{{> tagList currentTag=../tag}}
{{/each}}
</ul>
</div>