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.

445 lines
28 KiB

5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 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
4 years ago
5 years ago
4 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 can't be found after installation 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_Type</a>, <a href="#enum">Enum</a>, <a href="#exceptions">Exception</a>],\n' +
  21. ' <strong><span class="hljs-string">\'4. System\'</span></strong>: [<a href="#exit">Exit</a>, <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="#oscommands">OS_Commands</a>],\n' +
  22. ' <strong><span class="hljs-string">\'5. Data\'</span></strong>: [<a href="#json">JSON</a>, <a href="#pickle">Pickle</a>, <a href="#csv">CSV</a>, <a href="#sqlite">SQLite</a>, <a href="#bytes">Bytes</a>, <a href="#struct">Struct</a>, <a href="#array">Array</a>, <a href="#memoryview">Memory_View</a>, <a href="#deque">Deque</a>],\n' +
  23. ' <strong><span class="hljs-string">\'6. Advanced\'</span></strong>: [<a href="#threading">Threading</a>, <a href="#operator">Operator</a>, <a href="#introspection">Introspection</a>, <a href="#metaprograming">Metaprograming</a>, <a href="#eval">Eval</a>, <a href="#coroutines">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="#profiling">Profile</a>,\n' +
  25. ' <a href="#numpy">NumPy</a>, <a href="#image">Image</a>, <a href="#audio">Audio</a>, <a href="#pygame">Pygame</a>]\n' +
  26. '}\n' +
  27. '</code></pre>\n';
  28. const OS_RENAME =
  29. 'os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span>\n' +
  30. 'os.replace(from, to) <span class="hljs-comment"># Same, but overwrites \'to\' if it exists.</span>\n';
  31. const SHUTIL_COPY =
  32. 'shutil.copy(from, to) <span class="hljs-comment"># Copies the file. \'to\' can exist or be a dir.</span>\n' +
  33. 'shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. \'to\' must not exist.</span>\n';
  34. const EVAL =
  35. '<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> ast <span class="hljs-keyword">import</span> literal_eval\n' +
  36. '<span class="hljs-meta">&gt;&gt;&gt; </span>literal_eval(<span class="hljs-string">\'1 + 2\'</span>)\n' +
  37. '<span class="hljs-number">3</span>\n' +
  38. '<span class="hljs-meta">&gt;&gt;&gt; </span>literal_eval(<span class="hljs-string">\'[1, 2, 3]\'</span>)\n' +
  39. '[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]\n' +
  40. '<span class="hljs-meta">&gt;&gt;&gt; </span>literal_eval(<span class="hljs-string">\'abs(1)\'</span>)\n' +
  41. 'ValueError: malformed node or string\n';
  42. const LRU_CACHE =
  43. '<span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> lru_cache\n' +
  44. '\n' +
  45. '<span class="hljs-meta">@lru_cache(maxsize=None)</span>\n' +
  46. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fib</span><span class="hljs-params">(n)</span>:</span>\n' +
  47. ' <span class="hljs-keyword">return</span> n <span class="hljs-keyword">if</span> n &lt; <span class="hljs-number">2</span> <span class="hljs-keyword">else</span> fib(n-<span class="hljs-number">2</span>) + fib(n-<span class="hljs-number">1</span>)\n';
  48. const TYPE =
  49. '<code class="python language-python hljs">&lt;class&gt; = type(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;parents_tuple&gt;, &lt;attributes_dict&gt;)</code>';
  50. const DATACLASS =
  51. '<code class="python language-python hljs"><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass\n' +
  52. '&lt;class&gt; = make_dataclass(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;coll_of_attribute_names&gt;)\n' +
  53. '&lt;class&gt; = make_dataclass(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;coll_of_tuples&gt;)\n' +
  54. '&lt;tuple&gt; = (<span class="hljs-string">\'&lt;attr_name&gt;\'</span>, &lt;type&gt; [, &lt;default_value&gt;])</code>';
  55. const DATETIME =
  56. '<code class="python language-python hljs"><span class="hljs-string">\'&lt;DT&gt; = resolve_imaginary(&lt;DT&gt;)\'</span></code>';
  57. const DIAGRAM_1_A =
  58. '+-------------+-------------+\n' +
  59. '| Classes | Metaclasses |\n' +
  60. '+-------------+-------------|\n' +
  61. '| MyClass --> MyMetaClass |\n';
  62. const DIAGRAM_1_B =
  63. '┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┓\n' +
  64. '┃ Classes │ Metaclasses ┃\n' +
  65. '┠─────────────┼─────────────┨\n' +
  66. '┃ MyClass ──→ MyMetaClass ┃\n' +
  67. '┃ │ ↓ ┃\n' +
  68. '┃ object ─────→ type ←╮ ┃\n' +
  69. '┃ │ ↑ ╰──╯ ┃\n' +
  70. '┃ str ──────────╯ ┃\n' +
  71. '┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
  72. const DIAGRAM_2_A =
  73. '+-------------+-------------+\n' +
  74. '| Classes | Metaclasses |\n' +
  75. '+-------------+-------------|\n' +
  76. '| MyClass | MyMetaClass |\n';
  77. const DIAGRAM_2_B =
  78. '┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┓\n' +
  79. '┃ Classes │ Metaclasses ┃\n' +
  80. '┠─────────────┼─────────────┨\n' +
  81. '┃ MyClass │ MyMetaClass ┃\n' +
  82. '┃ ↓ │ ↓ ┃\n' +
  83. '┃ object ←───── type ┃\n' +
  84. '┃ ↑ │ ┃\n' +
  85. '┃ str │ ┃\n' +
  86. '┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
  87. const DIAGRAM_3_A =
  88. '+------------------+------------+------------+------------+\n' +
  89. '| | Sequence | Collection | Iterable |\n' +
  90. '+------------------+------------+------------+------------+\n';
  91. const DIAGRAM_3_B =
  92. '┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓\n' +
  93. '┃ │ Sequence │ Collection │ Iterable ┃\n' +
  94. '┠──────────────────┼────────────┼────────────┼────────────┨\n' +
  95. '┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' +
  96. '┃ dict, set │ │ ✓ │ ✓ ┃\n' +
  97. '┃ iter │ │ │ ✓ ┃\n' +
  98. '┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛\n';
  99. const DIAGRAM_4_A =
  100. '+--------------------+----------+----------+----------+----------+----------+\n' +
  101. '| | Integral | Rational | Real | Complex | Number |\n' +
  102. '+--------------------+----------+----------+----------+----------+----------+\n';
  103. const DIAGRAM_4_B =
  104. '┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓\n' +
  105. '┃ │ Integral │ Rational │ Real │ Complex │ Number ┃\n' +
  106. '┠────────────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨\n' +
  107. '┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  108. '┃ fractions.Fraction │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  109. '┃ float │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
  110. '┃ complex │ │ │ │ ✓ │ ✓ ┃\n' +
  111. '┃ decimal.Decimal │ │ │ │ │ ✓ ┃\n' +
  112. '┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛\n';
  113. const DIAGRAM_5_A =
  114. "+---------------+-----------------+-----------------+-----------------+-----------------+\n" +
  115. "| | {<float>} | {<float>:f} | {<float>:e} | {<float>:%} |\n" +
  116. "+---------------+-----------------+-----------------+-----------------+-----------------+\n";
  117. const DIAGRAM_5_B =
  118. "┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┓\n" +
  119. "┃ │ {&lt;float&gt;} │ {&lt;float&gt;:f} │ {&lt;float&gt;:e} │ {&lt;float&gt;:%} ┃\n" +
  120. "┠───────────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┨\n" +
  121. "┃ 0.000056789 │ '5.6789e-05' │ '0.000057' │ '5.678900e-05' │ '0.005679%' ┃\n" +
  122. "┃ 0.00056789 │ '0.00056789' │ '0.000568' │ '5.678900e-04' │ '0.056789%' ┃\n" +
  123. "┃ 0.0056789 │ '0.0056789' │ '0.005679' │ '5.678900e-03' │ '0.567890%' ┃\n" +
  124. "┃ 0.056789 │ '0.056789' │ '0.056789' │ '5.678900e-02' │ '5.678900%' ┃\n" +
  125. "┃ 0.56789 │ '0.56789' │ '0.567890' │ '5.678900e-01' │ '56.789000%' ┃\n" +
  126. "┃ 5.6789 │ '5.6789' │ '5.678900' │ '5.678900e+00' │ '567.890000%' ┃\n" +
  127. "┃ 56.789 │ '56.789' │ '56.789000' │ '5.678900e+01' │ '5678.900000%' ┃\n" +
  128. "┃ 567.89 │ '567.89' │ '567.890000' │ '5.678900e+02' │ '56789.000000%' ┃\n" +
  129. "┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┛\n";
  130. const DIAGRAM_6_A =
  131. "+---------------+-----------------+-----------------+-----------------+-----------------+\n" +
  132. "| | {<float>:.2} | {<float>:.2f} | {<float>:.2e} | {<float>:.2%} |\n" +
  133. "+---------------+-----------------+-----------------+-----------------+-----------------+\n";
  134. const DIAGRAM_6_B =
  135. "┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┓\n" +
  136. "┃ │ {&lt;float&gt;:.2} │ {&lt;float&gt;:.2f} │ {&lt;float&gt;:.2e} │ {&lt;float&gt;:.2%} ┃\n" +
  137. "┠───────────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┨\n" +
  138. "┃ 0.000056789 │ '5.7e-05' │ '0.00' │ '5.68e-05' │ '0.01%' ┃\n" +
  139. "┃ 0.00056789 │ '0.00057' │ '0.00' │ '5.68e-04' │ '0.06%' ┃\n" +
  140. "┃ 0.0056789 │ '0.0057' │ '0.01' │ '5.68e-03' │ '0.57%' ┃\n" +
  141. "┃ 0.056789 │ '0.057' │ '0.06' │ '5.68e-02' │ '5.68%' ┃\n" +
  142. "┃ 0.56789 │ '0.57' │ '0.57' │ '5.68e-01' │ '56.79%' ┃\n" +
  143. "┃ 5.6789 │ '5.7' │ '5.68' │ '5.68e+00' │ '567.89%' ┃\n" +
  144. "┃ 56.789 │ '5.7e+01' │ '56.79' │ '5.68e+01' │ '5678.90%' ┃\n" +
  145. "┃ 567.89 │ '5.7e+02' │ '567.89' │ '5.68e+02' │ '56789.00%' ┃\n" +
  146. "┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┛\n";
  147. const DIAGRAM_7_A =
  148. '+------------+------------+------------+------------+--------------+\n' +
  149. '| | Iterable | Collection | Sequence | abc.Sequence |\n' +
  150. '+------------+------------+------------+------------+--------------+\n';
  151. const DIAGRAM_7_B =
  152. '┏━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓\n' +
  153. '┃ │ Iterable │ Collection │ Sequence │ abc.Sequence ┃\n' +
  154. '┠────────────┼────────────┼────────────┼────────────┼──────────────┨\n' +
  155. '┃ iter() │ ! │ ! │ ✓ │ ✓ ┃\n' +
  156. '┃ contains() │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  157. '┃ len() │ │ ! │ ! │ ! ┃\n' +
  158. '┃ getitem() │ │ │ ! │ ! ┃\n' +
  159. '┃ reversed() │ │ │ ✓ │ ✓ ┃\n' +
  160. '┃ index() │ │ │ │ ✓ ┃\n' +
  161. '┃ count() │ │ │ │ ✓ ┃\n' +
  162. '┗━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛\n';
  163. const DIAGRAM_8_A =
  164. 'BaseException\n' +
  165. ' +-- SystemExit';
  166. const DIAGRAM_8_B =
  167. "BaseException\n" +
  168. " ├── SystemExit <span class='hljs-comment'># Raised by the sys.exit() function.</span>\n" +
  169. " ├── KeyboardInterrupt <span class='hljs-comment'># Raised when the user hits the interrupt key (ctrl-c).</span>\n" +
  170. " └── Exception <span class='hljs-comment'># User-defined exceptions should be derived from this class.</span>\n" +
  171. " ├── ArithmeticError <span class='hljs-comment'># Base class for arithmetic errors.</span>\n" +
  172. " │ └── ZeroDivisionError <span class='hljs-comment'># Raised when dividing by zero.</span>\n" +
  173. " ├── AttributeError <span class='hljs-comment'># Raised when an attribute is missing.</span>\n" +
  174. " ├── EOFError <span class='hljs-comment'># Raised by input() when it hits end-of-file condition.</span>\n" +
  175. " ├── LookupError <span class='hljs-comment'># Raised when a look-up on a collection fails.</span>\n" +
  176. " │ ├── IndexError <span class='hljs-comment'># Raised when a sequence index is out of range.</span>\n" +
  177. " │ └── KeyError <span class='hljs-comment'># Raised when a dictionary key or set element is not found.</span>\n" +
  178. " ├── NameError <span class='hljs-comment'># Raised when a variable name is not found.</span>\n" +
  179. " ├── OSError <span class='hljs-comment'># Failures such as “file not found” or “disk full”.</span>\n" +
  180. " │ └── FileNotFoundError <span class='hljs-comment'># When a file or directory is requested but doesn't exist.</span>\n" +
  181. " ├── RuntimeError <span class='hljs-comment'># Raised by errors that don't fall in other categories.</span>\n" +
  182. " │ └── RecursionError <span class='hljs-comment'># Raised when the maximum recursion depth is exceeded.</span>\n" +
  183. " ├── StopIteration <span class='hljs-comment'># Raised by next() when run on an empty iterator.</span>\n" +
  184. " ├── TypeError <span class='hljs-comment'># Raised when an argument is of wrong type.</span>\n" +
  185. " └── ValueError <span class='hljs-comment'># When an argument is of right type but inappropriate value.</span>\n" +
  186. " └── UnicodeError <span class='hljs-comment'># Raised when encoding/decoding strings to/from bytes fails.</span>\n";
  187. const DIAGRAM_9_A =
  188. '+------------------+--------------+--------------+--------------+\n' +
  189. '| | excel | excel-tab | unix |\n' +
  190. '+------------------+--------------+--------------+--------------+\n';
  191. const DIAGRAM_9_B =
  192. "┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓\n" +
  193. "┃ │ excel │ excel-tab │ unix ┃\n" +
  194. "┠──────────────────┼──────────────┼──────────────┼──────────────┨\n" +
  195. "┃ delimiter │ ',' │ '\\t' │ ',' ┃\n" +
  196. "┃ quotechar │ '\"' │ '\"' │ '\"' ┃\n" +
  197. "┃ doublequote │ True │ True │ True ┃\n" +
  198. "┃ skipinitialspace │ False │ False │ False ┃\n" +
  199. "┃ lineterminator │ '\\r\\n' │ '\\r\\n' │ '\\n' ┃\n" +
  200. "┃ quoting │ 0 │ 0 │ 1 ┃\n" +
  201. "┃ escapechar │ None │ None │ None ┃\n" +
  202. "┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛\n";
  203. const DIAGRAM_10_A =
  204. '+-----------+------------+------------+------------+\n' +
  205. '| | list | dict | set |\n' +
  206. '+-----------+------------+------------+------------+\n';
  207. const DIAGRAM_10_B =
  208. '┏━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓\n' +
  209. '┃ │ list │ dict │ set ┃\n' +
  210. '┠───────────┼────────────┼────────────┼────────────┨\n' +
  211. '┃ getitem() │ IndexError │ KeyError │ ┃\n' +
  212. '┃ pop() │ IndexError │ KeyError │ KeyError ┃\n' +
  213. '┃ remove() │ ValueError │ │ KeyError ┃\n' +
  214. '┃ index() │ ValueError │ │ ┃\n' +
  215. '┗━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛\n';
  216. const DIAGRAM_11_A =
  217. '+-----------+-------------+------+-------------+\n' +
  218. '| sampwidth | min | zero | max |\n' +
  219. '+-----------+-------------+------+-------------+\n';
  220. const DIAGRAM_11_B =
  221. '┏━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━┯━━━━━━━━━━━━━┓\n' +
  222. '┃ sampwidth │ min │ zero │ max ┃\n' +
  223. '┠───────────┼─────────────┼──────┼─────────────┨\n' +
  224. '┃ 1 │ 0 │ 128 │ 255 ┃\n' +
  225. '┃ 2 │ -32768 │ 0 │ 32767 ┃\n' +
  226. '┃ 3 │ -8388608 │ 0 │ 8388607 ┃\n' +
  227. '┃ 4 │ -2147483648 │ 0 │ 2147483647 ┃\n' +
  228. '┗━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━┷━━━━━━━━━━━━━┛\n';
  229. const DIAGRAM_12_A =
  230. '+---------------+----------+----------+----------+----------+----------+\n';
  231. const DIAGRAM_12_B =
  232. '┏━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓\n' +
  233. '┃ │ [ !#$%…] │ [a-zA-Z] │ [¼½¾] │ [²³¹] │ [0-9] ┃\n' +
  234. '┠───────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨\n' +
  235. '┃ isprintable() │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  236. '┃ isalnum() │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  237. '┃ isnumeric() │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
  238. '┃ isdigit() │ │ │ │ ✓ │ ✓ ┃\n' +
  239. '┃ isdecimal() │ │ │ │ │ ✓ ┃\n' +
  240. '┗━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛\n';
  241. function main() {
  242. const html = getMd();
  243. initDom(html);
  244. modifyPage();
  245. const template = readFile('web/template.html');
  246. const tokens = template.split('<div id=main_container></div>');
  247. const text = `${tokens[0]} ${document.body.innerHTML} ${tokens[1]}`;
  248. writeToFile('index.html', text);
  249. }
  250. function getMd() {
  251. var readme = readFile('README.md');
  252. var readme = readme.replace("#semaphore-event-barrier", "#semaphoreeventbarrier");
  253. const converter = new showdown.Converter();
  254. return converter.makeHtml(readme);
  255. }
  256. function initDom(html) {
  257. const { JSDOM } = jsdom;
  258. const dom = new JSDOM(html);
  259. const $ = (require('jquery'))(dom.window);
  260. global.$ = $;
  261. global.document = dom.window.document;
  262. }
  263. function modifyPage() {
  264. removeOrigToc();
  265. addToc();
  266. insertLinks();
  267. unindentBanner();
  268. highlightCode();
  269. updateDiagrams();
  270. }
  271. function removeOrigToc() {
  272. const headerContents = $('#contents');
  273. const contentsList = headerContents.next();
  274. headerContents.remove();
  275. contentsList.remove();
  276. }
  277. function addToc() {
  278. const nodes = $.parseHTML(TOC);
  279. $('#main').before(nodes);
  280. }
  281. function insertLinks() {
  282. $('h2').each(function() {
  283. const aId = $(this).attr('id');
  284. const text = $(this).text();
  285. const line = `<a href="#${aId}" name="${aId}">#</a>${text}`;
  286. $(this).html(line);
  287. });
  288. }
  289. function unindentBanner() {
  290. const montyImg = $('img').first();
  291. montyImg.parent().addClass('banner');
  292. const downloadPraragrapth = $('p').first();
  293. downloadPraragrapth.addClass('banner');
  294. }
  295. function highlightCode() {
  296. setApaches(['<D>', '<T>', '<DT>', '<TD>', '<a>', '<n>']);
  297. $('code').not('.python').not('.text').not('.bash').not('.apache').addClass('python');
  298. $('code').each(function(index) {
  299. hljs.highlightBlock(this);
  300. });
  301. fixClasses();
  302. fixHighlights();
  303. preventPageBreaks();
  304. fixPageBreaksFile();
  305. fixPageBreaksStruct();
  306. insertPageBreaks();
  307. }
  308. function setApaches(elements) {
  309. for (el of elements) {
  310. $(`code:contains(${el})`).addClass('apache');
  311. }
  312. }
  313. function fixClasses() {
  314. // Changes class="hljs-keyword" to class="hljs-title" of 'class' keyword.
  315. $('.hljs-class').filter(':contains(class \')').find(':first-child').removeClass('hljs-keyword').addClass('hljs-title')
  316. }
  317. function fixHighlights() {
  318. $(`code:contains(os.rename)`).html(OS_RENAME);
  319. $(`code:contains(shutil.copy)`).html(SHUTIL_COPY);
  320. $(`code:contains(ValueError: malformed node)`).html(EVAL);
  321. $(`code:contains(@lru_cache(maxsize=None))`).html(LRU_CACHE);
  322. $(`code:contains(\'<class_name>\', <parents_tuple>, <attributes_dict>)`).html(TYPE);
  323. $(`code:contains(make_dataclass(\'<class_name>\')`).html(DATACLASS);
  324. $(`code:contains((<DT>))`).html(DATETIME)
  325. }
  326. function preventPageBreaks() {
  327. $(':header').each(function(index) {
  328. var el = $(this)
  329. var untilPre = el.nextUntil('pre')
  330. var untilH2 = el.nextUntil('h2')
  331. if ((untilPre.length < untilH2.length) || el.prop('tagName') === 'H1') {
  332. untilPre.add(el).next().add(el).wrapAll("<div></div>");
  333. } else {
  334. untilH2.add(el).wrapAll("<div></div>");
  335. }
  336. });
  337. }
  338. function fixPageBreaksFile() {
  339. const modesDiv = $('#file').parent().parent().parent()
  340. move(modesDiv, 'file')
  341. move(modesDiv, 'exceptions-1')
  342. }
  343. function fixPageBreaksStruct() {
  344. const formatDiv = $('#floatingpointtypes').parent().parent().parent().parent()
  345. move(formatDiv, 'floatingpointtypes')
  346. move(formatDiv, 'integertypesuseacapitalletterforunsignedtypestandardsizesareinbrackets')
  347. move(formatDiv, 'forstandardsizesstartformatstringwith')
  348. }
  349. function move(anchor_el, el_id) {
  350. const el = $('#'+el_id).parent()
  351. anchor_el.after(el)
  352. }
  353. function insertPageBreaks() {
  354. insertPageBreakBefore('#print')
  355. }
  356. function insertPageBreakBefore(an_id) {
  357. $('<div class="pagebreak"></div>').insertBefore($(an_id).parent())
  358. }
  359. function updateDiagrams() {
  360. $(`code:contains(${DIAGRAM_1_A})`).html(DIAGRAM_1_B);
  361. $(`code:contains(${DIAGRAM_2_A})`).html(DIAGRAM_2_B);
  362. $(`code:contains(${DIAGRAM_3_A})`).html(DIAGRAM_3_B);
  363. $(`code:contains(${DIAGRAM_4_A})`).html(DIAGRAM_4_B);
  364. $(`code:contains(${DIAGRAM_5_A})`).html(DIAGRAM_5_B);
  365. $(`code:contains(${DIAGRAM_6_A})`).html(DIAGRAM_6_B);
  366. $(`code:contains(${DIAGRAM_7_A})`).html(DIAGRAM_7_B);
  367. $(`code:contains(${DIAGRAM_8_A})`).html(DIAGRAM_8_B);
  368. $(`code:contains(${DIAGRAM_9_A})`).html(DIAGRAM_9_B);
  369. $(`code:contains(${DIAGRAM_10_A})`).html(DIAGRAM_10_B);
  370. $(`code:contains(${DIAGRAM_11_A})`).html(DIAGRAM_11_B);
  371. $(`code:contains(${DIAGRAM_12_A})`).html(DIAGRAM_12_B);
  372. }
  373. function readFile(filename) {
  374. try {
  375. return fs.readFileSync(filename, 'utf8');
  376. } catch(e) {
  377. console.error('Error:', e.stack);
  378. }
  379. }
  380. function writeToFile(filename, text) {
  381. try {
  382. return fs.writeFileSync(filename, text, 'utf8');
  383. } catch(e) {
  384. console.error('Error:', e.stack);
  385. }
  386. }
  387. main();