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.

114 lines
5.0 KiB

  1. #!/usr/bin/env node
  2. // Usage: node test.js
  3. // Script that creates index.html out of web/template.html and README.md.
  4. // It is written in JS because this code used to be executed on the client side.
  5. // To install dependencies run:
  6. // $ npm install -g jsdom jquery showdown highlightjs
  7. // If running on mac and modules cant be found after instalation add:
  8. // export NODE_PATH=/usr/local/lib/node_modules
  9. // to the ~/.bash_profile or ~/.bashrc file and run '$ bash'.
  10. const fs = require('fs');
  11. const jsdom = require('jsdom');
  12. const showdown = require('showdown');
  13. const hljs = require('highlightjs');
  14. const TOC =
  15. '<br>' +
  16. '<h2 id="toc">Contents</h2>\n' +
  17. '<pre><code class="hljs bash"><strong>ToC</strong> = {\n' +
  18. ' <strong><span class="hljs-string">\'1. Collections\'</span></strong>: [<a href="#list">List</a>, <a href="#dictionary">Dict</a>, <a href="#set">Set</a>, <a href="#range">Range</a>, <a href="#enumerate">Enumerate</a>, <a href="#namedtuple">Namedtuple</a>, <a href="#iterator">Iterator</a>, <a href="#generator">Generator</a>],\n' +
  19. ' <strong><span class="hljs-string">\'2. Types\'</span></strong>: [<a href="#type">Type</a>, <a href="#string">String</a>, <a href="#regex">Regex</a>, <a href="#format">Format</a>, <a href="#numbers">Numbers</a>, <a href="#combinatorics">Combinatorics</a>, <a href="#datetime">Datetime</a>ᴺᴱᵂ],\n' +
  20. ' <strong><span class="hljs-string">\'3. Syntax\'</span></strong>: [<a href="#arguments">Arguments</a>, <a href="#splatoperator">Splat</a>, <a href="#inline">Inline</a>, <a href="#closure">Closure</a>, <a href="#decorator">Decorator</a>, <a href="#class">Class</a>, <a href="#enum">Enum</a>, <a href="#exceptions">Exceptions</a>],\n' +
  21. ' <strong><span class="hljs-string">\'4. System\'</span></strong>: [<a href="#print">Print</a>, <a href="#input">Input</a>, <a href="#commandlinearguments">Command_Line_Arguments</a>, <a href="#open">Open</a>, <a href="#path">Path</a>ᴺᴱᵂ, <a href="#commandexecution">Command_Execution</a>],\n' +
  22. ' <strong><span class="hljs-string">\'5. Data\'</span></strong>: [<a href="#csv">CSV</a>, <a href="#json">JSON</a>, <a href="#pickle">Pickle</a>, <a href="#sqlite">SQLite</a>, <a href="#bytes">Bytes</a>, <a href="#struct">Struct</a>, <a href="#array">Array</a>, <a href="#memoryview">MemoryView</a>, <a href="#deque">Deque</a>],\n' +
  23. ' <strong><span class="hljs-string">\'6. Advanced\'</span></strong>: [<a href="#threading">Threading</a>, <a href="#introspection">Introspection</a>, <a href="#metaprograming">Metaprograming</a>, <a href="#operator">Operator</a>, <a href="#eval">Eval</a>, <a href="#coroutine">Coroutine</a>],\n' +
  24. ' <strong><span class="hljs-string">\'7. Libraries\'</span></strong>: [<a href="#progressbar">Progress_Bar</a>, <a href="#plot">Plot</a>, <a href="#table">Table</a>, <a href="#curses">Curses</a>, <a href="#logging">Logging</a>ᴺᴱᵂ, <a href="#scraping">Scraping</a>, <a href="#web">Web</a>, <a href="#profile">Profile</a>,\n' +
  25. ' <a href="#numpy">NumPy</a>, <a href="#image">Image</a>, <a href="#audio">Audio</a>]\n' +
  26. '}\n' +
  27. '</code></pre>\n';
  28. function main() {
  29. const html = getMd();
  30. initDom(html);
  31. modifyPage();
  32. const template = readFile('web/template.html');
  33. const tokens = template.split('<div id=main_container></div>');
  34. const text = `${tokens[0]} ${document.body.innerHTML} ${tokens[1]}`;
  35. writeToFile('index.html', text);
  36. }
  37. function initDom(html) {
  38. const { JSDOM } = jsdom;
  39. const dom = new JSDOM(html);
  40. const $ = (require('jquery'))(dom.window);
  41. global.$ = $;
  42. global.document = dom.window.document;
  43. }
  44. function getMd() {
  45. const readme = readFile('README.md');
  46. const converter = new showdown.Converter();
  47. return converter.makeHtml(readme);
  48. }
  49. function modifyPage() {
  50. removeOrigToc();
  51. addToc();
  52. insertLinks();
  53. unindentBanner();
  54. $('code').not('.python').not('.text').not('.bash').addClass('python');
  55. $('code').each(function(index) {
  56. hljs.highlightBlock(this);
  57. });
  58. }
  59. function removeOrigToc() {
  60. const headerContents = $('#contents');
  61. const contentsList = headerContents.next();
  62. headerContents.remove();
  63. contentsList.remove();
  64. }
  65. function insertLinks() {
  66. $('h2').each(function() {
  67. const aId = $(this).attr('id');
  68. const text = $(this).text();
  69. const line = `<a href="#${aId}" name="${aId}">#</a>${text}`;
  70. $(this).html(line);
  71. });
  72. }
  73. function unindentBanner() {
  74. const montyImg = $('img').first();
  75. montyImg.parent().addClass('banner');
  76. const downloadPraragrapth = $('p').first();
  77. downloadPraragrapth.addClass('banner');
  78. }
  79. function addToc() {
  80. const headerMain = $('#main');
  81. const nodes = $.parseHTML(TOC);
  82. headerMain.before(nodes);
  83. }
  84. function readFile(filename) {
  85. try {
  86. return fs.readFileSync(filename, 'utf8');
  87. } catch(e) {
  88. console.error('Error:', e.stack);
  89. }
  90. }
  91. function writeToFile(filename, text) {
  92. try {
  93. return fs.writeFileSync(filename, text, 'utf8');
  94. } catch(e) {
  95. console.error('Error:', e.stack);
  96. }
  97. }
  98. main();