1 | 'use strict';
|
2 |
|
3 | var fs = require('fs');
|
4 | var _ = require('lodash');
|
5 |
|
6 | var templates = {
|
7 | ruleSourcePath: _.template('rules/<%= ruleName %>.js'),
|
8 | ruleDocumentationPath: _.template('docs/<%= ruleName %>.md'),
|
9 | ruleExamplesPath: _.template('examples/<%= ruleName %>.js'),
|
10 | styleguide: _.template('[<%= name %> by <%= type %> - <%= description %>](<%= link %>)'),
|
11 | styleguideShort: _.template('[<%= name %>](<%= link %>)'),
|
12 | styleguideLinks: {
|
13 | johnpapa: _.template('https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-<%= name %>')
|
14 | }
|
15 | };
|
16 |
|
17 | var templatesDir = './scripts/templates/';
|
18 | var templateSettings = {
|
19 | imports: {
|
20 | formatStyleguideReference: function(styleRef) {
|
21 | return templates.styleguide(styleguideReferenceTemplateContext(styleRef));
|
22 | },
|
23 | formatStyleguideReferenceListShort: function(rule) {
|
24 | if (!rule.styleguideReferences || rule.styleguideReferences.length === 0) {
|
25 | return '';
|
26 | }
|
27 | return ' (' + rule.styleguideReferences
|
28 | .map(styleguideReferenceTemplateContext)
|
29 | .map(templates.styleguideShort).join(', ') +
|
30 | ')';
|
31 | },
|
32 | formatConfigAsJson: function(examples) {
|
33 | var config = examples[0].displayOptions;
|
34 | if (!config) {
|
35 | return 2;
|
36 | }
|
37 | return JSON.stringify([2].concat(config));
|
38 | },
|
39 | formatConfigAsMarkdown: function(examples) {
|
40 | var config = examples[0].displayOptions;
|
41 | if (!config) {
|
42 | return '';
|
43 | }
|
44 | return '`' + config.map(JSON.stringify).join('` and `') + '`';
|
45 | },
|
46 | indent: function(content, indentation) {
|
47 | var spaces = new Array(indentation + 1).join(' ');
|
48 | return content.replace(/\n/g, '\n' + spaces);
|
49 | }
|
50 | }
|
51 | };
|
52 |
|
53 | fs.readdirSync(templatesDir).forEach(function(templateFilename) {
|
54 | var templateName = templateFilename.split('.')[0];
|
55 | if (templates[templateName] !== undefined) {
|
56 | throw new Error('Can not create from template "' + templateFilename + '" because template key "' +
|
57 | templateName + '" already exists.');
|
58 | }
|
59 |
|
60 | templates[templateName] = _.template(fs.readFileSync(templatesDir + templateFilename), templateSettings);
|
61 | });
|
62 |
|
63 | module.exports = templates;
|
64 |
|
65 | function styleguideReferenceTemplateContext(styleRef) {
|
66 | var linkTemplate = templates.styleguideLinks[styleRef.type];
|
67 | if (!linkTemplate) {
|
68 | throw new Error('No styleguide link template for styleguide type: "' + styleRef.type);
|
69 | }
|
70 | return _.extend({
|
71 | link: linkTemplate(styleRef)
|
72 | }, styleRef);
|
73 | }
|