Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 64ea3b6

Browse files
committed
First pass at build-shred-map complete
1 parent 713a428 commit 64ea3b6

28 files changed

+465
-130
lines changed

gulpfile.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ var watch = require('gulp-watch');
33
var gutil = require('gulp-util');
44
var path = require('path');
55
var del = require('del');
6+
var _ = require('lodash');
67

78
var docShredder = require('./public/doc-shredder/doc-shredder');
89

910
var _shredOptions = {
1011
basePath: path.resolve('./public/docs'),
11-
sourceDir: "_examples",
12-
destDir: "_fragments"
12+
examplesDir: "_examples",
13+
fragmentsDir: "_fragments"
1314
};
1415

1516
/*
@@ -42,19 +43,29 @@ gulp.task('serve-and-watch', function (cb) {
4243
});
4344

4445
gulp.task('shred-full', ['shred-clean'], function() {
45-
docShredder.shred( _shredOptions);
46+
return docShredder.shred( _shredOptions);
4647
});
4748

4849
gulp.task('shred-clean', function(cb) {
49-
var cleanPath = path.join(_shredOptions.basePath, _shredOptions.destDir, '**/*.*')
50+
var cleanPath = path.join(_shredOptions.basePath, _shredOptions.fragmentsDir, '**/*.*')
5051
del([ cleanPath, '!**/*.ovr.*'], function (err, paths) {
5152
// console.log('Deleted files/folders:\n', paths.join('\n'));
5253
cb();
5354
});
5455
});
5556

57+
gulp.task('build-shred-maps', ['shred-full'], function() {
58+
var options = _.extend(_shredOptions, {
59+
jadeDir: '.',
60+
outputDir: '.'
61+
});
62+
return docShredder.buildShredMap(options).then(function(x) {
63+
// var json = x[2];
64+
});
65+
})
66+
5667
function shredWatch(shredOptions, postShredAction) {
57-
var pattern = path.join(shredOptions.basePath, shredOptions.sourceDir, "**/*.*");
68+
var pattern = path.join(shredOptions.basePath, shredOptions.examplesDir, "**/*.*");
5869
watch([pattern], function (event, done) {
5970
console.log('Event type: ' + event.event); // added, changed, or deleted
6071
console.log('Event path: ' + event.path); // The path of the modified file

public/doc-shredder/doc-shredder.js

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,7 @@ var delPromise = Q.denodeify(del);
77
var Dgeni = require('dgeni');
88
var _ = require('lodash');
99

10-
var resolveShredOptions = function(shredOptions) {
11-
return _.defaults({}, shredOptions, {
12-
basePath: path.resolve('.'),
13-
// read files from any subdir under here
14-
sourceDir: "docs/_examples",
15-
// shredded files get copied here with same subdir structure.
16-
destDir: "docs/_fragments",
17-
// whether to include subdirectories when shredding.
18-
includeSubdirs: true
19-
});
20-
}
2110

22-
var resolveMapOptions = function(mapOptions) {
23-
return _.defaults({}, mapOptions, {
24-
basePath: path.resolve('.'),
25-
// read files from any subdir under here
26-
sourceDir: "docs",
27-
destDir: "docs",
28-
// whether to include subdirectories when shredding.
29-
includeSubdirs: true
30-
});
31-
}
3211

3312
var shred = function(shredOptions) {
3413
try {
@@ -42,26 +21,26 @@ var shred = function(shredOptions) {
4221
}
4322

4423
var shredSingleDir = function(shredOptions, filePath) {
45-
shredOptions = resolveOptions(shredOptions);
46-
var root = path.resolve(shredOptions.basePath, shredOptions.sourceDir);
24+
shredOptions = resolveShredOptions(shredOptions);
25+
var root = path.resolve(shredOptions.basePath, shredOptions.examplesDir);
4726
var fileDir = path.dirname(filePath);
4827
var relativePath = path.relative(root, fileDir);
49-
var sourceDir = path.join(shredOptions.sourceDir, relativePath);
50-
var destDir = path.join(shredOptions.destDir, relativePath);
28+
var examplesDir = path.join(shredOptions.examplesDir, relativePath);
29+
var fragmentsDir = path.join(shredOptions.fragmentsDir, relativePath);
5130
var options = {
5231
basePath: shredOptions.basePath,
5332
includeSubdirs: false,
54-
sourceDir: sourceDir,
55-
destDir: destDir
33+
examplesDir: examplesDir,
34+
fragmentsDir: fragmentsDir
5635
}
57-
var cleanPath = path.join(shredOptions.basePath, destDir, '*.*')
36+
var cleanPath = path.join(shredOptions.basePath, fragmentsDir, '*.*')
5837
return delPromise([ cleanPath, '!**/*.ovr.*']).then(function(paths) {
5938
// console.log('Deleted files/folders:\n', paths.join('\n'));
6039
return shred(options);
6140
});
6241
}
6342

64-
var getShredMap = function(shredMapOptions) {
43+
var buildShredMap = function(shredMapOptions) {
6544
try {
6645
var pkg = createShredMapPackage(shredMapOptions);
6746
var dgeni = new Dgeni([ pkg]);
@@ -73,11 +52,11 @@ var getShredMap = function(shredMapOptions) {
7352
}
7453

7554

55+
7656
module.exports = {
7757
shred: shred,
7858
shredSingleDir: shredSingleDir,
79-
resolveShredOptions: resolveShredOptions,
80-
getShredMap: getShredMap
59+
buildShredMap: buildShredMap
8160
};
8261

8362
function createShredPackage(shredOptions) {
@@ -103,28 +82,28 @@ function createShredPackage(shredOptions) {
10382
var extns = ['*.js', '*.html', '*.ts', '*.css' ];
10483
var includeFiles = extns.map(function(extn) {
10584
if (options.includeSubdirs) {
106-
return path.join(options.sourceDir, '**', extn);
85+
return path.join(options.examplesDir, '**', extn);
10786
} else {
108-
return path.join(options.sourceDir, extn);
87+
return path.join(options.examplesDir, extn);
10988
}
11089
});
11190
readFilesProcessor.sourceFiles = [ {
11291
// Process all candidate files in `src` and its subfolders ...
11392
include: includeFiles,
11493
// When calculating the relative path to these files use this as the base path.
11594
// So `src/foo/bar.js` will have relative path of `foo/bar.js`
116-
basePath: options.sourceDir
95+
basePath: options.examplesDir
11796
} ];
11897
})
11998
.config(function(writeFilesProcessor) {
12099
// Specify where the writeFilesProcessor will write our generated doc files
121-
writeFilesProcessor.outputFolder = options.destDir;
100+
writeFilesProcessor.outputFolder = options.fragmentsDir;
122101
});
123102
return pkg;
124103
}
125104

126105
var createShredMapPackage = function(mapOptions) {
127-
var pkg = new Dgeni.Package('docshred-mapper', [
106+
var pkg = new Dgeni.Package('doc-shred-mapper', [
128107
require('dgeni-packages/base'),
129108
require('dgeni-packages/nunjucks')
130109
]);
@@ -133,7 +112,9 @@ var createShredMapPackage = function(mapOptions) {
133112
initializePackage(pkg)
134113
.factory(require('./extractPathsReader'))
135114
.processor(require('./shredMapProcessor'))
136-
115+
.config(function(shredMapProcessor) {
116+
shredMapProcessor.options = options;
117+
})
137118
.config(function(readFilesProcessor, extractPathsReader ) {
138119
readFilesProcessor.fileReaders = [ extractPathsReader];
139120
})
@@ -146,25 +127,25 @@ var createShredMapPackage = function(mapOptions) {
146127
var extns = ['*.jade' ];
147128
var includeFiles = extns.map(function(extn) {
148129
if (options.includeSubdirs) {
149-
return path.join(options.sourceDir, '**', extn);
130+
return path.join(options.jadeDir, '**', extn);
150131
} else {
151-
return path.join(options.sourceDir, extn);
132+
return path.join(options.jadeDir, extn);
152133
}
153134
});
154135
readFilesProcessor.sourceFiles = [ {
155136
// Process all candidate files in `src` and its subfolders ...
156137
include: includeFiles,
157138
// When calculating the relative path to these files use this as the base path.
158139
// So `src/foo/bar.js` will have relative path of `foo/bar.js`
159-
basePath: options.sourceDir
140+
basePath: options.jadeDir
160141
} ];
161142
})
162143
.config(function(writeFilesProcessor) {
163144
// Specify where the writeFilesProcessor will write our generated doc files
164-
writeFilesProcessor.outputFolder = options.destDir;
145+
writeFilesProcessor.outputFolder = options.outputDir;
165146
})
166147
.config(function(templateFinder) {
167-
// Add a folder to search for our own templates to use when rendering docs
148+
// look for templates in this folder
168149
templateFinder.templateFolders = [ path.resolve(__dirname) ];
169150

170151
// Specify how to match docs to templates.
@@ -174,6 +155,7 @@ var createShredMapPackage = function(mapOptions) {
174155
.config(function(computePathsProcessor, computeIdsProcessor) {
175156
computePathsProcessor.$enabled = false;
176157
computeIdsProcessor.$enabled = false;
158+
// Unused for now.
177159
//computePathsProcessor.pathTemplates.push({
178160
// docTypes: ['foo'],
179161
// pathTemplate: '',
@@ -192,6 +174,30 @@ var createShredMapPackage = function(mapOptions) {
192174
return pkg;
193175
}
194176

177+
function resolveShredOptions(shredOptions) {
178+
return _.defaults({}, shredOptions, {
179+
basePath: path.resolve('.'),
180+
// read files from any subdir under here
181+
examplesDir: "docs/_examples",
182+
// shredded files get copied here with same subdir structure.
183+
fragmentsDir: "docs/_fragments",
184+
// whether to include subdirectories when shredding.
185+
includeSubdirs: true
186+
});
187+
}
188+
189+
function resolveMapOptions(mapOptions) {
190+
return _.defaults({}, mapOptions, {
191+
basePath: path.resolve('.'),
192+
// read files from any subdir under here
193+
jadeDir: "docs",
194+
fragmentsDir: "docs/_fragments",
195+
examplesDir: "docs/_examples",
196+
// whether to include subdirectories when shredding.
197+
includeSubdirs: true
198+
});
199+
}
200+
195201
function initializePackage(pkg) {
196202
return pkg
197203
.processor(require('dgeni-packages/base/processors/read-files'))

public/doc-shredder/extractPathsReader.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
/**
2-
* @dgService htmlFileShredder
2+
* @dgService extractPathsReader
33
* @description
44
*/
55

66
var path = require('canonical-path');
77

88
module.exports = function extractPathsReader(log) {
9+
// regex for makeTabs line
910
var rx = /\s*\+makeTabs\(\s*["'](.*?)["']\s*,\s*["'](.*?)["'].*?\)/g
1011
return {
1112
name: 'extractPathsReader',
1213

1314
getDocs: function (fileInfo) {
1415
var content = fileInfo.content;
15-
var refPaths = [];
16+
var fragPaths = [];
1617
var r;
1718
while ((r = rx.exec(content)) !== null) {
1819
var basePath = r[1];
1920
var fileNames = r[2].split(',');
2021
fileNames.forEach(function(fn) {
21-
refPaths.push(path.join(basePath, fn));
22+
fragPaths.push(path.join(basePath, fn.trim()));
2223
})
2324
}
24-
if (refPaths.length) {
25+
if (fragPaths.length) {
2526
return [{
26-
refPaths: refPaths
27+
fragPaths: fragPaths
2728
}];
2829
} else {
2930
return [];

public/doc-shredder/regionExtractor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = function regionExtractor() {
2929
}
3030
});
3131

32+
var rx = new RegExp(nullLine + '\n', 'g');
3233
docs.forEach(function(doc) {
3334
var content;
3435
if (doc.endIx) {
@@ -37,8 +38,7 @@ module.exports = function regionExtractor() {
3738
content = lines.slice(doc.startIx + 1).join('\n');
3839
}
3940
// eliminate all #docregion lines
40-
var rx = new RegExp(nullLine + '\n', 'g');
41-
var content = content.replace(rx, '');
41+
content = content.replace(rx, '');
4242
if (content.substr(-3) === nullLine) {
4343
content = content.substr(0, content.length-3);
4444
}

public/doc-shredder/shredMapProcessor.js

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,53 @@
33
* @description
44
*
55
*/
6+
var path = require('canonical-path');
7+
var fs = require('fs');
8+
69
module.exports = function shredMapProcessor(log) {
710
return {
811
$runAfter: ['readFilesProcessor'],
912
$runBefore: ['rendering-docs'],
13+
1014
$process: function(docs) {
11-
var docMaps = []
15+
var options = this.options;
16+
var jadeToFragMap = {};
17+
var fragToJadeMap = {};
1218
docs.forEach(function(doc) {
13-
var docMap = {
14-
jadePath: doc.fileInfo.filePath,
15-
jadeRelativePath: doc.fileInfo.projectRelativePath,
16-
refPaths: doc.refPaths
17-
}
18-
docMaps.push(docMap);
19+
var jadePath = path.join(options.jadeDir, doc.fileInfo.relativePath);
20+
var fragInfos = doc.fragPaths.map(function(fragPath) {
21+
fragPath = path.join(options.fragmentsDir, fragPath) + '.md';
22+
var fullPath = path.join(options.basePath, fragPath);
23+
var fragInfo = { fragPath: fragPath, exists: fs.existsSync(fullPath) };
24+
if (fragInfo.exists) {
25+
var jadePaths = fragToJadeMap[fragInfo];
26+
if (!jadePaths) {
27+
jadePaths = [];
28+
fragToJadeMap[fragPath] = jadePaths;
29+
}
30+
jadePaths.push(jadePath);
31+
}
32+
return fragInfo;
33+
});
34+
jadeToFragMap[jadePath] = fragInfos;
1935
});
2036
var newDocs = [{
21-
docType: 'xref-doc.html',
22-
docMaps: docMaps,
23-
outputPath: 'xref-doc.html'
37+
docType: 'xref-jade.html',
38+
basePath: this.options.basePath,
39+
jadeToFragMap: jadeToFragMap,
40+
outputPath: 'xref-jade-to-frag.html'
2441
}, {
25-
docType: 'xref-doc.js',
26-
json: JSON.stringify(docMaps),
27-
outputPath: 'xref-doc.js'
42+
docType: 'xref-frag.html',
43+
basePath: this.options.basePath,
44+
fragToJadeMap: fragToJadeMap,
45+
outputPath: 'xref-frag-to-jade.html'
46+
}, {
47+
docType: 'xref-doc.json',
48+
json: JSON.stringify({
49+
basePath: this.options.basePath,
50+
jadeToFragMap: jadeToFragMap,
51+
}, null, 2),
52+
outputPath: 'xref-jade.json'
2853
}]
2954
return newDocs;
3055
}

0 commit comments

Comments
 (0)