generate.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. var fs = require('fs');
  2. function get_file_contents(filepath) {
  3. try {
  4. if (fs.existsSync(filepath)) {
  5. return fs.readFileSync(filepath, 'utf8');
  6. } else {
  7. return '';
  8. }
  9. } catch(err) {
  10. console.error(err);
  11. }
  12. }
  13. var info = JSON.parse(get_file_contents('src/info.json'));
  14. var html = get_file_contents('src/html/index.html');
  15. var menu_item_template = get_file_contents('src/html/menu_item.html');
  16. var column_headers_template = get_file_contents('src/html/column_headers.html');
  17. var code_rows_template = get_file_contents('src/html/code_rows.html');
  18. var code_cell_template = get_file_contents('src/html/code_cell.html');
  19. // Process Languages
  20. if ( info.languages ) {
  21. var output_queue, output_queue2;
  22. var output, output2;
  23. // Process menu items
  24. output_queue = '';
  25. info.languages.forEach(function(lang) {
  26. output = menu_item_template;
  27. //console.log(lang);
  28. if ( info.default_langs.includes(lang.slug) ) {
  29. output = output.replace(/{{_ checked _}}/g, 'checked');
  30. } else {
  31. output = output.replace(/{{_ checked _}}/g, '');
  32. }
  33. output = output.replace(/{{_ slug _}}/g, lang.slug)
  34. output = output.replace(/{{_ label _}}/g, lang.label)
  35. output_queue += output;
  36. });
  37. html = html.replace(/{{_ menu_items _}}/g, output_queue);
  38. // Process columns
  39. output_queue = '';
  40. info.languages.forEach(function(lang) {
  41. output = column_headers_template;
  42. if ( info.default_langs.includes(lang.slug) ) {
  43. output = output.replace(/{{_ hidden _}}/g, '');
  44. } else {
  45. output = output.replace(/{{_ hidden _}}/g, 'hidden');
  46. }
  47. output = output.replace(/{{_ slug _}}/g, lang.slug);
  48. output = output.replace(/{{_ label _}}/g, lang.label);
  49. output_queue += output;
  50. });
  51. html = html.replace(/{{_ column_headers _}}/g, output_queue);
  52. }
  53. // Process codes
  54. if ( info.codes ) {
  55. output_queue = '';
  56. info.codes.forEach(function(code) {
  57. output = code_rows_template;
  58. output = output.replace(/{{_ code_label _}}/g, code.label);
  59. // Code cells
  60. output_queue2 = '';
  61. info.languages.forEach(function(lang) {
  62. output2 = code_cell_template;
  63. if ( info.default_langs.includes(lang.slug) ) {
  64. output2 = output2.replace(/{{_ hidden _}}/g, '');
  65. } else {
  66. output2 = output2.replace(/{{_ hidden _}}/g, 'hidden');
  67. }
  68. output2 = output2.replace(/{{_ slug _}}/g, lang.slug);
  69. output2 = output2.replace(/{{_ label _}}/g, lang.label);
  70. output2 = output2.replace(/{{_ code _}}/g, get_file_contents('src/codes/'+lang.slug+'/'+code.slug+'.'+lang.ext));
  71. output_queue2 += output2;
  72. });
  73. output = output.replace(/{{_ code_cells _}}/g, output_queue2);
  74. output_queue += output;
  75. });
  76. }
  77. html = html.replace(/{{_ code_rows _}}/g, output_queue);
  78. // Write index.html
  79. try {
  80. const data = fs.writeFileSync('public/index.html', html)
  81. } catch (err) {
  82. console.error(err)
  83. }