Skip to content

Commit 23e7f63

Browse files
author
Shuwen Qian
committed
Fix docs nav
1 parent cdea532 commit 23e7f63

1 file changed

Lines changed: 66 additions & 8 deletions

File tree

Gulpfile.js

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ gulp.task('sass:docs', function() {
175175
});
176176

177177
gulp.task('docs:templates', function() {
178+
function lookupArticle(list, key) {
179+
for(var i=0; i<list.length; i++) {
180+
if (list[i].key === key) {
181+
return list[i];
182+
}
183+
}
184+
return null;
185+
}
186+
178187
function mergeDocArray(doc, behavior) {
179188
var p = {};
180189
if (!behavior) {
@@ -221,13 +230,13 @@ gulp.task('docs:templates', function() {
221230
}
222231

223232
// Ad-hoc plugin for injecting everything else
224-
function injectDocsMeta(pkg, moduleList, articleList, articleMap) {
233+
function injectModuleMeta(pkg, moduleMap, articleList, articleMap) {
225234
return through.obj(function(file, enc, cb) {
226235
var moduleDoc = JSON.parse(file.contents);
227236

228237
// Inject metadata
229238
moduleDoc.revision = pkg.version;
230-
moduleDoc.modules = moduleList;
239+
moduleDoc.modules = moduleMap;
231240
moduleDoc.articleList = articleList;
232241
moduleDoc.articleMap = articleMap;
233242

@@ -237,11 +246,34 @@ gulp.task('docs:templates', function() {
237246
});
238247
}
239248

249+
function injectArticleMeta(pkg, moduleMap, articleList, articleMap) {
250+
console.log(articleList);
251+
console.log(articleMap);
252+
return through.obj(function(file, enc, cb) {
253+
var articleContents = file.contents.toString('utf8');
254+
var articleDoc = {
255+
contents: articleContents,
256+
revision: pkg.version,
257+
modules: moduleMap,
258+
articleList: articles,
259+
articleMap: articleMap
260+
};
261+
var templatePath = path.join(__dirname,'docs/article_template.html');
262+
var templateString = fs.readFileSync(templatePath).toString('utf8');
263+
var template = hogan.compile(templateString);
264+
var doc = template.render(articleDoc, partialMap);
265+
file.contents = new Buffer(doc, enc);
266+
this.push(file);
267+
cb();
268+
});
269+
}
270+
240271
var pkg = getPkgInfo();
241272

242273
// Create moduleList from directory listing
243274
var modules = glob.sync("mm-*/", {cwd:SRC});
244-
var moduleList = modules.map(function(name) { return name.replace('/','') });
275+
var moduleList = modules.map(function(name) { return name.replace('/',''); });
276+
var moduleMap = moduleList.map(function(name) { return {name: name}; });
245277

246278
// Create behaviorsMap
247279
var behaviors = glob.sync(SRC+'shared/behaviors/*.json');
@@ -256,9 +288,33 @@ gulp.task('docs:templates', function() {
256288
// Create articleList and articleMap
257289
var articles = glob.sync("*.md", {cwd:"./docs/articles/"});
258290
var articleMap = JSON.parse(fs.readFileSync('./docs/articles/manifest.json'));
291+
var articleList = articles.map(function(article) {
292+
var file = fs.readFileSync('./docs/articles/'+article).toString('utf8'),
293+
name = file.split("\n")[0].replace("#",""),
294+
key = path.basename(article, '.md'),
295+
link = "article_" + key + ".html";
296+
return {
297+
key: key,
298+
name: name,
299+
link: link
300+
};
301+
});
302+
articleMap = articleMap.map(function(section) {
303+
var a = lookupArticle(articleList, section.key);
304+
if (a) {
305+
section.link = a.link.trim();
306+
section.name = a.name.trim();
307+
}
308+
if (section.children) {
309+
section.children = section.children.map(function(key) {
310+
return lookupArticle(articleList, key);
311+
});
312+
}
313+
return section;
314+
});
259315

260316
// Compile partials
261-
var partials = glob.sync("*.html", {cwd:"./docs/", ignore: '*_template.html'});
317+
var partials = glob.sync("*.html", {cwd:"./docs/", ignore: '(*_template).html'});
262318
var partialMap = {};
263319
partials.forEach(function(part) {
264320
var name = part.replace('.html','');
@@ -268,6 +324,7 @@ gulp.task('docs:templates', function() {
268324

269325
var indexStream = gulp.src('./docs/index.html')
270326
.pipe(through.obj(function(file, enc, cb) {
327+
// TODO: Put this in a closure
271328
var templateString = file.contents.toString('utf8');
272329
var template = hogan.compile(templateString);
273330
var doc = template.render(null, partialMap);
@@ -279,8 +336,8 @@ gulp.task('docs:templates', function() {
279336

280337
var moduleStream = gulp.src(SRC+'mm-*/doc.json')
281338
.pipe(injectBehaviorDocs(behaviorsMap))
282-
.pipe(injectDocsMeta(pkg, moduleList, articles, articleMap))
283-
.pipe(debug())
339+
.pipe(injectModuleMeta(pkg, moduleMap, articleList, articleMap))
340+
// .pipe(debug())
284341
.pipe(through.obj(function(file, enc, cb) {
285342
// TODO: Put this in a closure
286343
var moduleDoc = JSON.parse(file.contents);
@@ -301,9 +358,10 @@ gulp.task('docs:templates', function() {
301358
.on('error',console.log);
302359

303360
var articleStream = gulp.src('./docs/articles/*.md')
304-
.pipe(debug())
361+
// .pipe(debug())
305362
.pipe(marked().on('error',console.log))
306-
.pipe(wrap({src:"./docs/article_template.html"},{},{engine:"hogan"}).on('error',console.log))
363+
.pipe(injectArticleMeta(pkg, moduleMap, articleList, articleMap))
364+
.pipe(rename({prefix: 'article_'}))
307365
.pipe(gulp.dest(BUILD_DOCS));
308366

309367
return merge(indexStream, moduleStream, articleStream);

0 commit comments

Comments
 (0)