Permalink
Browse files

Add unindent for description fields.

  • Loading branch information...
1 parent 309a467 commit df5535564aaa91bdcfa1e53a544a0be5b0537935 @rottmann rottmann committed Dec 22, 2014
View
@@ -220,19 +220,20 @@ Parser.prototype._parseBlockElements = function(indexApiBlocks, detectedElements
if (attachMethod !== 'insert' && attachMethod !== 'push')
throw new ParserError('Only push or insert are allowed parser method values.', '', '', element.sourceName);
- // Markdown
- // TODO: put this into converters
- if ( values &&
- app.markdown &&
- elementParser.markdownFields &&
- elementParser.markdownFields.length > 0
- ) {
- for (var markdownIndex = 0; markdownIndex < elementParser.markdownFields.length; markdownIndex += 1) {
- var markdownField = elementParser.markdownFields[markdownIndex];
- if (values[markdownField] && app.markdown) {
- values[markdownField] = app.markdown(values[markdownField]);
- // remove line breaks
- values[markdownField] = values[markdownField].replace(/(\r\n|\n|\r)/g, ' ');
+ // TODO: put this into "converters"
+ if (values) {
+ // Markdown
+ if ( app.markdown &&
+ elementParser.markdownFields &&
+ elementParser.markdownFields.length > 0
+ ) {
+ for (var markdownIndex = 0; markdownIndex < elementParser.markdownFields.length; markdownIndex += 1) {
+ var field = elementParser.markdownFields[markdownIndex];
+ if (values[field]) {
+ values[field] = app.markdown(values[field]);
+ // remove line breaks
+ values[field] = values[field].replace(/(\r\n|\n|\r)/g, ' ');
+ }
}
}
}
View
@@ -1,6 +1,7 @@
+var trim = require('../utils/trim');
+
function parse(content) {
- // trim
- content = content.replace(/^\s*|\s*$/g, '');
+ content = trim(content);
// Search: type, url and title
// Example: {get} /user/:id Get User by ID.
@@ -1,3 +1,6 @@
+var trim = require('../utils/trim');
+var unindent = require('../utils/unindent');
+
var ParameterError = require('../errors/parameter_error');
// Additional information for error log
@@ -12,8 +15,7 @@ var _messages = {
function parse(content, source, messages) {
messages = messages || _messages;
- // trim
- content = content.replace(/^\s*|\s*$/g, '');
+ content = trim(content);
var parseRegExp = /^(\w*)(.*?)(?:\s+|$)(.*)$/gm;
var matches = parseRegExp.exec(content);
@@ -40,7 +42,7 @@ function parse(content, source, messages) {
return {
name : name,
title : title,
- description: description
+ description: unindent(description)
};
}
@@ -1,12 +1,14 @@
+var trim = require('../utils/trim');
+var unindent = require('../utils/unindent');
+
function parse(content) {
- // trim
- var description = content.replace(/^\s*|\s*$/g, '');
+ var description = trim(content);
if (description.length === 0)
return null;
return {
- description: description
+ description: unindent(description)
};
}
@@ -1,6 +1,8 @@
+var trim = require('../utils/trim');
+var unindent = require('../utils/unindent');
+
function parse(content, source) {
- // trim
- source = source.replace(/^\s*|\s*$/g, '');
+ source = trim(source);
var title = '';
var text = '';
@@ -27,7 +29,7 @@ function parse(content, source) {
return {
title : title,
- content: text,
+ content: unindent(text),
type : type || 'json'
};
}
@@ -1,6 +1,7 @@
+var trim = require('../utils/trim');
+
function parse(content) {
- // trim
- var group = content.replace(/^\s*|\s*$/g, '');
+ var group = trim(content);
if (group.length === 0)
return null;
@@ -1,12 +1,14 @@
+var trim = require('../utils/trim');
+var unindent = require('../utils/unindent');
+
function parse(content) {
- // trim
- var description = content.replace(/^\s*|\s*$/g, '');
+ var description = trim(content);
if (description.length === 0)
return null;
return {
- groupDescription: description
+ groupDescription: unindent(description)
};
}
@@ -1,6 +1,7 @@
+var trim = require('../utils/trim');
+
function parse(content) {
- // trim
- var name = content.replace(/^\s*|\s*$/g, '');
+ var name = trim(content);
if(name.length === 0)
return null;
@@ -1,3 +1,6 @@
+var trim = require('../utils/trim');
+var unindent = require('../utils/unindent');
+
var group = '';
// Search: group, type, optional, fieldname, defaultValue, size, description
@@ -65,8 +68,7 @@ var allowedValuesWithQuoteRegExp = new RegExp(/\'[^\']*[^\']\'/g);
var allowedValuesRegExp = new RegExp(/[^,\s]+/g);
function parse(content, source, defaultGroup) {
- // trim
- content = content.replace(/^\s*|\s*$/g, '');
+ content = trim(content);
// replace Linebreak with Unicode
content = content.replace(/\n/g, '\uffff');
@@ -110,7 +112,7 @@ function parse(content, source, defaultGroup) {
optional : (matches[5] && matches[5][0] === '[') ? true : false,
field : matches[6],
defaultValue : matches[7] || matches[8] || matches[9],
- description : matches[10] || ''
+ description : unindent(matches[10] || '')
};
}
@@ -1,6 +1,7 @@
+var trim = require('../utils/trim');
+
function parse(content) {
- // trim
- content = content.replace(/^\s*|\s*$/g, '');
+ content = trim(content);
// replace Linebreak with Unicode
content = content.replace(/\n/g, '\uffff');
@@ -1,6 +1,7 @@
+var trim = require('../utils/trim');
+
function parse(content) {
- // trim
- var url = content.replace(/^\s*|\s*$/g, '');
+ var url = trim(content);
if(url.length === 0)
return null;
@@ -1,6 +1,7 @@
+var trim = require('../utils/trim');
+
function parse(content) {
- // trim
- var name = content.replace(/^\s*|\s*$/g, '');
+ var name = trim(content);
if (name.length === 0)
return null;
@@ -1,9 +1,11 @@
var semver = require('semver');
+
+var trim = require('../utils/trim');
+
var ParameterError = require('../errors/parameter_error');
function parse(content) {
- // trim
- content = content.replace(/^\s*|\s*$/g, '');
+ content = trim(content);
if (content.length === 0)
return null;
View
@@ -0,0 +1,9 @@
+/**
+ * Strip whitespace from the beginning and end of a string
+ *
+ * @param str string
+ * @returns string
+ */
+module.exports = function trim(str) {
+ return str.replace(/^\s*|\s*$/g, '');
+};
View
@@ -0,0 +1,35 @@
+/**
+ * Strips from each line any leading whitespace that is shared by all lines.
+ *
+ * @param str string
+ * @returns string
+ */
+module.exports = function unindent(str) {
+ var lines = str.split('\n');
+
+ var xs = lines.filter(function (x) {
+ return /\S/.test(x);
+ }).sort();
+
+ if (xs.length === 0)
+ return str;
+
+ var a = xs[0];
+ var b = xs[xs.length - 1];
+
+ var maxLength = Math.min(a.length, b.length);
+
+ var i = 0;
+ while (i < maxLength &&
+ /\s/.test(a.charAt(i)) &&
+ a.charAt(i) === b.charAt(i)) {
+ i += 1;
+ }
+
+ if (i === 0)
+ return str;
+
+ return lines.map(function (line) {
+ return line.substr(i);
+ }).join('\n');
+};
View
@@ -1,6 +1,6 @@
{
"name": "apidoc-core",
- "version": "0.2.2",
+ "version": "0.3.0",
"description": "Core parser library to generate apidoc result following the apidoc-spec",
"author": "Peter Rottmann <rottmann@inveris.de>",
"license": {
@@ -17,7 +17,7 @@
"url": "https://github.com/apidoc/apidoc-core/issues"
},
"scripts": {
- "test": "npm run jshint && mocha test/",
+ "test": "npm run jshint && mocha test/**/*_test.js",
"jshint": "jshint lib/ test/"
},
"keywords": [
@@ -0,0 +1,64 @@
+/*jshint unused:false*/
+
+/**
+ * Test: Parser apiDescription
+ */
+
+// node modules
+var should = require('should');
+
+// lib modules
+var parser = require('../lib/parsers/api_description');
+
+describe('Parser: apiDescription', function() {
+
+ // TODO: Add 1.000 more possible cases ;-)
+ var testCases = [
+ {
+ title: 'Word only',
+ content: 'Text',
+ expected: {
+ description: 'Text'
+ }
+ },
+ {
+ title: 'Trim single line',
+ content: ' Text line 1 (Begin: 3xSpaces (3 removed), End: 1xSpace). ',
+ expected: {
+ description: 'Text line 1 (Begin: 3xSpaces (3 removed), End: 1xSpace).'
+ }
+ },
+ {
+ title: 'Trim multi line (spaces)',
+ content: ' Text line 1 (Begin: 4xSpaces (3 removed)).\n Text line 2 (Begin: 3xSpaces (3 removed), End: 2xSpaces). ',
+ expected: {
+ description: 'Text line 1 (Begin: 4xSpaces (3 removed)).\n Text line 2 (Begin: 3xSpaces (3 removed), End: 2xSpaces).'
+ }
+ },
+ {
+ title: 'Trim multi line (tabs)',
+ content: '\t\t\tText line 1 (Begin: 3xTab (2 removed)).\n\t\tText line 2 (Begin: 2x Tab (2 removed), End: 1xTab).\t',
+ expected: {
+ description: 'Text line 1 (Begin: 3xTab (2 removed)).\n\t\tText line 2 (Begin: 2x Tab (2 removed), End: 1xTab).'
+ }
+ },
+ {
+ title: 'Trim multi line (tabs and space)',
+ content: '\t Text line 1 (Begin: 1xTab, 2xSpaces).\n Text line 2 (Begin: 3xSpaces, End: 1xTab).\t',
+ expected: {
+ description: 'Text line 1 (Begin: 1xTab, 2xSpaces).\n Text line 2 (Begin: 3xSpaces, End: 1xTab).'
+ }
+ }
+ ];
+
+ // create
+ it('case 1: should pass all regexp test cases', function(done) {
+ testCases.forEach(function(testCase) {
+ var parsed = parser.parse(testCase.content);
+ (parsed !== null).should.equal(true, 'Title: ' + testCase.title + ', Source: ' + testCase.content);
+ parsed.should.eql(testCase.expected);
+ });
+ done();
+ });
+
+});
@@ -0,0 +1,52 @@
+/*jshint unused:false*/
+
+/**
+ * Test: Util unindent
+ */
+
+// node modules
+var should = require('should');
+
+// lib modules
+var unindent = require('../lib/utils/unindent');
+
+describe('Util: unindent', function() {
+
+ it('should strip common leading spaces', function(done) {
+ unindent(' a\n b\n c').should.equal('a\n b\n c');
+ done();
+ });
+
+ it('should strip common leading tabs', function(done) {
+ unindent('\t\ta\n\t\t\t\tb\n\t\t\tc').should.equal('a\n\t\tb\n\tc');
+ done();
+ });
+
+ it('should strip all leading whitespace from a single line', function(done) {
+ unindent(' \t a').should.equal('a');
+ done();
+ });
+
+ it('should not modify the empty string', function(done) {
+ var s = '';
+ unindent(s).should.equal(s);
+ done();
+ });
+
+ it('should not modify if any line starts with non-whitespace', function(done) {
+ var s = ' a\n b\nc d\n e';
+ unindent(s).should.equal(s);
+ done();
+ });
+
+ it('should strip common leading tabs and keep spaces', function(done) {
+ unindent('\ta\n\t b\n\t c').should.equal('a\n b\n c');
+ done();
+ });
+
+ it('should strip common leading tabs and 1 space on each line', function(done) {
+ unindent('\t a\n\t b\n\t c').should.equal(' a\n b\nc');
+ done();
+ });
+
+});
Oops, something went wrong.

0 comments on commit df55355

Please sign in to comment.