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.

130 lines
5.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 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. 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. highlightCode();
  55. }
  56. function removeOrigToc() {
  57. const headerContents = $('#contents');
  58. const contentsList = headerContents.next();
  59. headerContents.remove();
  60. contentsList.remove();
  61. }
  62. function addToc() {
  63. const nodes = $.parseHTML(TOC);
  64. $('#main').before(nodes);
  65. }
  66. function insertLinks() {
  67. $('h2').each(function() {
  68. const aId = $(this).attr('id');
  69. const text = $(this).text();
  70. const line = `<a href="#${aId}" name="${aId}">#</a>${text}`;
  71. $(this).html(line);
  72. });
  73. }
  74. function unindentBanner() {
  75. const montyImg = $('img').first();
  76. montyImg.parent().addClass('banner');
  77. const downloadPraragrapth = $('p').first();
  78. downloadPraragrapth.addClass('banner');
  79. }
  80. function highlightCode() {
  81. setApaches(['<D>', '<T>', '<DT>', '<TD>', '<a>', '<n>']);
  82. $('code').not('.python').not('.text').not('.bash').not('.apache').addClass('python');
  83. $('code').each(function(index) {
  84. hljs.highlightBlock(this);
  85. });
  86. fixClasses()
  87. }
  88. function setApaches(elements) {
  89. for (el of elements) {
  90. $(`code:contains(${el})`).addClass('apache');
  91. }
  92. }
  93. function fixClasses() {
  94. // Changes class="hljs-keyword" to class="hljs-title" of 'class' keyword.
  95. $('.hljs-class').filter(':contains(class \')').find(':first-child').removeClass('hljs-keyword').addClass('hljs-title')
  96. }
  97. function readFile(filename) {
  98. try {
  99. return fs.readFileSync(filename, 'utf8');
  100. } catch(e) {
  101. console.error('Error:', e.stack);
  102. }
  103. }
  104. function writeToFile(filename, text) {
  105. try {
  106. return fs.writeFileSync(filename, text, 'utf8');
  107. } catch(e) {
  108. console.error('Error:', e.stack);
  109. }
  110. }
  111. main();