build(aio): refactor getExampleRegion into a reusable service

This commit is contained in:
Peter Bacon Darwin 2017-03-23 12:54:29 +00:00 committed by Pete Bacon Darwin
parent c9e51d9911
commit d5cf684d99
4 changed files with 63 additions and 33 deletions

View File

@ -10,6 +10,7 @@ module.exports =
.factory(require('./services/example-map'))
.factory(require('./file-readers/example-reader'))
.factory(require('./services/region-parser'))
.factory(require('./services/getExampleRegion'))
.processor(require('./processors/collect-examples'))

View File

@ -14,7 +14,7 @@ var entities = require('entities');
* @kind function
*/
module.exports = function exampleInlineTagDef(
parseArgString, exampleMap, createDocMessage, log, collectExamples) {
parseArgString, exampleMap, createDocMessage, log, collectExamples, getExampleRegion) {
return {
name: 'example',
description:
@ -22,8 +22,6 @@ module.exports = function exampleInlineTagDef(
handler: function(doc, tagName, tagDescription) {
const EXAMPLES_FOLDERS = collectExamples.exampleFolders;
var tagArgs = parseArgString(entities.decodeHTML(tagDescription));
var unnamedArgs = tagArgs._;
var relativePath = unnamedArgs[0];
@ -33,42 +31,13 @@ module.exports = function exampleInlineTagDef(
var linenums = tagArgs.linenums;
var stylePattern = tagArgs.stylePattern; // TODO: not yet implemented here
const sourceCode = getExampleRegion();
const sourceCode = getExampleRegion(doc, relativePath, regionName);
const attributes = [];
if (title) attributes.push(` title="${title}"`);
if (linenums !== undefined) attributes.push(` linenums="${linenums}"`);
return '<code-example' + attributes.join('') + '>\n' + sourceCode + '\n</code-example>';
function getExampleRegion() {
// Find the example in the folders
var exampleFile;
// Try an "annotated" version first
EXAMPLES_FOLDERS.some(EXAMPLES_FOLDER => { return exampleFile = exampleMap[EXAMPLES_FOLDER][relativePath + '.annotated']; });
// If no annotated version is available then try the actual file
if (!exampleFile) {
EXAMPLES_FOLDERS.some(EXAMPLES_FOLDER => { return exampleFile = exampleMap[EXAMPLES_FOLDER][relativePath]; });
}
// If still no file then we error
if (!exampleFile) {
log.error(createDocMessage('Missing example file... relativePath: "' + relativePath + '".', doc));
log.error('Example files can be found in: ' + EXAMPLES_FOLDERS.join(', '));
return '';
}
var sourceCodeDoc = exampleFile.regions[regionName];
if (!sourceCodeDoc) {
log.error(createDocMessage('Missing example region... relativePath: "' + relativePath + '", region: "' + regionName + '".', doc));
log.error('Regions available are:', Object.keys[exampleFile.regions]);
return '';
}
return sourceCodeDoc.renderedContent;
}
}
};
};

View File

@ -0,0 +1,31 @@
module.exports = function getExampleRegion(exampleMap, createDocMessage, log, collectExamples) {
return function getExampleRegionImpl(doc, relativePath, regionName) {
const EXAMPLES_FOLDERS = collectExamples.exampleFolders;
// Find the example in the folders
var exampleFile;
// Try an "annotated" version first
EXAMPLES_FOLDERS.some(EXAMPLES_FOLDER => { return exampleFile = exampleMap[EXAMPLES_FOLDER][relativePath + '.annotated']; });
// If no annotated version is available then try the actual file
if (!exampleFile) {
EXAMPLES_FOLDERS.some(EXAMPLES_FOLDER => { return exampleFile = exampleMap[EXAMPLES_FOLDER][relativePath]; });
}
// If still no file then we error
if (!exampleFile) {
log.error(createDocMessage('Missing example file... relativePath: "' + relativePath + '".', doc));
log.error('Example files can be found in: ' + EXAMPLES_FOLDERS.join(', '));
return '';
}
var sourceCodeDoc = exampleFile.regions[regionName || ''];
if (!sourceCodeDoc) {
log.error(createDocMessage('Missing example region... relativePath: "' + relativePath + '", region: "' + regionName + '".', doc));
log.error('Regions available are:', Object.keys[exampleFile.regions]);
return '';
}
return sourceCodeDoc.renderedContent;
};
};

View File

@ -0,0 +1,29 @@
var testPackage = require('../../helpers/test-package');
var Dgeni = require('dgeni');
describe('getExampleRegion', () => {
var dgeni, injector, getExampleRegion, collectExamples, exampleMap;
beforeEach(function() {
dgeni = new Dgeni([testPackage('examples-package', true)]);
injector = dgeni.configureInjector();
getExampleRegion = injector.get('getExampleRegion');
collectExamples = injector.get('collectExamples');
exampleMap = injector.get('exampleMap');
collectExamples.exampleFolders = ['examples'];
exampleMap['examples'] = {
'test/url': { regions: {
'': { renderedContent: 'whole file' },
'region-1': { renderedContent: 'region 1 contents' }
} }
};
});
it('should contain the whole contents from the example file if no region is specified', () => {
expect(getExampleRegion({}, 'test/url')).toEqual('whole file');
});
it('should contain the region contents from the example file if a region is specified', () => {
expect(getExampleRegion({}, 'test/url', 'region-1')).toEqual('region 1 contents');
});
});