#!/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\':        [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' + '@lru_cache(maxsize=None)\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(['', '', '
', '', '', '']); $('code').not('.python').not('.text').not('.bash').not('.apache').addClass('python'); $('code').each(function(index) { hljs.highlightBlock(this); }); fixClasses(); fixHighlights(); preventPageBreaks(); fixPageBreaksFile(); fixPageBreaksStruct(); insertPageBreaks(); } 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 fixHighlights() { $(`code:contains(os.rename)`).html(OS_RENAME); $(`code:contains(shutil.copy)`).html(SHUTIL_COPY); $(`code:contains(ValueError: malformed node)`).html(EVAL); $(`code:contains(@lru_cache(maxsize=None))`).html(LRU_CACHE); $(`code:contains(\'\', , )`).html(TYPE); $(`code:contains(make_dataclass(\'\')`).html(DATACLASS); $(`code:contains((
))`).html(DATETIME) } 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 fixPageBreaksFile() { const modesDiv = $('#file').parent().parent().parent() move(modesDiv, 'file') move(modesDiv, 'exceptions-1') } function fixPageBreaksStruct() { const formatDiv = $('#floatingpointtypes').parent().parent().parent().parent() move(formatDiv, 'floatingpointtypes') move(formatDiv, 'integertypesusecapitalletterforunsignedtypestandardsizesareinbrackets') move(formatDiv, 'forstandardsizesstartformatstringwith') } function move(anchor_el, el_id) { const el = $('#'+el_id).parent() anchor_el.after(el) } function insertPageBreaks() { // insertPageBreakBefore('#libraries') insertPageBreakBefore('#print') } function insertPageBreakBefore(an_id) { $('
').insertBefore($(an_id).parent()) } 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();