Applying highlight.js to inline code on WordPress

If you're using the plugin Prismatic to apply highlight.js to your code excerpts, but you want it to also affect inline <code> tags (i.e. those not wrapped in <pre> blocks), simply go Settings > Prismatic > Highlight.js and paste this monster into the “Init Script” field: // This init script was written by James E.…

Parsing the HTTP "Date" header in Python

As per RFC 7231 §7.1.1.1: A recipient that parses a timestamp value in an HTTP header field MUST accept all three HTTP-date formats. These formats are then described as (with the first being the only preferred format; the latter 2 are designated as "obsolete"), converted for this post into strftime(3) syntax: %a, %d %b %Y…

Basic JS Async Cookbook

// aXMLHttpRequest: an asynchronous XHR function function aXMLHttpRequest(method, location, responseType="", body=undefined) { // somewhat basic; improvements welcome return new Promise( (resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open(method, location); xhr.responseType = responseType; xhr.onload = resolve; xhr.onerror = reject; xhr.send(body); return xhr; }); } // asleep: an asynchronous sleep function asleep = (delay =>…

JS: Tail-call optimized recursive setTimeout

If you're writing some code on a time-delayed-loop in JavaScript, there are 2 main options: setInterval setTimeout, recursively However, each has a potential downside: setInterval schedules the code to run at the given interval, regardless of whether the last invocation has completed. This means that, if the function takes longer than the delay to execute,…