#!/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 =
'
' +
'
ToC = {\n' +
' \'1. Collections\': [List, Dictionary, Set, Tuple, Range, Enumerate, Iterator, Generator],\n' +
' \'2. Types\': [Type, String, Regular_Exp, Format, Numbers, Combinatorics, Datetime],\n' +
' \'3. Syntax\': [Args, Inline, Closure, Decorator, Class, Duck_Types, Enum, Exceptions],\n' +
' \'4. System\': [Print, Input, Command_Line_Arguments, Open, Path, Command_Execution],\n' +
' \'5. Data\': [JSON, Pickle, CSV, SQLite, Bytes, Struct, Array, MemoryView, Deque],\n' +
' \'6. Advanced\': [Threading, Operator, Introspection, Metaprograming, Eval, Coroutine],\n' +
' \'7. Libraries\': [Progress_Bar, Plot, Table, Curses, Logging, Scraping, Web, Profile,\n' +
' NumPy, Image, Animation, Audio, Synthesizer]\n' +
'}\n' +
'
\n';
const OS_RENAME =
'os.rename(from, to) # Renames the file or directory.\n' +
'os.replace(from, to) # Same, but overwrites \'to\' if it exists.\n';
const SHUTIL_COPY =
'shutil.copy(from, to) # Copies the file.\n' +
'shutil.copytree(from, to) # Copies the entire directory tree.\n';
const EVAL =
'from ast import literal_eval\n' +
' literal_eval(\'1 + 2\')\n' +
'3\n' +
' literal_eval(\'[1, 2, 3]\')\n' +
'[1, 2, 3]\n' +
' literal_eval(\'abs(1)\')\n' +
'ValueError: malformed node or string\n';
const LRU_CACHE =
'from functools import lru_cache\n' +
'\n' +
' \n' +
'def fib(n):\n' +
' return n if n < 2 else fib(n-2) + fib(n-1)\n';
const TYPE =
'<class> = type(\'<class_name>\', <parents_tuple>, <attributes_dict>)
';
const DATACLASS =
'from dataclasses import make_dataclass\n' +
'<class> = make_dataclass(\'<class_name>\', <coll_of_attribute_names>)\n' +
'<class> = make_dataclass(\'<class_name>\', <coll_of_tuples>)\n' +
'<tuple> = (\'<attr_name>\', <type> [, <default_value>])
';
const DATETIME =
'\'<DT> = resolve_imaginary(<DT>)\'
';
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() {
var 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() {
setApaches(['