style_guide_task.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. module.exports = function(grunt) {
  2. "use strict";
  3. function escapeRegExp(str) {
  4. return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  5. }
  6. function extractColour(line) {
  7. var regex = /\s*:\s*(#[a-fA-F0-9]{3,6})\s*(!default|!default;)?/;
  8. var matches = line.match(regex);
  9. return matches ? matches[1] : matches;
  10. }
  11. function extractVariable(line) {
  12. var matches = line.match(/(\$[0-9a-zA-Z_-]+)\s*(!default|!default;)?/)
  13. return matches ? matches[1] : matches
  14. }
  15. function readVars(file, obj) {
  16. var content = grunt.file.read(file);
  17. var lines = content.split('\n');
  18. lines.forEach(function(line) {
  19. var variable = extractVariable(line);
  20. if (variable) {
  21. var color = extractColour(line, variable);
  22. if (color) {
  23. obj[variable] = color;
  24. }
  25. }
  26. });
  27. }
  28. grunt.registerTask('styleguide', function() {
  29. var data = {
  30. dark: {}, light: {}
  31. };
  32. readVars('public/sass/_variables.dark.scss', data.dark);
  33. readVars('public/sass/_variables.light.scss', data.light);
  34. var styleGuideJson = grunt.config().genDir + '/sass/styleguide.json';
  35. grunt.file.write(styleGuideJson, JSON.stringify(data, null, 4));
  36. });
  37. };