diff options
| author | Igor Minar | 2011-02-07 15:55:23 -0800 |
|---|---|---|
| committer | Igor Minar | 2011-02-07 23:56:51 -0800 |
| commit | d600c608e3bf01be444a9fe25119670d55f7f757 (patch) | |
| tree | e2c045c39a5ec3e4419b02845683ed44aea817ef /docs | |
| parent | 86321d1f5722e0aefcdbe8f3e5a14bd15d4caecb (diff) | |
| download | angular.js-d600c608e3bf01be444a9fe25119670d55f7f757.tar.bz2 | |
markdown pre-processor should strip all the extra indentation
- split trim into trim and indent
- merged my indentation code with trim
- cleaned up some small issues
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/spec/ngdocSpec.js | 33 | ||||
| -rw-r--r-- | docs/src/ngdoc.js | 57 |
2 files changed, 70 insertions, 20 deletions
diff --git a/docs/spec/ngdocSpec.js b/docs/spec/ngdocSpec.js index 95e86d99..35dcc4cb 100644 --- a/docs/spec/ngdocSpec.js +++ b/docs/spec/ngdocSpec.js @@ -143,6 +143,33 @@ describe('ngdoc', function(){ toMatch('</div><h1>One</h1><div'); }); + it('should unindent text before processing based on the second line', function() { + expect(new Doc().markdown('first line\n' + + ' second line\n\n' + + ' third line\n' + + ' fourth line\n\n' + + ' fifth line')). + toMatch('<p>first line\n' + + 'second line</p>\n\n' + + '<pre><code>third line\n' + + ' fourth line\n</code></pre>\n\n' + + '<p>fifth line</p>'); + }); + + it('should unindent text before processing based on the first line', function() { + expect(new Doc().markdown(' first line\n\n' + + ' second line\n' + + ' third line\n' + + ' fourth line\n\n' + + ' fifth line')). + toMatch('<p>first line</p>\n\n' + + '<pre><code>second line\n' + + 'third line\n' + + ' fourth line\n</code></pre>\n\n' + + '<p>fifth line</p>'); + }); + + }); describe('trim', function(){ @@ -163,7 +190,7 @@ describe('ngdoc', function(){ var methodB = new Doc({name:'methodB', methodOf:'angular.service.abc'}); var propA = new Doc({name:'propA', propertyOf:'angular.service.abc'}); var propB = new Doc({name:'propB', propertyOf:'angular.service.abc'}); - ;var docs = [methodB, methodA, propB, propA, parent]; // keep wrong order; + var docs = [methodB, methodA, propB, propA, parent]; // keep wrong order; ngdoc.merge(docs); expect(docs.length).toEqual(1); expect(docs[0].name).toEqual('angular.service.abc'); @@ -185,7 +212,7 @@ describe('ngdoc', function(){ name : 'number', optional: false, 'default' : undefined, - description : '<p>Number \n to format.</p>' }]); + description : '<p>Number \nto format.</p>' }]); }); it('should parse with default and optional', function(){ @@ -267,7 +294,7 @@ describe('ngdoc', function(){ var doc = new Doc("@returns {string} description\n new line\n another line"); doc.parse(); expect(doc.returns). - toEqual({type: 'string', description: '<p>description\n new line\n another line</p>'}); + toEqual({type: 'string', description: '<p>description\nnew line\nanother line</p>'}); }); }); diff --git a/docs/src/ngdoc.js b/docs/src/ngdoc.js index 9ca78dc6..7c4d9d1d 100644 --- a/docs/src/ngdoc.js +++ b/docs/src/ngdoc.js @@ -62,8 +62,10 @@ Doc.prototype = { var IS_URL = /^(https?:\/\/|ftps?:\/\/|mailto:|\.|\/)/; var IS_ANGULAR = /^angular\./; if (!text) return text; - var parts = text.split(/(<pre>[\s\S]*?<\/pre>|<doc:example>[\s\S]*?<\/doc:example>)/), - match; + + text = trim(text); + + var parts = text.split(/(<pre>[\s\S]*?<\/pre>|<doc:example>[\s\S]*?<\/doc:example>)/); parts.forEach(function(text, i){ if (text.match(/^<pre>/)) { @@ -495,7 +497,7 @@ function scenarios(docs){ specs.push(' });'); specs.push(''); doc.scenarios.forEach(function(scenario){ - specs.push(trim(scenario, ' ')); + specs.push(indent(trim(scenario), 2)); specs.push(''); }); specs.push('});'); @@ -564,36 +566,57 @@ function keywordSort(a, b){ ////////////////////////////////////////////////////////// -function trim(text, prefix) { - var MAX = 9999; +function trim(text) { + var MAX_INDENT = 9999; var empty = RegExp.prototype.test.bind(/^\s*$/); var lines = text.split('\n'); - var minIndent = MAX; - prefix = prefix || ''; + var minIndent = MAX_INDENT; + var indentRegExp; + var ignoreLine = (lines[0][0] != ' ' && lines.length > 1); + // ignore first line if it has no indentation and there is more than one line + lines.forEach(function(line){ - minIndent = Math.min(minIndent, indent(line)); + if (ignoreLine) { + ignoreLine = false; + return; + } + + var indent = line.match(/^\s*/)[0].length; + if (indent > 0 || minIndent == MAX_INDENT) { + minIndent = Math.min(minIndent, indent); + } }); + + indentRegExp = new RegExp('^\\s{0,' + minIndent + '}'); + for ( var i = 0; i < lines.length; i++) { - lines[i] = prefix + lines[i].substring(minIndent); + lines[i] = lines[i].replace(indentRegExp, ''); } + // remove leading lines while (empty(lines[0])) { lines.shift(); } + // remove trailing while (empty(lines[lines.length - 1])) { lines.pop(); } return lines.join('\n'); +} - function indent(line) { - for(var i = 0; i < line.length; i++) { - if (line.charAt(i) != ' ') { - return i; - } - } - return MAX; - } +function indent(text, spaceCount) { + var lines = text.split('\n'), + indent = '', + fixedLines = []; + + while(spaceCount--) indent += ' '; + + lines.forEach(function(line) { + fixedLines.push(indent + line); + }); + + return fixedLines.join('\n'); } ////////////////////////////////////////////////////////// |
