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.

271 lines
15 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
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">Dictionary</a>, <a href="#set">Set</a>, <a href="#tuple">Tuple</a>, <a href="#range">Range</a>, <a href="#enumerate">Enumerate</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">Regular_Exp</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. const DIAGRAM_3_A =
  69. '+------------------+----------+------------+----------+\n' +
  70. '| | Sequence | Collection | Iterable |\n' +
  71. '+------------------+----------+------------+----------+\n' +
  72. '| list, range, str | yes | yes | yes |\n' +
  73. '| dict, set | | yes | yes |\n' +
  74. '| iter | | | yes |\n' +
  75. '+------------------+----------+------------+----------+\n';
  76. const DIAGRAM_3_B =
  77. '┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━┓\n' +
  78. '┃ │ Sequence │ Collection │ Iterable ┃\n' +
  79. '┠──────────────────┼──────────┼────────────┼──────────┨\n' +
  80. '┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' +
  81. '┃ dict, set │ │ ✓ │ ✓ ┃\n' +
  82. '┃ iter │ │ │ ✓ ┃\n' +
  83. '┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┛\n';
  84. const DIAGRAM_4_A =
  85. '+--------------------+----------+----------+------+---------+--------+\n' +
  86. '| | Integral | Rational | Real | Complex | Number |\n' +
  87. '+--------------------+----------+----------+------+---------+--------+\n' +
  88. '| int | yes | yes | yes | yes | yes |\n' +
  89. '| fractions.Fraction | | yes | yes | yes | yes |\n' +
  90. '| float | | | yes | yes | yes |\n' +
  91. '| complex | | | | yes | yes |\n' +
  92. '+--------------------+----------+----------+------+---------+--------+\n';
  93. const DIAGRAM_4_B =
  94. '┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━┯━━━━━━━━━┯━━━━━━━━┓\n' +
  95. '┃ │ Integral │ Rational │ Real │ Complex │ Number ┃\n' +
  96. '┠────────────────────┼──────────┼──────────┼──────┼─────────┼────────┨\n' +
  97. '┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  98. '┃ fractions.Fraction │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  99. '┃ float │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
  100. '┃ complex │ │ │ │ ✓ │ ✓ ┃\n' +
  101. '┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━┷━━━━━━━━━┷━━━━━━━━┛\n';
  102. const DIAGRAM_5_A =
  103. "+----------------+--------------+---------------+---------------+---------------+\n" +
  104. "| | {<float>:.2} | {<float>:.2f} | {<float>:.2e} | {<float>:.2%} |\n" +
  105. "+----------------+--------------+---------------+---------------+---------------+\n" +
  106. "| 0.000056789 | '5.7e-05' | '0.00' | '5.68e-05' | '0.01%' |\n" +
  107. "| 0.00056789 | '0.00057' | '0.00' | '5.68e-04' | '0.06%' |\n" +
  108. "| 0.0056789 | '0.0057' | '0.01' | '5.68e-03' | '0.57%' |\n" +
  109. "| 0.056789 | '0.057' | '0.06' | '5.68e-02' | '5.68%' |\n" +
  110. "| 0.56789 | '0.57' | '0.57' | '5.68e-01' | '56.79%' |\n" +
  111. "| 5.6789 | '5.7' | '5.68' | '5.68e+00' | '567.89%' |\n" +
  112. "| 56.789 | '5.7e+01' | '56.79' | '5.68e+01' | '5678.90%' |\n" +
  113. "| 567.89 | '5.7e+02' | '567.89' | '5.68e+02' | '56789.00%' |\n" +
  114. "+----------------+--------------+---------------+---------------+---------------+\n";
  115. const DIAGRAM_5_B =
  116. "┏━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  117. "┃ │ {<float>:.2} │ {<float>:.2f} │ {<float>:.2e} │ {<float>:.2%} ┃\n" +
  118. "┠────────────────┼──────────────┼───────────────┼───────────────┼───────────────┨\n" +
  119. "┃ 0.000056789 │ '5.7e-05' │ '0.00' │ '5.68e-05' │ '0.01%' ┃\n" +
  120. "┃ 0.00056789 │ '0.00057' │ '0.00' │ '5.68e-04' │ '0.06%' ┃\n" +
  121. "┃ 0.0056789 │ '0.0057' │ '0.01' │ '5.68e-03' │ '0.57%' ┃\n" +
  122. "┃ 0.056789 │ '0.057' │ '0.06' │ '5.68e-02' │ '5.68%' ┃\n" +
  123. "┃ 0.56789 │ '0.57' │ '0.57' │ '5.68e-01' │ '56.79%' ┃\n" +
  124. "┃ 5.6789 │ '5.7' │ '5.68' │ '5.68e+00' │ '567.89%' ┃\n" +
  125. "┃ 56.789 │ '5.7e+01' │ '56.79' │ '5.68e+01' │ '5678.90%' ┃\n" +
  126. "┃ 567.89 │ '5.7e+02' │ '567.89' │ '5.68e+02' │ '56789.00%' ┃\n" +
  127. "┗━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  128. const DIAGRAM_6_A =
  129. '+------------+----------+------------+----------+--------------+\n' +
  130. '| | Iterable | Collection | Sequence | abc.Sequence |\n' +
  131. '+------------+----------+------------+----------+--------------+\n' +
  132. '| iter() | yes | yes | yes | yes |\n' +
  133. '| len() | | yes | yes | yes |\n' +
  134. '| getitem() | | | yes | yes |\n' +
  135. '| contains() | | yes | | yes |\n' +
  136. '+------------+----------+------------+----------+--------------+\n';
  137. const DIAGRAM_6_B =
  138. '┏━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━━━┓\n' +
  139. '┃ │ Iterable │ Collection │ Sequence │ abc.Sequence ┃\n' +
  140. '┠────────────┼──────────┼────────────┼──────────┼──────────────┨\n' +
  141. '┃ iter() │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  142. '┃ len() │ │ ✓ │ ✓ │ ✓ ┃\n' +
  143. '┃ getitem() │ │ │ ✓ │ ✓ ┃\n' +
  144. '┃ contains() │ │ ✓ │ │ ✓ ┃\n' +
  145. '┗━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━┛\n';
  146. function main() {
  147. const html = getMd();
  148. initDom(html);
  149. modifyPage();
  150. const template = readFile('web/template.html');
  151. const tokens = template.split('<div id=main_container></div>');
  152. const text = `${tokens[0]} ${document.body.innerHTML} ${tokens[1]}`;
  153. writeToFile('index.html', text);
  154. }
  155. function initDom(html) {
  156. const { JSDOM } = jsdom;
  157. const dom = new JSDOM(html);
  158. const $ = (require('jquery'))(dom.window);
  159. global.$ = $;
  160. global.document = dom.window.document;
  161. }
  162. function getMd() {
  163. var readme = readFile('README.md');
  164. readme = switchClassDiagrams(readme);
  165. const converter = new showdown.Converter();
  166. return converter.makeHtml(readme);
  167. }
  168. function switchClassDiagrams(readme) {
  169. readme = readme.replace(DIAGRAM_1_A, DIAGRAM_1_B)
  170. readme = readme.replace(DIAGRAM_2_A, DIAGRAM_2_B)
  171. readme = readme.replace(DIAGRAM_3_A, DIAGRAM_3_B)
  172. readme = readme.replace(DIAGRAM_4_A, DIAGRAM_4_B)
  173. readme = readme.replace(DIAGRAM_5_A, DIAGRAM_5_B)
  174. readme = readme.replace(DIAGRAM_6_A, DIAGRAM_6_B)
  175. return readme
  176. }
  177. function modifyPage() {
  178. removeOrigToc();
  179. addToc();
  180. insertLinks();
  181. unindentBanner();
  182. highlightCode();
  183. }
  184. function removeOrigToc() {
  185. const headerContents = $('#contents');
  186. const contentsList = headerContents.next();
  187. headerContents.remove();
  188. contentsList.remove();
  189. }
  190. function addToc() {
  191. const nodes = $.parseHTML(TOC);
  192. $('#main').before(nodes);
  193. }
  194. function insertLinks() {
  195. $('h2').each(function() {
  196. const aId = $(this).attr('id');
  197. const text = $(this).text();
  198. const line = `<a href="#${aId}" name="${aId}">#</a>${text}`;
  199. $(this).html(line);
  200. });
  201. }
  202. function unindentBanner() {
  203. const montyImg = $('img').first();
  204. montyImg.parent().addClass('banner');
  205. const downloadPraragrapth = $('p').first();
  206. downloadPraragrapth.addClass('banner');
  207. }
  208. function highlightCode() {
  209. setApaches(['<D>', '<T>', '<DT>', '<TD>', '<a>', '<n>']);
  210. $('code').not('.python').not('.text').not('.bash').not('.apache').addClass('python');
  211. $('code').each(function(index) {
  212. hljs.highlightBlock(this);
  213. });
  214. fixClasses()
  215. }
  216. function setApaches(elements) {
  217. for (el of elements) {
  218. $(`code:contains(${el})`).addClass('apache');
  219. }
  220. }
  221. function fixClasses() {
  222. // Changes class="hljs-keyword" to class="hljs-title" of 'class' keyword.
  223. $('.hljs-class').filter(':contains(class \')').find(':first-child').removeClass('hljs-keyword').addClass('hljs-title')
  224. }
  225. function readFile(filename) {
  226. try {
  227. return fs.readFileSync(filename, 'utf8');
  228. } catch(e) {
  229. console.error('Error:', e.stack);
  230. }
  231. }
  232. function writeToFile(filename, text) {
  233. try {
  234. return fs.writeFileSync(filename, text, 'utf8');
  235. } catch(e) {
  236. console.error('Error:', e.stack);
  237. }
  238. }
  239. main();