From 2845dd159087fc59eb29845a578f32c7c462e8e6 Mon Sep 17 00:00:00 2001 From: Matias Niemelä Date: Tue, 2 Apr 2013 18:20:33 -0400 Subject: feat(ngdocs): added functionality to import and extract contents of external files inside docs comment code --- docs/src/ngdoc.js | 37 +++++++++++++++++++++++++++++++++++++ src/ng/rootScope.js | 20 +------------------- test/ng/rootScopeSpec.js | 26 ++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 19 deletions(-) diff --git a/docs/src/ngdoc.js b/docs/src/ngdoc.js index 2a96abd9..3e1bbe9d 100644 --- a/docs/src/ngdoc.js +++ b/docs/src/ngdoc.js @@ -8,6 +8,8 @@ var htmlEscape = require('./dom.js').htmlEscape; var Example = require('./example.js').Example; var NEW_LINE = /\n\r?/; var globalID = 0; +var fs = require('fs'); +var fspath = require('path'); exports.trim = trim; exports.metadata = metadata; @@ -113,6 +115,19 @@ Doc.prototype = { return id; } + function extractInlineDocCode(text, tag) { + if(tag == 'all') { + //use a greedy operator to match the last tag + regex = /\/\/([.\s\S]+)\/\/<\/docs>/im; + } + else { + //use a non-greedy operator to match the next tag + regex = new RegExp("\/\/([.\\s\\S]+?)\/\/<\/docs>","im"); + } + var matches = regex.exec(text.toString()); + return matches && matches.length > 1 ? matches[1] : ""; + } + parts.forEach(function(text, i) { parts[i] = (text || ''). replace(/([\s\S]*?)<\/example>/gmi, function(_, module, deps, content) { @@ -123,8 +138,30 @@ Doc.prototype = { content.replace(/([\s\S]*?)<\/file>/gmi, function(_, name, content) { example.addSource(name, content); }); + content.replace(//gmi, function(_, file, tag, name) { + if(fspath.existsSync(file)) { + var content = fs.readFileSync(file, 'utf8'); + if(content && content.length > 0) { + if(tag && tag.length > 0) { + content = extractInlineDocCode(content, tag); + } + name = name && name.length > 0 ? name : fspath.basename(file); + example.addSource(name, content); + } + } + return ''; + }) return placeholder(example.toHtml()); }). + replace(/(?:\*\s+)?/i, function(_, file, tag) { + if(fspath.existsSync(file)) { + var content = fs.readFileSync(file, 'utf8'); + if(tag && tag.length > 0) { + content = extractInlineDocCode(content, tag); + } + return content; + } + }). replace(/^]*)?>([\s\S]*)<\/doc:example>/mi, function(_, attrs, content) { var html, script, scenario, example = new Example(self.scenarios); diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 1fad7b0e..2a4bcf76 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -83,25 +83,7 @@ function $RootScopeProvider(){ * * Here is a simple scope snippet to show how you can interact with the scope. *
-        angular.injector(['ng']).invoke(function($rootScope) {
-           var scope = $rootScope.$new();
-           scope.salutation = 'Hello';
-           scope.name = 'World';
-
-           expect(scope.greeting).toEqual(undefined);
-
-           scope.$watch('name', function() {
-             scope.greeting = scope.salutation + ' ' + scope.name + '!';
-           }); // initialize the watch
-
-           expect(scope.greeting).toEqual(undefined);
-           scope.name = 'Misko';
-           // still old value, since watches have not been called yet
-           expect(scope.greeting).toEqual(undefined);
-
-           scope.$digest(); // fire all  the watches
-           expect(scope.greeting).toEqual('Hello Misko!');
-        });
+     * 
      * 
* * # Inheritance diff --git a/test/ng/rootScopeSpec.js b/test/ng/rootScopeSpec.js index f4b6e8a1..cd8d4109 100644 --- a/test/ng/rootScopeSpec.js +++ b/test/ng/rootScopeSpec.js @@ -1114,4 +1114,30 @@ describe('Scope', function() { }); }); }); + + describe("doc examples", function() { + + it("should properly fire off watch listeners upon scope changes", inject(function($rootScope) { +// + var scope = $rootScope.$new(); + scope.salutation = 'Hello'; + scope.name = 'World'; + + expect(scope.greeting).toEqual(undefined); + + scope.$watch('name', function() { + scope.greeting = scope.salutation + ' ' + scope.name + '!'; + }); // initialize the watch + + expect(scope.greeting).toEqual(undefined); + scope.name = 'Misko'; + // still old value, since watches have not been called yet + expect(scope.greeting).toEqual(undefined); + + scope.$digest(); // fire all the watches + expect(scope.greeting).toEqual('Hello Misko!'); +// + })); + + }); }); -- cgit v1.2.3