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.

180 lines
7.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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">Args</a>, <a href="#inline">Inline</a>, <a href="#closure">Closure</a>, <a href="#decorator">Decorator</a>, <a href="#class">Class</a>, <a href="#ducktypes">Duck_Types</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 DIAGRAM_1_A =
  29. '+---------+-------------+\n' +
  30. '| Classes | Metaclasses |\n' +
  31. '+---------+-------------|\n' +
  32. '| MyClass > MyMetaClass |\n' +
  33. '| | v |\n' +
  34. '| object ---> type <+ |\n' +
  35. '| | ^ +---+ |\n' +
  36. '| str -------+ |\n' +
  37. '+---------+-------------+\n';
  38. const DIAGRAM_1_B =
  39. '┏━━━━━━━━━┯━━━━━━━━━━━━━┓\n' +
  40. '┃ Classes │ Metaclasses ┃\n' +
  41. '┠─────────┼─────────────┨\n' +
  42. '┃ MyClass → MyMetaClass ┃\n' +
  43. '┃ │ ↓ ┃\n' +
  44. '┃ object ───→ type ←╮ ┃\n' +
  45. '┃ │ ↑ ╰───╯ ┃\n' +
  46. '┃ str ───────╯ ┃\n' +
  47. '┗━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
  48. const DIAGRAM_2_A =
  49. '+---------+-------------+\n' +
  50. '| Classes | Metaclasses |\n' +
  51. '+---------+-------------|\n' +
  52. '| MyClass | MyMetaClass |\n' +
  53. '| v | v |\n' +
  54. '| object <--- type |\n' +
  55. '| ^ | |\n' +
  56. '| str | |\n' +
  57. '+---------+-------------+\n';
  58. const DIAGRAM_2_B =
  59. '┏━━━━━━━━━┯━━━━━━━━━━━━━┓\n' +
  60. '┃ Classes │ Metaclasses ┃\n' +
  61. '┠─────────┼─────────────┨\n' +
  62. '┃ MyClass │ MyMetaClass ┃\n' +
  63. '┃ ↓ │ ↓ ┃\n' +
  64. '┃ object ←─── type ┃\n' +
  65. '┃ ↑ │ ┃\n' +
  66. '┃ str │ ┃\n' +
  67. '┗━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
  68. function main() {
  69. const html = getMd();
  70. initDom(html);
  71. modifyPage();
  72. const template = readFile('web/template.html');
  73. const tokens = template.split('<div id=main_container></div>');
  74. const text = `${tokens[0]} ${document.body.innerHTML} ${tokens[1]}`;
  75. writeToFile('index.html', text);
  76. }
  77. function initDom(html) {
  78. const { JSDOM } = jsdom;
  79. const dom = new JSDOM(html);
  80. const $ = (require('jquery'))(dom.window);
  81. global.$ = $;
  82. global.document = dom.window.document;
  83. }
  84. function getMd() {
  85. var readme = readFile('README.md');
  86. readme = switchClassDiagrams(readme);
  87. const converter = new showdown.Converter();
  88. return converter.makeHtml(readme);
  89. }
  90. function switchClassDiagrams(readme) {
  91. readme = readme.replace(DIAGRAM_1_A, DIAGRAM_1_B)
  92. return readme.replace(DIAGRAM_2_A, DIAGRAM_2_B)
  93. }
  94. function modifyPage() {
  95. removeOrigToc();
  96. addToc();
  97. insertLinks();
  98. unindentBanner();
  99. highlightCode();
  100. }
  101. function removeOrigToc() {
  102. const headerContents = $('#contents');
  103. const contentsList = headerContents.next();
  104. headerContents.remove();
  105. contentsList.remove();
  106. }
  107. function addToc() {
  108. const nodes = $.parseHTML(TOC);
  109. $('#main').before(nodes);
  110. }
  111. function insertLinks() {
  112. $('h2').each(function() {
  113. const aId = $(this).attr('id');
  114. const text = $(this).text();
  115. const line = `<a href="#${aId}" name="${aId}">#</a>${text}`;
  116. $(this).html(line);
  117. });
  118. }
  119. function unindentBanner() {
  120. const montyImg = $('img').first();
  121. montyImg.parent().addClass('banner');
  122. const downloadPraragrapth = $('p').first();
  123. downloadPraragrapth.addClass('banner');
  124. }
  125. function highlightCode() {
  126. setApaches(['<D>', '<T>', '<DT>', '<TD>', '<a>', '<n>']);
  127. $('code').not('.python').not('.text').not('.bash').not('.apache').addClass('python');
  128. $('code').each(function(index) {
  129. hljs.highlightBlock(this);
  130. });
  131. fixClasses()
  132. }
  133. function setApaches(elements) {
  134. for (el of elements) {
  135. $(`code:contains(${el})`).addClass('apache');
  136. }
  137. }
  138. function fixClasses() {
  139. // Changes class="hljs-keyword" to class="hljs-title" of 'class' keyword.
  140. $('.hljs-class').filter(':contains(class \')').find(':first-child').removeClass('hljs-keyword').addClass('hljs-title')
  141. }
  142. function readFile(filename) {
  143. try {
  144. return fs.readFileSync(filename, 'utf8');
  145. } catch(e) {
  146. console.error('Error:', e.stack);
  147. }
  148. }
  149. function writeToFile(filename, text) {
  150. try {
  151. return fs.writeFileSync(filename, text, 'utf8');
  152. } catch(e) {
  153. console.error('Error:', e.stack);
  154. }
  155. }
  156. main();