UNPKG

@gravityforms/gulp-tasks

Version:
116 lines (89 loc) 2.02 kB
function getGroupHeaders( groups ) { const headers = []; groups.forEach( (group) => { group.forEach( ( lineItem ) => { const parts = lineItem.split(':'); if ( parts.length < 2 ) { return; } headers.push( parts[0].trim() ); }); }); return [ ...new Set( headers ) ]; } function processRows( headers, rows ) { let resp = ''; resp += `<table><thead>` headers.forEach( (header) => { resp += `<th>${header}</th>`; }); resp += `</thead><tbody>`; rows.forEach((row) => { resp += `<tr>`; headers.forEach( (headerType) => { if ( ! row[ headerType ] ) { resp += `<td></td>`; return; } const tdVal = row[ headerType ]; resp += `<td>${tdVal}</td>`; }); resp += `</tr>`; }); resp += `</tbody></table>`; return resp; } function isProp( text ) { const matches = text.match( /^@[a-zA-Z0-9]+prop/ ); if ( ! matches ) { return false; } return true; } function getGroups( lines ) { // setup vars let group = []; const groups = []; let propType = false; // Loop through each line and process. lines.forEach( (line, lineIdx) => { // clean lines lineItem = line.replace(/^\s+|\s+$/g, ''); // group items if ( propType && ! isProp( lineItem ) ) { group.push( lineItem ); } // detect new prop or end of file if ( ( propType && isProp( lineItem ) ) || ( group.length && lineIdx === lines.length -1 ) ) { groups.push( group ); } // detect prop and begin grouping items if ( isProp( lineItem ) ) { propType = lineItem; group = []; return; } }); return groups; } function render( lines ) { const groups = getGroups( lines ); const headers = getGroupHeaders( groups ); const rows = []; groups.forEach( (group) => { const row = {}; group.forEach( (lineItem) => { const parts = lineItem.split(':'); if ( parts.length < 2 ) { return; } row[ parts[0].trim() ] = parts[1].trim(); }); rows.push( row ); }); return processRows( headers, rows ); } module.exports = { render };