#!/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 DIAGRAM_1_A =
'+---------+-------------+\n' +
'| Classes | Metaclasses |\n' +
'+---------+-------------|\n' +
'| MyClass > MyMetaClass |\n' +
'| | v |\n' +
'| object ---> type <+ |\n' +
'| | ^ +---+ |\n' +
'| str -------+ |\n' +
'+---------+-------------+\n';
const DIAGRAM_1_B =
'┏━━━━━━━━━┯━━━━━━━━━━━━━┓\n' +
'┃ Classes │ Metaclasses ┃\n' +
'┠─────────┼─────────────┨\n' +
'┃ MyClass → MyMetaClass ┃\n' +
'┃ │ ↓ ┃\n' +
'┃ object ───→ type ←╮ ┃\n' +
'┃ │ ↑ ╰───╯ ┃\n' +
'┃ str ───────╯ ┃\n' +
'┗━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
const DIAGRAM_2_A =
'+---------+-------------+\n' +
'| Classes | Metaclasses |\n' +
'+---------+-------------|\n' +
'| MyClass | MyMetaClass |\n' +
'| v | v |\n' +
'| object <--- type |\n' +
'| ^ | |\n' +
'| str | |\n' +
'+---------+-------------+\n';
const DIAGRAM_2_B =
'┏━━━━━━━━━┯━━━━━━━━━━━━━┓\n' +
'┃ Classes │ Metaclasses ┃\n' +
'┠─────────┼─────────────┨\n' +
'┃ MyClass │ MyMetaClass ┃\n' +
'┃ ↓ │ ↓ ┃\n' +
'┃ object ←─── type ┃\n' +
'┃ ↑ │ ┃\n' +
'┃ str │ ┃\n' +
'┗━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
const DIAGRAM_3_A =
'+------------------+----------+------------+----------+\n' +
'| | Sequence | Collection | Iterable |\n' +
'+------------------+----------+------------+----------+\n' +
'| list, range, str | yes | yes | yes |\n' +
'| dict, set | | yes | yes |\n' +
'| iter | | | yes |\n' +
'+------------------+----------+------------+----------+\n';
const DIAGRAM_3_B =
'┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━┓\n' +
'┃ │ Sequence │ Collection │ Iterable ┃\n' +
'┠──────────────────┼──────────┼────────────┼──────────┨\n' +
'┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ dict, set │ │ ✓ │ ✓ ┃\n' +
'┃ iter │ │ │ ✓ ┃\n' +
'┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┛\n';
const DIAGRAM_4_A =
'+--------------------+----------+----------+------+---------+--------+\n' +
'| | Integral | Rational | Real | Complex | Number |\n' +
'+--------------------+----------+----------+------+---------+--------+\n' +
'| int | yes | yes | yes | yes | yes |\n' +
'| fractions.Fraction | | yes | yes | yes | yes |\n' +
'| float | | | yes | yes | yes |\n' +
'| complex | | | | yes | yes |\n' +
'+--------------------+----------+----------+------+---------+--------+\n';
const DIAGRAM_4_B =
'┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━┯━━━━━━━━━┯━━━━━━━━┓\n' +
'┃ │ Integral │ Rational │ Real │ Complex │ Number ┃\n' +
'┠────────────────────┼──────────┼──────────┼──────┼─────────┼────────┨\n' +
'┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ fractions.Fraction │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ float │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ complex │ │ │ │ ✓ │ ✓ ┃\n' +
'┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━┷━━━━━━━━━┷━━━━━━━━┛\n';
const DIAGRAM_5_A =
"+----------------+----------------+---------------+----------------+-----------------+\n" +
"| | {<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_val>])
';
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');
// readme = switchClassDiagrams(readme);
const converter = new showdown.Converter();
return converter.makeHtml(readme);
}
function switchClassDiagrams(readme) {
readme = readme.replace(DIAGRAM_1_A, DIAGRAM_1_B);
readme = readme.replace(DIAGRAM_2_A, DIAGRAM_2_B);
readme = readme.replace(DIAGRAM_3_A, DIAGRAM_3_B);
readme = readme.replace(DIAGRAM_4_A, DIAGRAM_4_B);
readme = readme.replace(DIAGRAM_5_A, DIAGRAM_5_B);
readme = readme.replace(DIAGRAM_6_A, DIAGRAM_6_B);
readme = readme.replace(DIAGRAM_7_A, DIAGRAM_7_B);
return 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(['