#!/usr/bin/env node // Usage: node test.js // Script that creates index.html out of web/template.html and README.md. // It is written in JS because this code used to be executed on the client side. // To install dependencies run: // $ npm install -g jsdom jquery showdown highlightjs // If running on mac and modules cant be found after instalation add: // export NODE_PATH=/usr/local/lib/node_modules // to the ~/.bash_profile or ~/.bashrc file and run '$ bash'. const fs = require('fs'); const jsdom = require('jsdom'); const showdown = require('showdown'); const hljs = require('highlightjs'); const TOC = '
' + '

Contents

\n' + '
ToC = {\n' +
  '    \'1. Collections\': [List, Dict, Set, Range, Enumerate, Namedtuple, Iterator, Generator],\n' +
  '    \'2. Types\':       [Type, String, Regex, Format, Numbers, Combinatorics, Datetimeᴺᴱᵂ],\n' +
  '    \'3. Syntax\':      [Arguments, Splat, Inline, Closure, Decorator, Class, Enum, Exceptions],\n' +
  '    \'4. System\':      [Print, Input, Command_Line_Arguments, Open, Pathᴺᴱᵂ, Command_Execution],\n' +
  '    \'5. Data\':        [CSV, JSON, Pickle, SQLite, Bytes, Struct, Array, MemoryView, Deque],\n' +
  '    \'6. Advanced\':    [Threading, Introspection, Metaprograming, Operator, Eval, Coroutine],\n' +
  '    \'7. Libraries\':   [Progress_Bar, Plot, Table, Curses, Loggingᴺᴱᵂ, Scraping, Web, Profile,\n' +
  '                       NumPy, Image, Audio]\n' +
  '}\n' +
  '
\n'; const MRO = '
>>> C.mro()\n[<class \'C\'>, <class \'A\'>, <class \'B\'>, <class \'object\'>]\n
\n' function main() { const html = getMd(); initDom(html); modifyPage(); const template = readFile('web/template.html'); const tokens = template.split('
'); const text = `${tokens[0]} ${document.body.innerHTML} ${tokens[1]}`; writeToFile('index.html', text); } function initDom(html) { const { JSDOM } = jsdom; const dom = new JSDOM(html); const $ = (require('jquery'))(dom.window); global.$ = $; global.document = dom.window.document; } function getMd() { const readme = readFile('README.md'); const converter = new showdown.Converter(); return converter.makeHtml(readme); } function modifyPage() { removeOrigToc(); addToc(); insertLinks(); unindentBanner(); highlightCode(); } function removeOrigToc() { const headerContents = $('#contents'); const contentsList = headerContents.next(); headerContents.remove(); contentsList.remove(); } function addToc() { const nodes = $.parseHTML(TOC); $('#main').before(nodes); } function insertLinks() { $('h2').each(function() { const aId = $(this).attr('id'); const text = $(this).text(); const line = `#${text}`; $(this).html(line); }); } function unindentBanner() { const montyImg = $('img').first(); montyImg.parent().addClass('banner'); const downloadPraragrapth = $('p').first(); downloadPraragrapth.addClass('banner'); } function highlightCode() { setApache('') setApache('') setApache('
') setApache('') setApache('') setApache('') $('code').not('.python').not('.text').not('.bash').not('.apache').addClass('python'); $('code').each(function(index) { hljs.highlightBlock(this); }); $('#copy').prev().remove() const nodes = $.parseHTML(MRO); $('#copy').before(nodes); } function setApache(codeContents) { $(`code:contains(${codeContents})`).addClass('apache'); } function readFile(filename) { try { return fs.readFileSync(filename, 'utf8'); } catch(e) { console.error('Error:', e.stack); } } function writeToFile(filename, text) { try { return fs.writeFileSync(filename, text, 'utf8'); } catch(e) { console.error('Error:', e.stack); } } main();