This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 340
/
parseRecipes.js
101 lines (89 loc) · 3.17 KB
/
parseRecipes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var marked = require('marked');
var glob = require('glob');
var fs = require('fs');
var assert = require('assert');
var difficulties = {
'Advanced': 3,
'Intermediate': 2,
'Beginner': 1
};
var categories = module.exports.categories = [
{ title: 'Caching strategies', slug: 'caching-strategies' },
{ title: 'Web Push', slug: 'web-push' },
{ title: 'General Usage', slug: 'general-usage' },
{ title: 'Offline', slug: 'offline' },
{ title: 'Beyond Offline', slug: 'beyond-offline' },
{ title: 'Performance', slug: 'performance' }
];
var categoryOrder = categories.map(function(category) {
return category.title;
});
module.exports.parse = function(recipeSlugs) {
var recipes = recipeSlugs.map(function(recipe) {
var tokens = marked.lexer(fs.readFileSync(recipe + '/README.md', 'utf8'));
assert(tokens.length > 1, recipe + ': README.md must have contents.');
assert(tokens[0].type === 'heading', recipe + ': README.md first token must be header.');
assert(tokens[1].type === 'paragraph', recipe + ': README.md second token must be summary.');
var name = tokens[0].text;
var summary = tokens[1].text;
function getMetadata(type) {
var index;
for (index = 0; index < tokens.length; index++) {
var token = tokens[index];
if (token.type === 'heading' && token.text === type) {
break;
}
}
assert(index !== -1, recipe + ': README.md should contain a ' + type);
assert(index + 1 <= tokens.length, recipe + ': README.md should contain a ' + type);
return tokens[index + 1].text;
}
var difficultyTerm = getMetadata('Difficulty'), difficulty = 0;
if(difficultyTerm in difficulties) {
difficulty = difficulties[difficultyTerm];
} else {
assert(false, recipe + ': Unexpected difficulty value "' + difficultyTerm + '"');
}
var srcs = glob.sync('*.js', { cwd: recipe }).map(function(src) {
var srcName = src.substr(0, src.length - 3);
return {
filename: src,
name: srcName,
ref: recipe + '_' + srcName + '_doc.html'
};
});
var category = getMetadata('Category');
assert(categoryOrder.indexOf(category) !== -1, name + ': Unexpected category value "' + category + '"');
return {
name: name,
summary: summary,
difficulty: difficulty,
difficultyTerm: difficultyTerm,
category: category,
slug: recipe,
srcs: srcs,
demo_ref: recipe + '_demo.html',
demo_index: recipe + '/index.html',
intro_ref: recipe + '.html',
};
});
// Sort categories by category preference, then by difficulty
recipes = recipes.sort(function(recipeA, recipeB) {
// Sort by category
if (categoryOrder.indexOf(recipeA.category) === categoryOrder.indexOf(recipeB.category)) {
// Sort by difficulty
if (recipeA.category === recipeB.category) {
return recipeA.difficulty < recipeB.difficulty ? -1 : 1;
}
if (recipeA.category < recipeB.category) {
return -1;
}
return 1;
} else if (categoryOrder.indexOf(recipeA.category) > categoryOrder.indexOf(recipeB.category)) {
return 1;
} else {
return -1;
}
});
return recipes;
};