parse.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. const fs = require('fs');
  3. console.log('starting');
  4. const inFile = 'WiFiManager.template.html';
  5. const outFile = 'template.h';
  6. const defineRegEx = /<!-- ([A-Z_]+) -->/gm;
  7. console.log('parsing', inFile);
  8. fs.readFile(inFile, 'utf8', function (err,data) {
  9. if (err) {
  10. return console.log(err);
  11. }
  12. //console.log(data);
  13. let defines = data.match(defineRegEx);
  14. //console.log(defines);
  15. var stream = fs.createWriteStream(outFile);
  16. stream.once('open', function(fd) {
  17. for (const i in defines) {
  18. const start = defines[i];
  19. const end = start.replace('<!-- ', '<!-- /')
  20. defineRegEx.lastIndex = 0;
  21. const constantName = defineRegEx.exec(start)[1];
  22. console.log(constantName);
  23. var extractRE = new RegExp(start + '([\\s\\S]+)' + end, 'gm');
  24. let extractArray = extractRE.exec(data);
  25. if(extractArray.length > 1) {
  26. let def = extractArray[1];
  27. //console.log(def);
  28. //minimise a bit
  29. def = def.replace(/\s+/g, ' ');
  30. def = def.replace(/>\s+</g, '><');
  31. def = def.trim();
  32. //more extraneous spaces - possible bad results, needs to be checked
  33. //def = def.replace(/(\w)\s(\W)|(\W)\s(\w)|(\W)\s(\W)/g, '$1$2$3$4$5$6');
  34. def = def.replace(/(\w)\s(\W)|(\W)\s(\w)/g, '$1$2$3$4');
  35. //escape double quotes
  36. def = def.replace(/\\([\s\S])|(")/g, "\\$1$2");
  37. console.log(def);
  38. //const char HTTP_HEAD[] PROGMEM =
  39. let string = 'const char ' + constantName + '[] PROGMEM';
  40. for (let i = string.length; i < 42; i++) {
  41. string += ' ';
  42. }
  43. string += '= "' + def + '";\n';
  44. stream.write(string);
  45. }
  46. }
  47. stream.end();
  48. });
  49. });