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.

132 lines
5.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  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. const MRO =
  29. '<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>C.mro()\n[&lt;<span class="hljs-class"><span class="hljs-title">class</span> \'<span class="hljs-title">C</span>\'&gt;, &lt;<span class="hljs-title">class</span> \'<span class="hljs-title">A</span>\'&gt;, &lt;<span class="hljs-title">class</span> \'<span class="hljs-title">B</span>\'&gt;, &lt;<span class="hljs-title">class</span> \'<span class="hljs-title">object</span>\'&gt;]</span></code>\n</pre>\n'
  30. function main() {
  31. const html = getMd();
  32. initDom(html);
  33. modifyPage();
  34. const template = readFile('web/template.html');
  35. const tokens = template.split('<div id=main_container></div>');
  36. const text = `${tokens[0]} ${document.body.innerHTML} ${tokens[1]}`;
  37. writeToFile('index.html', text);
  38. }
  39. function initDom(html) {
  40. const { JSDOM } = jsdom;
  41. const dom = new JSDOM(html);
  42. const $ = (require('jquery'))(dom.window);
  43. global.$ = $;
  44. global.document = dom.window.document;
  45. }
  46. function getMd() {
  47. const readme = readFile('README.md');
  48. const converter = new showdown.Converter();
  49. return converter.makeHtml(readme);
  50. }
  51. function modifyPage() {
  52. removeOrigToc();
  53. addToc();
  54. insertLinks();
  55. unindentBanner();
  56. highlightCode();
  57. }
  58. function removeOrigToc() {
  59. const headerContents = $('#contents');
  60. const contentsList = headerContents.next();
  61. headerContents.remove();
  62. contentsList.remove();
  63. }
  64. function addToc() {
  65. const nodes = $.parseHTML(TOC);
  66. $('#main').before(nodes);
  67. }
  68. function insertLinks() {
  69. $('h2').each(function() {
  70. const aId = $(this).attr('id');
  71. const text = $(this).text();
  72. const line = `<a href="#${aId}" name="${aId}">#</a>${text}`;
  73. $(this).html(line);
  74. });
  75. }
  76. function unindentBanner() {
  77. const montyImg = $('img').first();
  78. montyImg.parent().addClass('banner');
  79. const downloadPraragrapth = $('p').first();
  80. downloadPraragrapth.addClass('banner');
  81. }
  82. function highlightCode() {
  83. setApache('<D>')
  84. setApache('<T>')
  85. setApache('<DT>')
  86. setApache('<TD>')
  87. setApache('<a>')
  88. setApache('<n>')
  89. $('code').not('.python').not('.text').not('.bash').not('.apache').addClass('python');
  90. $('code').each(function(index) {
  91. hljs.highlightBlock(this);
  92. });
  93. $('#copy').prev().remove()
  94. const nodes = $.parseHTML(MRO);
  95. $('#copy').before(nodes);
  96. }
  97. function setApache(codeContents) {
  98. $(`code:contains(${codeContents})`).addClass('apache');
  99. }
  100. function readFile(filename) {
  101. try {
  102. return fs.readFileSync(filename, 'utf8');
  103. } catch(e) {
  104. console.error('Error:', e.stack);
  105. }
  106. }
  107. function writeToFile(filename, text) {
  108. try {
  109. return fs.writeFileSync(filename, text, 'utf8');
  110. } catch(e) {
  111. console.error('Error:', e.stack);
  112. }
  113. }
  114. main();