зеркало из https://github.com/telerik/dss.git
feat(parsers): extract multiline parser logic from markup
This commit is contained in:
Родитель
081d1fc62b
Коммит
64b991e3bf
101
dss.js
101
dss.js
|
@ -364,82 +364,87 @@ let dss = ( function() {
|
|||
|
||||
};
|
||||
|
||||
_dss.getMultiLineContent = (i, line, block, file, parserName) => {
|
||||
/* eslint-disable no-param-reassign */
|
||||
// find the next instance of a parser (if there is one based on the @ symbol)
|
||||
// in order to isolate the current multi-line parser
|
||||
let nextParserIndex = block.indexOf('@', i + 1);
|
||||
let markupLength = (nextParserIndex > -1) ? nextParserIndex - i : block.length;
|
||||
let markup = _dss.trim(block.split('').splice(i, markupLength).join(''));
|
||||
let parserMarker = '@' + parserName;
|
||||
|
||||
markup = ((markup) => {
|
||||
let ret = [];
|
||||
let lines = markup.split('\n');
|
||||
|
||||
lines.forEach((line) => {
|
||||
let pattern = '*';
|
||||
let index = line.indexOf(pattern);
|
||||
|
||||
if (index > 0 && index < 10) {
|
||||
line = line.split('').splice((index + pattern.length), line.length).join('');
|
||||
}
|
||||
|
||||
// multiline
|
||||
if (lines.length <= 2) {
|
||||
line = dss.trim( line );
|
||||
}
|
||||
|
||||
if (line && line.indexOf(parserMarker) === -1) {
|
||||
ret.push(line);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
return ret.join('\n');
|
||||
})( markup );
|
||||
/* eslint-enable no-param-reassign */
|
||||
|
||||
return markup;
|
||||
};
|
||||
|
||||
// Return function
|
||||
return _dss;
|
||||
|
||||
})();
|
||||
|
||||
// Describe detection pattern
|
||||
dss.detector( function( line ) {
|
||||
if ( typeof line !== 'string' ) {
|
||||
dss.detector((line) => {
|
||||
if (typeof line !== 'string') {
|
||||
return false;
|
||||
}
|
||||
let reference = line.split( '\n\n' ).pop();
|
||||
return Boolean(reference.match( /.*@/ ));
|
||||
return Boolean(reference.match(/.*@/));
|
||||
});
|
||||
|
||||
// Describe parsing a name
|
||||
dss.parser( 'name', function( i, line, block, file ) { // eslint-disable-line no-unused-vars
|
||||
dss.parser('name', (i, line, block, file) => { // eslint-disable-line no-unused-vars
|
||||
return line;
|
||||
});
|
||||
|
||||
// Describe parsing a description
|
||||
dss.parser( 'description', function( i, line, block, file ) { // eslint-disable-line no-unused-vars
|
||||
dss.parser('description', (i, line, block, file) => { // eslint-disable-line no-unused-vars
|
||||
return line;
|
||||
});
|
||||
|
||||
// Describe parsing a state
|
||||
dss.parser( 'state', function( i, line, block, file ) { // eslint-disable-line no-unused-vars
|
||||
let state = line.split( ' - ' );
|
||||
dss.parser('state', (i, line, block, file) => { // eslint-disable-line no-unused-vars
|
||||
let state = line.split(' - ');
|
||||
|
||||
return [ {
|
||||
name: ( state[ 0 ] ) ? dss.trim( state[ 0 ] ) : '',
|
||||
escaped: ( state[ 0 ] ) ? dss.trim( state[ 0 ].replace( '.', ' ' ).replace( ':', ' pseudo-class-' ) ) : '',
|
||||
description: ( state[ 1 ] ) ? dss.trim( state[ 1 ] ) : ''
|
||||
name: (state[0]) ? dss.trim(state[0]) : '',
|
||||
escaped: (state[0]) ? dss.trim(state[0].replace('.', ' ').replace(':', ' pseudo-class-')) : '',
|
||||
description: (state[1]) ? dss.trim(state[1]) : ''
|
||||
} ];
|
||||
});
|
||||
|
||||
// Describe parsing markup
|
||||
dss.parser( 'markup', function( i, line, block, file, parserName ) {
|
||||
/* eslint-disable no-param-reassign */
|
||||
// find the next instance of a parser (if there is one based on the @ symbol)
|
||||
// in order to isolate the current multi-line parser
|
||||
let nextParserIndex = block.indexOf( '* @', i + 1 );
|
||||
let markupLength = ( nextParserIndex > -1 ) ? nextParserIndex - i : block.length;
|
||||
let markup = block.split( '' ).splice( i, markupLength ).join( '' );
|
||||
let parserMarker = '@' + parserName;
|
||||
|
||||
markup = ( function( markup ) {
|
||||
let ret = [];
|
||||
let lines = markup.split( '\n' );
|
||||
|
||||
lines.forEach( function( line ) {
|
||||
let pattern = '*';
|
||||
let index = line.indexOf( pattern );
|
||||
|
||||
if ( index > 0 && index < 10 ) {
|
||||
line = line.split( '' ).splice( ( index + pattern.length ), line.length ).join( '' );
|
||||
}
|
||||
|
||||
// multiline
|
||||
if ( lines.length <= 2 ) {
|
||||
line = dss.trim( line );
|
||||
}
|
||||
|
||||
if ( line && line.indexOf( parserMarker ) === -1 ) {
|
||||
ret.push( line );
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
return ret.join( '\n' );
|
||||
/* eslint-enable no-param-reassign */
|
||||
})( markup );
|
||||
dss.parser('markup', (i, line, block, file, parserName) => {
|
||||
let markup = dss.getMultiLineContent(i, line, block, file, parserName);
|
||||
|
||||
return {
|
||||
example: markup,
|
||||
escaped: markup.replace( /</g, '<' ).replace( />/g, '>' )
|
||||
escaped: markup.replace(/</g, '<').replace(/>/g, '>')
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
module.exports = dss;
|
||||
|
|
|
@ -7,4 +7,6 @@
|
|||
/// @state .smaller - A smaller button
|
||||
///
|
||||
/// @markup
|
||||
/// <button>This is a button</button>
|
||||
/// <span>
|
||||
/// <button>This is a button</button>
|
||||
/// </span>
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
/// @name Button
|
||||
/// @markup
|
||||
/// <span>
|
||||
/// <button>This is a button</button>
|
||||
/// </span>
|
||||
/// @description Your standard form button.
|
35
test/test.js
35
test/test.js
|
@ -4,6 +4,7 @@ const fs = require('fs');
|
|||
const path = require('path');
|
||||
|
||||
describe('Core tests', function() {
|
||||
|
||||
it('should should parse the data', function() {
|
||||
const file = fs.readFileSync(path.join(__dirname, 'data/button.scss'), 'utf8');
|
||||
|
||||
|
@ -19,8 +20,38 @@ describe('Core tests', function() {
|
|||
assert.strictEqual(data.state[2].name, '.primary');
|
||||
assert.strictEqual(data.state[2].description, 'Indicates button is the primary action.');
|
||||
assert.strictEqual(data.state[3].name, '.smaller');
|
||||
assert.strictEqual(data.markup.example, '<button>This is a button</button>');
|
||||
assert.strictEqual(data.markup.escaped, '<button>This is a button</button>');
|
||||
assert.strictEqual(data.markup.example,
|
||||
' <span>\n' +
|
||||
' <button>This is a button</button>\n' +
|
||||
' </span>'
|
||||
);
|
||||
assert.strictEqual(data.markup.escaped,
|
||||
' <span>\n' +
|
||||
' <button>This is a button</button>\n' +
|
||||
' </span>'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('multiline markup is correctly parsed when not as last parser', function() {
|
||||
const file = fs.readFileSync(path.join(__dirname, 'data/markup-not-last.scss'), 'utf8');
|
||||
|
||||
dss.parse(file, {}, function(parsed) {
|
||||
const data = parsed.blocks[0];
|
||||
|
||||
assert.strictEqual(data.name, 'Button');
|
||||
assert.strictEqual(data.description, 'Your standard form button.');
|
||||
assert.strictEqual(data.markup.example,
|
||||
' <span>\n' +
|
||||
' <button>This is a button</button>\n' +
|
||||
' </span>'
|
||||
);
|
||||
assert.strictEqual(data.markup.escaped,
|
||||
' <span>\n' +
|
||||
' <button>This is a button</button>\n' +
|
||||
' </span>'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче