Render Whitespace on GitHub

Renders spaces as · and tabs as → in all the code on GitHub.

اعتبارا من 06-09-2017. شاهد أحدث إصدار.

/**
The MIT License (MIT)

Copyright (c) 2017 Gleb Mazovetskiy

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/
// ==UserScript==
// @id              RenderWhitespace
// @name            Render Whitespace on GitHub
// @description     Renders spaces as · and tabs as → in all the code on GitHub.
// @namespace       https://github.com/glebm
// @version         1.0.0
// @author          Gleb Mazovetskiy <[email protected]>
// @domain          github.com
// @domain          gist.github.com
// @match           https://gist.github.com/*
// @match           https://github.com/*
// @homepageUrl     https://gist.github.com/5b6c4517322193fbc51090dc3b57a44a
// @run-at          document-end
// @contributionURL https://etherchain.org/account/0x962644db6d8735446c1af84a2c1f16143f780184
// ==/UserScript==


var SPACE = '·';
var TAB = '→';
var WHITESPACE_OPACITY = 0.78;

var ROOT_SELECTOR = 'table[data-tab-size]';
var NODE_FILTER = {
    acceptNode(node) {
        let parent = node.parentNode;
        while (parent.nodeName != 'TABLE') {
            if (parent.classList.contains('blob-code-inner')) {
                return !(parent.firstChild === node && node.nodeValue === ' ') ?
                    NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
            }
            parent = parent.parentNode;
        }
        return NodeFilter.FILTER_SKIP;
    }
};

function showWhitespace() {
    for (const root of document.querySelectorAll(ROOT_SELECTOR)) {
        const tab = TAB.padEnd(+root.dataset.tabSize);
        const treeWalker =
            document.createTreeWalker(root, NodeFilter.SHOW_TEXT, NODE_FILTER);
        const nodes = [];
        while (treeWalker.nextNode()) nodes.push(treeWalker.currentNode);
        for (const node of nodes) replaceWhitespace(node, tab, SPACE);
    }
}

function replaceWhitespace(node, tab, space) {
    const tabParts = node.nodeValue.split('\t');
    const tabSpaceParts = tabParts.map(s => s.split(' '));
    if (tabSpaceParts.length === 1 && tabSpaceParts[0].length === 1) return;
    const parent = node.parentNode;
    const insert = (newNode) => {
        parent.insertBefore(newNode, node);
    };
    insertParts(tabSpaceParts,
        spaceParts => spaceParts.length === 1 && spaceParts[0] === '',
        n => insert(createWhitespaceNode(tab.repeat(n))),
        spaceParts =>
            insertParts(spaceParts,
                text => text === '',
                n => insert(createWhitespaceNode(space.repeat(n))),
                text => insert(document.createTextNode(text))));
    parent.removeChild(node);
}

function createWhitespaceNode(text) {
    const node = document.createElement('span');
    node.textContent = text;
    node.style.opacity = WHITESPACE_OPACITY;
    return node;
}

function insertParts(parts, isConsecutiveFn, addInterFn, addPartFn) {
    const n = parts.length;
    parts.reduce((consecutive, part, i) => {
        const isConsecutive = isConsecutiveFn(part);
        if (isConsecutive) {
            if (i !== n - 1) return consecutive + 1;
        }
        if (consecutive > 0) addInterFn(consecutive);
        if (!isConsecutive) addPartFn(part);
        return 1;
    }, 0);
}

showWhitespace();