#!/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, 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\':        [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 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" + "| | {} | {:f} | {:e} | {:%} |\n" + "+----------------+----------------+---------------+----------------+-----------------+\n" + "| 0.000056789 | '5.6789e-05' | '0.000057' | '5.678900e-05' | '0.005679%' |\n" + "| 0.00056789 | '0.00056789' | '0.000568' | '5.678900e-04' | '0.056789%' |\n" + "| 0.0056789 | '0.0056789' | '0.005679' | '5.678900e-03' | '0.567890%' |\n" + "| 0.056789 | '0.056789' | '0.056789' | '5.678900e-02' | '5.678900%' |\n" + "| 0.56789 | '0.56789' | '0.567890' | '5.678900e-01' | '56.789000%' |\n" + "| 5.6789 | '5.6789' | '5.678900' | '5.678900e+00' | '567.890000%' |\n" + "| 56.789 | '56.789' | '56.789000' | '5.678900e+01' | '5678.900000%' |\n" + "| 567.89 | '567.89' | '567.890000' | '5.678900e+02' | '56789.000000%' |\n" + "+----------------+----------------+---------------+----------------+-----------------+\n"; const DIAGRAM_5_B = "┏━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┓\n" + "┃ │ {} │ {:f} │ {:e} │ {:%} ┃\n" + "┠────────────────┼────────────────┼───────────────┼────────────────┼─────────────────┨\n" + "┃ 0.000056789 │ '5.6789e-05' │ '0.000057' │ '5.678900e-05' │ '0.005679%' ┃\n" + "┃ 0.00056789 │ '0.00056789' │ '0.000568' │ '5.678900e-04' │ '0.056789%' ┃\n" + "┃ 0.0056789 │ '0.0056789' │ '0.005679' │ '5.678900e-03' │ '0.567890%' ┃\n" + "┃ 0.056789 │ '0.056789' │ '0.056789' │ '5.678900e-02' │ '5.678900%' ┃\n" + "┃ 0.56789 │ '0.56789' │ '0.567890' │ '5.678900e-01' │ '56.789000%' ┃\n" + "┃ 5.6789 │ '5.6789' │ '5.678900' │ '5.678900e+00' │ '567.890000%' ┃\n" + "┃ 56.789 │ '56.789' │ '56.789000' │ '5.678900e+01' │ '5678.900000%' ┃\n" + "┃ 567.89 │ '567.89' │ '567.890000' │ '5.678900e+02' │ '56789.000000%' ┃\n" + "┗━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┛\n"; const DIAGRAM_6_A = "+----------------+----------------+---------------+----------------+-----------------+\n" + "| | {:.2} | {:.2f} | {:.2e} | {:.2%} |\n" + "+----------------+----------------+---------------+----------------+-----------------+\n" + "| 0.000056789 | '5.7e-05' | '0.00' | '5.68e-05' | '0.01%' |\n" + "| 0.00056789 | '0.00057' | '0.00' | '5.68e-04' | '0.06%' |\n" + "| 0.0056789 | '0.0057' | '0.01' | '5.68e-03' | '0.57%' |\n" + "| 0.056789 | '0.057' | '0.06' | '5.68e-02' | '5.68%' |\n" + "| 0.56789 | '0.57' | '0.57' | '5.68e-01' | '56.79%' |\n" + "| 5.6789 | '5.7' | '5.68' | '5.68e+00' | '567.89%' |\n" + "| 56.789 | '5.7e+01' | '56.79' | '5.68e+01' | '5678.90%' |\n" + "| 567.89 | '5.7e+02' | '567.89' | '5.68e+02' | '56789.00%' |\n" + "+----------------+----------------+---------------+----------------+-----------------+\n"; const DIAGRAM_6_B = "┏━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┓\n" + "┃ │ {:.2} │ {:.2f} │ {:.2e} │ {:.2%} ┃\n" + "┠────────────────┼────────────────┼───────────────┼────────────────┼─────────────────┨\n" + "┃ 0.000056789 │ '5.7e-05' │ '0.00' │ '5.68e-05' │ '0.01%' ┃\n" + "┃ 0.00056789 │ '0.00057' │ '0.00' │ '5.68e-04' │ '0.06%' ┃\n" + "┃ 0.0056789 │ '0.0057' │ '0.01' │ '5.68e-03' │ '0.57%' ┃\n" + "┃ 0.056789 │ '0.057' │ '0.06' │ '5.68e-02' │ '5.68%' ┃\n" + "┃ 0.56789 │ '0.57' │ '0.57' │ '5.68e-01' │ '56.79%' ┃\n" + "┃ 5.6789 │ '5.7' │ '5.68' │ '5.68e+00' │ '567.89%' ┃\n" + "┃ 56.789 │ '5.7e+01' │ '56.79' │ '5.68e+01' │ '5678.90%' ┃\n" + "┃ 567.89 │ '5.7e+02' │ '567.89' │ '5.68e+02' │ '56789.00%' ┃\n" + "┗━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┛\n"; const DIAGRAM_7_A = '+------------+----------+------------+----------+--------------+\n' + '| | Iterable | Collection | Sequence | abc.Sequence |\n' + '+------------+----------+------------+----------+--------------+\n' + '| iter() | REQ | REQ | yes | yes |\n' + '| contains() | yes | yes | yes | yes |\n' + '| len() | | REQ | REQ | REQ |\n' + '| getitem() | | | REQ | REQ |\n' + '| reversed() | | | yes | yes |\n' + '| index() | | | | yes |\n' + '| count() | | | | yes |\n' + '+------------+----------+------------+----------+--------------+\n'; const DIAGRAM_7_B = '┏━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━━━┓\n' + '┃ │ Iterable │ Collection │ Sequence │ abc.Sequence ┃\n' + '┠────────────┼──────────┼────────────┼──────────┼──────────────┨\n' + '┃ iter() │ ! │ ! │ ✓ │ ✓ ┃\n' + '┃ contains() │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' + '┃ len() │ │ ! │ ! │ ! ┃\n' + '┃ getitem() │ │ │ ! │ ! ┃\n' + '┃ reversed() │ │ │ ✓ │ ✓ ┃\n' + '┃ index() │ │ │ │ ✓ ┃\n' + '┃ count() │ │ │ │ ✓ ┃\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'; 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(['', '', '
', '', '', '']); $('code').not('.python').not('.text').not('.bash').not('.apache').addClass('python'); $('code').each(function(index) { hljs.highlightBlock(this); }); fixClasses(); fixFroms(); preventPageBreaks(); } function setApaches(elements) { for (el of elements) { $(`code:contains(${el})`).addClass('apache'); } } function fixClasses() { // Changes class="hljs-keyword" to class="hljs-title" of 'class' keyword. $('.hljs-class').filter(':contains(class \')').find(':first-child').removeClass('hljs-keyword').addClass('hljs-title') } function fixFroms() { $(`code:contains(os.rename)`).html(OS_RENAME); } function preventPageBreaks() { $(':header').each(function(index) { var el = $(this) var untilPre = el.nextUntil('pre') var untilH2 = el.nextUntil('h2') if ((untilPre.length < untilH2.length) || el.prop('tagName') === 'H1') { untilPre.add(el).next().add(el).wrapAll("
"); } else { untilH2.add(el).wrapAll("
"); } }); } 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();