Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thecodewarrior/6be181009a86fca2d33888f0fa00f78a to your computer and use it in GitHub Desktop.
Save thecodewarrior/6be181009a86fca2d33888f0fa00f78a to your computer and use it in GitHub Desktop.
Re-formats the angle bracket email quoting from a mailing list as actual indentation levels with nice lines. For use with TamperMonkey.
// ==UserScript==
// @name Swift-Evolution Email Formatting
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Formats the nested `>` indents of the mailing list into depth-marking lines
// @author TheCodeWarrior
// @match https://lists.swift.org/pipermail/swift-evolution/*
// @grant none
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==
(function() {
'use strict';
// The list of selectors that find all the email root elements.
let email_selectors = ['body > pre'];
// This should match any unwanted separators between the `>`s and the lines. If the separator is part of the `>`'s text node this isn't required. To disable set this to `null`
let line_prefix_removal = /^ /;
function addGlobalStyle(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
$(document).ready(function() {
addGlobalStyle(`
.line-indent-wrap > i {
display: inline-block;
}
.line-indent {
display: inline-block;
margin-left: 1.5em;
border-left: 1px solid gray;
padding-left: 1em;
}
`);
function removeFirstSpace(jq) {
if(line_prefix_removal !== null) {
var elems = jq.contents();
if(elems.length > 0) {
var first = elems[0];
if(first.nodeType === 3) {
first.nodeValue = first.nodeValue.replace(line_prefix_removal, "");
}
if(first.nodeType === 1) {
removeFirstSpace($(first));
}
}
}
}
var doIt = true;
var lastTag = null;
$(email_selectors.join(', ')).contents().each(function () {
if (this.nodeType === 3) {
if(doIt) {
var newTag = $('<div class="line-indent-wrap"></div>');
this.parentNode.insertBefore(newTag[0], this);
this.parentNode.removeChild(this);
lastTag = newTag;
var currentText = $.trim($(this).text());
currentText.replace(/^\s*>*/, function(result) {
var count = $.trim(result).length;
for (var i = 0; i < count; i++) {
var tag = $('<span class="line-indent"></span>');
lastTag.append(tag);
lastTag = tag;
}
});
if(lastTag === newTag) {
newTag.append($(this));
doIt = currentText.endsWith("\n");
}
} else {
lastTag.append($(this));
doIt = $(this).text().endsWith("\n");
}
}
if (this.nodeType === 1) {
var jq = $(this);
doIt = jq.text().endsWith("\n");
if(lastTag !== null) {
jq.remove();
removeFirstSpace(jq);
lastTag.append(jq);
}
}
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment