this.files.forEach(function(f) { // Concat specified files. var src = f.src.filter(function(filepath) { // Warn on and remove invalid source files (if nonull was set). if (!grunt.file.exists(filepath)) { grunt.log.warn('Source file "' + filepath + '" not found.'); return false; } else { return true; } }).map(function(filepath) { // Read file source. return grunt.file.read(filepath); }).join('\n'); // parse the comment blocks using DSS dss.parse(src, {}, function(parsed) { // strip the first white space character that just is there after the * parsed.blocks.forEach(function(block) { block.markup.example = block.markup.example.split("\n").map(function(text) { return text.replace(/^\s/, ''); }).join('\n'); }); grunt.file.write(f.dest, JSON.stringify(parsed, null, "\t")); grunt.log.writeln('File "' + f.dest + '" created.'); }); });
it('should allow hyphens in variable names', function(done) { var css = '/**\n * @variable sky-blue - Sky blue\n */\n$sky-blue: #0000FF;\n'; dss.parser('variable', dssParserVariable()); dss.parse(css, {}, function(parsedDss) { expect(parsedDss.blocks[0].variable.name).to.equal('sky-blue'); expect(parsedDss.blocks[0].variable.value).to.equal('#0000FF'); expect(parsedDss.blocks[0].variable.description).to.equal('Sky blue'); done(); }); });
it('should allow space separation of name/description with hyphens in descriptions', function(done) { var css = '/**\n * @variable blue - Sky blue - my favourite colour\n */\n$blue: #0000FF;\n'; dss.parser('variable', dssParserVariable()); dss.parse(css, {}, function(parsedDss) { expect(parsedDss.blocks[0].variable.name).to.equal('blue'); expect(parsedDss.blocks[0].variable.value).to.equal('#0000FF'); expect(parsedDss.blocks[0].variable.description).to.equal('Sky blue - my favourite colour'); done(); }); });
it('should allow name and description to be space separated', function(done) { var css = '/**\n * @variable blue Sky blue\n */\n$blue: #0000FF;\n'; dss.parser('variable', dssParserVariable()); dss.parse(css, {}, function(parsedDss) { expect(parsedDss.blocks[0].variable.name).to.equal('blue'); expect(parsedDss.blocks[0].variable.value).to.equal('#0000FF'); expect(parsedDss.blocks[0].variable.description).to.equal('Sky blue'); done(); }); });
it('should parse LESS @variable syntax', function(done) { var css = '/**\n * @variable blue - Sky blue\n */\n@blue: #0000FF;\n'; dss.parser('variable', dssParserVariable()); dss.parse(css, {}, function(parsedDss) { expect(parsedDss.blocks[0].variable.name).to.equal('blue'); expect(parsedDss.blocks[0].variable.value).to.equal('#0000FF'); expect(parsedDss.blocks[0].variable.description).to.equal('Sky blue'); done(); }); });
it('should ignore @variable if not defined', function(done) { var css = '/**\n * @variable blue - Sky blue\n * @variable red\n*/\n$red: #FF0000;\n'; dss.parser('variable', dssParserVariable()); dss.parse(css, {}, function(parsedDss) { expect(parsedDss.blocks[0].variable.name).to.equal('red'); expect(parsedDss.blocks[0].variable.value).to.equal('#FF0000'); done(); }); });
it('should parse multiples @variable into an array', function(done) { var css = '/**\n * @variable blue - Sky blue\n * @variable red\n*/\n$blue: #0000FF;\n$red: #FF0000;\n'; dss.parser('variable', dssParserVariable()); dss.parse(css, {}, function(parsedDss) { expect(parsedDss.blocks[0].variable[0].name).to.equal('blue'); expect(parsedDss.blocks[0].variable[0].value).to.equal('#0000FF'); expect(parsedDss.blocks[0].variable[0].description).to.equal('Sky blue'); expect(parsedDss.blocks[0].variable[1].name).to.equal('red'); expect(parsedDss.blocks[0].variable[1].value).to.equal('#FF0000'); expect(parsedDss.blocks[0].variable[1].description).to.equal(''); done(); }); });
function bufferContents(file, encoding, callback) { // ignore empty files if (file.isNull()) { callback(); return; } // we don't do streams (yet) if (file.isStream()) { this.emit('error', new PluginError('gulp-dss-styleguide', 'Streaming not supported')); cb(); return; } var temp; dss.parse(file.contents.toString(), {}, function (page) { page.blocks = page.blocks.filter(function (block) { return typeof block['styleguide'] !== 'undefined'; }); if(page.blocks.length > 0) { // Add filename page.file = file.path if(opt.transformFileName) { page.url = opt.transformFileName(page.file) } else { page.url = page.file; } page.url = page.url.replace(file.cwd + '/', '').replace(/\.(css|less|scss|sass)/g, '.html'); page.title = page.url.replace(/\.(css|less|scss|sass|html)/g, '').split('/').filter(function (item) { return item !== ''}).pop().replace(/_/g, ' ').trim(); styleguide.push(page) } callback(null); }); }
it('should support the example in the README', function(done) { var readme = fs.readFileSync('README.md'); var css = readme.toString().match(/```css([^`])+/)[0].split('\n').slice(1).join('\n'); var expected = JSON.parse(readme.toString().match(/```json([^`])+/)[0].split('\n').slice(1).join('\n')); dss.parse(css, {}, function(parsedDss) { expect(parsedDss.blocks.length).to.equal(expected.blocks.length); expect(parsedDss.blocks[0].variable.length).to.equal(expected.blocks[0].variable.length); expect(parsedDss.blocks[0].variable[0].name).to.equal(expected.blocks[0].variable[0].name); expect(parsedDss.blocks[0].variable[0].value).to.equal(expected.blocks[0].variable[0].value); expect(parsedDss.blocks[0].variable[0].description).to.equal(expected.blocks[0].variable[0].description); expect(parsedDss.blocks[0].variable[1].name).to.equal(expected.blocks[0].variable[1].name); expect(parsedDss.blocks[0].variable[1].value).to.equal(expected.blocks[0].variable[1].value); expect(parsedDss.blocks[0].variable[1].description).to.equal(expected.blocks[0].variable[1].description); done(); }); });
files.map(function(filename){ // Report file grunt.verbose.writeln('• ' + grunt.log.wordlist([filename], {color: 'cyan'})); // Parse dss.parse(grunt.file.read(filename), { file: filename }, function(parsed) { // Continue only if file contains DSS annotation if (options.include_empty_files || parsed.blocks.length) { // Add filename parsed['file'] = filename; // Add comment block to styleguide styleguide.push(parsed); } // Check if we're done if (length > 1) { length--; } else { // Set output template and file var template_filepath = template_dir + options.template_index, output_filepath = output_dir + options.output_index; if (!grunt.file.exists(template_filepath)) { grunt.fail.fatal('Cannot read the template file'); } // copy template assets (except index.handlebars) grunt.file.expandMapping([ '**/*', '!' + options.template_index ], output_dir, { cwd: template_dir }).forEach(function(filePair) { filePair.src.forEach(function(src) { if (grunt.file.isDir(src)) { grunt.verbose.writeln('Creating ' + filePair.dest.cyan); grunt.file.mkdir(filePair.dest); } else { grunt.verbose.writeln('Copying ' + src.cyan + ' -> ' + filePair.dest.cyan); grunt.file.copy(src, filePair.dest); } }); }); // Create HTML ouput var html = handlebars.compile(grunt.file.read(template_filepath))({ project: grunt.file.readJSON('package.json'), files: styleguide }); var output_type = 'created', output = null; if (grunt.file.exists(output_filepath)) { output_type = 'overwrited'; output = grunt.file.read(output_filepath); } // avoid write if there is no change if (output !== html) { // Render file grunt.file.write(output_filepath, html); // Report build grunt.log.writeln('✓ Styleguide ' + output_type + ' at: ' + grunt.log.wordlist([output_dir], {color: 'cyan'})); } else { // no change grunt.log.writeln('‣ Styleguide unchanged'); } // Return promise promise(); } }); });
files.forEach(function(filePath){ var fullPath = path.join(specDirPath, filePath); try { var file = fs.readFileSync(fullPath, 'utf-8'); dss.parse(file, {}, function(parsed) { // Normalizing DSS parser output https://github.com/darcyclarke/DSS/issues/58 parsed.blocks.forEach(function(block){ for (var key in block) { var value = block[key]; if (key === 'state' && !util.isArray(value)) { block[key] = [value]; } } }); dataForTemplates.sections = dataForTemplates.sections.concat(parsed.blocks); }); } catch(err) { global.log.debug('DSS error parsing '+ fullPath +':', err); } });