#!/usr/bin/env node const fs = require('fs'); const cheerio = require('cheerio'); const entities = require('html-entities'); const hljs = require('./build/highlight.js'); const githublink = `\
  • \ \ \ https://github.com/dtolnay/cxx\ \
  • `; const opengraph = `\ \ \ \ \ \ \ `; const htmljs = `\ var html = document.querySelector('html'); html.classList.remove('no-js'); html.classList.add('js');`; const dirs = ['build']; while (dirs.length) { const dir = dirs.pop(); fs.readdirSync(dir).forEach((entry) => { path = dir + '/' + entry; const stat = fs.statSync(path); if (stat.isDirectory()) { dirs.push(path); return; } if (!path.endsWith('.html')) { return; } const index = fs.readFileSync(path, 'utf8'); const $ = cheerio.load(index, { decodeEntities: false }); $('head').append(opengraph); $('script:nth-of-type(3)').text(htmljs); $('nav#sidebar ol.chapter').append(githublink); $('head link[href="tomorrow-night.css"]').attr('disabled', true); $('head link[href="ayu-highlight.css"]').attr('disabled', true); $('button#theme-toggle').attr('style', 'display:none'); $('pre code').each(function () { const node = $(this); const langClass = node.attr('class').split(' ', 2)[0]; if (!langClass.startsWith('language-')) { return; } const lang = langClass.replace('language-', ''); const lines = node.html().split('\n'); const boring = lines.map((line) => line.includes('') ); const ellipsis = lines.map((line) => line.includes('// ...')); const target = entities.decode(node.text()); const highlighted = hljs.highlight(lang, target).value; const result = highlighted .split('\n') .map(function (line, i) { if (boring[i]) { line = '' + line; } else if (ellipsis[i]) { line = '' + line; } if (i > 0 && (boring[i - 1] || ellipsis[i - 1])) { line = '' + line; } return line; }) .join('\n'); node.text(result); node.removeClass(langClass); if (!node.hasClass('focuscomment')) { node.addClass('hidelines'); node.addClass('hide-boring'); } }); $('code').each(function () { $(this).addClass('hljs'); }); const out = $.html(); fs.writeFileSync(path, out); }); } fs.copyFileSync('build/highlight.css', 'build/tomorrow-night.css'); fs.copyFileSync('build/highlight.css', 'build/ayu-highlight.css'); var bookjs = fs.readFileSync('build/book.js', 'utf8'); bookjs = bookjs .replace('set_theme(theme, false);', '') .replace('document.querySelectorAll("code.hljs")', 'document.querySelectorAll("code.hidelines")'); fs.writeFileSync('build/book.js', bookjs);