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.

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