Mirror of Awesome Self Hosted
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
4.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. // Accepts input of any filename, ie. node test.js README.md
  2. const fs = require('fs');
  3. let log = '{\n';
  4. let issuelog = ' "message": "#### Syntax Issues\\n\\n Name | Entry\\n----|----------------------\\n';
  5. let fails = ''
  6. const file = fs.readFileSync(process.argv[2], 'utf8'); // Reads argv into var file
  7. function entryFilter(md) { // Function to find lines with entries
  8. const linepatt = /^\s{0,2}-\s\[.*`/;
  9. return linepatt.test(md);
  10. }
  11. function split(text) { // Function to split lines into array
  12. return text.split(/\r?\n/);
  13. }
  14. function findPattern(text) { // Test entries against 8 patterns. If matches pattern returns true
  15. const nodnospatt = /^\s{0,2}-\s\[.*?\]\(.*?\) - .{0,249}?\. `.*?` `.*?`/; // Regex for entries with no demo and no source code
  16. const slpatt = /^\s{0,2}-\s\[.*?\]\(.*?\) - .{0,249}?\. \(\[Demo\b\]\(.*?\), \[Source Code\b\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with demo and source code
  17. const nodpatt = /^\s{0,2}-\s\[.*?\]\(.*?\) - .{0,249}?\. \(\[Source Code\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with no demo
  18. const nospatt = /^\s{0,2}-\s\[.*?\]\(.*?\) - .{0,249}?\. \(\[Demo\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with no source code
  19. const pnodnospatt = /^\s{0,2}-\s\[.*?\]\(.*?\) `⚠` - .{0,249}?\. `.*?` `.*?`/; // Regex for entries with proprietary with no demo and no source code
  20. const pslpatt = /^\s{0,2}-\s\[.*?\]\(.*?\) `⚠` - .{0,249}?\. \(\[Demo\b\]\(.*?\), \[Source Code\b\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with proprietary with demo and source code
  21. const pnodpatt = /^\s{0,2}-\s\[.*?\]\(.*?\) `⚠` - .{0,249}?\. \(\[Source Code\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with proprietary with no demo
  22. const pnospatt = /^\s{0,2}-\s\[.*?\]\(.*?\) `⚠` - .{0,249}?\. \(\[Demo\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with proprietary with no source code
  23. if (nodnospatt.test(text) === true) {
  24. return true;
  25. } else if (slpatt.test(text) === true) {
  26. return true;
  27. } else if (nodpatt.test(text) === true) {
  28. return true;
  29. } else if (nospatt.test(text) === true) {
  30. return true;
  31. } else if (pnodnospatt.test(text) === true) {
  32. return true;
  33. } else if (pslpatt.test(text) === true) {
  34. return true;
  35. } else if (pnodpatt.test(text) === true) {
  36. return true;
  37. } else if (pnospatt.test(text) === true) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. function entryErrorCheck(md) {
  43. const namepatt = /^\s{0,2}-\s\[(.*?)\]/; // regex pattern to find name of entryArray
  44. const entries = split(md); // Inserts each line into the entries array
  45. let totalFail = 0;
  46. let totalPass = 0;
  47. let total = 0;
  48. const entryArray = [];
  49. for (let i = 0, len = entries.length; i < len; i += 1) { // Loop to create array of objects
  50. entryArray[i] = new Object;
  51. entryArray[i].raw = entries[i];
  52. if (entryFilter(entries[i]) === true) { // filter out lines that don't with * [)
  53. total += 1;
  54. entryArray[i].name = namepatt.exec(entries[i])[1]; // Parses name of entry
  55. entryArray[i].pass = findPattern(entries[i]); // Tests against known patterns
  56. if (entryArray[i].pass === true) { // If entry passes increment totalPass counter
  57. totalPass += 1;
  58. } else {
  59. console.log(`${entryArray[i].name} Failed.`); // If entry fails increment totalFail counter and append error to issuelog
  60. // entryArray[i].error = findError(entries[i]) //WIP
  61. totalFail += 1;
  62. issuelog += `${entryArray[i].name} | ${entries[i]} \\n`;
  63. fails += `${entries[i]} \n\n`;
  64. }
  65. }
  66. }
  67. if (totalFail > 0) { // Logs # passed & failed to console, and failures to syntaxcheck.json
  68. console.log(`${totalFail} Failed, ${totalPass} Passed, of ${total}\n-----------------------------`);
  69. console.log(fails)
  70. log += ` "error": true,\n "title": "Found ${totalFail} entries with syntax error(s).",\n`;
  71. fs.writeFileSync('syntaxcheck.json', `${log} ${issuelog} "\n}`);
  72. process.exit(1);
  73. } else { // Logs # of entries passed to console and error: false to syntaxcheck.json
  74. console.log(`${totalFail} Failed, ${totalPass} Passed, of ${total} \n`);
  75. log += ' "error": false\n}';
  76. fs.writeFileSync('syntaxcheck.json', log);
  77. process.exit(0);
  78. }
  79. }
  80. entryErrorCheck(file);