Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 2845dd1

Browse files
matskomhevery
authored andcommitted
feat(ngdocs): added functionality to import and extract contents of external files inside docs comment code
1 parent 0b6f1ce commit 2845dd1

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

docs/src/ngdoc.js

+37
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var htmlEscape = require('./dom.js').htmlEscape;
88
var Example = require('./example.js').Example;
99
var NEW_LINE = /\n\r?/;
1010
var globalID = 0;
11+
var fs = require('fs');
12+
var fspath = require('path');
1113

1214
exports.trim = trim;
1315
exports.metadata = metadata;
@@ -113,6 +115,19 @@ Doc.prototype = {
113115
return id;
114116
}
115117

118+
function extractInlineDocCode(text, tag) {
119+
if(tag == 'all') {
120+
//use a greedy operator to match the last </docs> tag
121+
regex = /\/\/<docs.*?>([.\s\S]+)\/\/<\/docs>/im;
122+
}
123+
else {
124+
//use a non-greedy operator to match the next </docs> tag
125+
regex = new RegExp("\/\/<docs\\s*tag=\"" + tag + "\".*?>([.\\s\\S]+?)\/\/<\/docs>","im");
126+
}
127+
var matches = regex.exec(text.toString());
128+
return matches && matches.length > 1 ? matches[1] : "";
129+
}
130+
116131
parts.forEach(function(text, i) {
117132
parts[i] = (text || '').
118133
replace(/<example(?:\s+module="([^"]*)")?(?:\s+deps="([^"]*)")?>([\s\S]*?)<\/example>/gmi, function(_, module, deps, content) {
@@ -123,8 +138,30 @@ Doc.prototype = {
123138
content.replace(/<file\s+name="([^"]*)"\s*>([\s\S]*?)<\/file>/gmi, function(_, name, content) {
124139
example.addSource(name, content);
125140
});
141+
content.replace(/<file\s+src="([^"]+)"(?:\s+tag="([^"]+)")?(?:\s+name="([^"]+)")?\s*\/?>/gmi, function(_, file, tag, name) {
142+
if(fspath.existsSync(file)) {
143+
var content = fs.readFileSync(file, 'utf8');
144+
if(content && content.length > 0) {
145+
if(tag && tag.length > 0) {
146+
content = extractInlineDocCode(content, tag);
147+
}
148+
name = name && name.length > 0 ? name : fspath.basename(file);
149+
example.addSource(name, content);
150+
}
151+
}
152+
return '';
153+
})
126154
return placeholder(example.toHtml());
127155
}).
156+
replace(/(?:\*\s+)?<file.+?src="([^"]+)"(?:\s+tag="([^"]+)")?\s*\/?>/i, function(_, file, tag) {
157+
if(fspath.existsSync(file)) {
158+
var content = fs.readFileSync(file, 'utf8');
159+
if(tag && tag.length > 0) {
160+
content = extractInlineDocCode(content, tag);
161+
}
162+
return content;
163+
}
164+
}).
128165
replace(/^<doc:example(\s+[^>]*)?>([\s\S]*)<\/doc:example>/mi, function(_, attrs, content) {
129166
var html, script, scenario,
130167
example = new Example(self.scenarios);

src/ng/rootScope.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,7 @@ function $RootScopeProvider(){
8383
*
8484
* Here is a simple scope snippet to show how you can interact with the scope.
8585
* <pre>
86-
angular.injector(['ng']).invoke(function($rootScope) {
87-
var scope = $rootScope.$new();
88-
scope.salutation = 'Hello';
89-
scope.name = 'World';
90-
91-
expect(scope.greeting).toEqual(undefined);
92-
93-
scope.$watch('name', function() {
94-
scope.greeting = scope.salutation + ' ' + scope.name + '!';
95-
}); // initialize the watch
96-
97-
expect(scope.greeting).toEqual(undefined);
98-
scope.name = 'Misko';
99-
// still old value, since watches have not been called yet
100-
expect(scope.greeting).toEqual(undefined);
101-
102-
scope.$digest(); // fire all the watches
103-
expect(scope.greeting).toEqual('Hello Misko!');
104-
});
86+
* <file src="./test/ng/rootScopeSpec.js" tag="docs1" />
10587
* </pre>
10688
*
10789
* # Inheritance

test/ng/rootScopeSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -1114,4 +1114,30 @@ describe('Scope', function() {
11141114
});
11151115
});
11161116
});
1117+
1118+
describe("doc examples", function() {
1119+
1120+
it("should properly fire off watch listeners upon scope changes", inject(function($rootScope) {
1121+
//<docs tag="docs1">
1122+
var scope = $rootScope.$new();
1123+
scope.salutation = 'Hello';
1124+
scope.name = 'World';
1125+
1126+
expect(scope.greeting).toEqual(undefined);
1127+
1128+
scope.$watch('name', function() {
1129+
scope.greeting = scope.salutation + ' ' + scope.name + '!';
1130+
}); // initialize the watch
1131+
1132+
expect(scope.greeting).toEqual(undefined);
1133+
scope.name = 'Misko';
1134+
// still old value, since watches have not been called yet
1135+
expect(scope.greeting).toEqual(undefined);
1136+
1137+
scope.$digest(); // fire all the watches
1138+
expect(scope.greeting).toEqual('Hello Misko!');
1139+
//</docs>
1140+
}));
1141+
1142+
});
11171143
});

0 commit comments

Comments
 (0)