Skip to content

Instantly share code, notes, and snippets.

@mscalora
mscalora / .htaccess
Created August 11, 2019 13:08
Htaccess to force all html to be processed by PHP
# for Dreamhost, need to adjust FcgidWrapper path for other hosts
AddHandler fcgid-script .html
FcgidWrapper "/dh/cgi-system/php72.cgi" .html
const encs = {json: 'application/json', text: 'text/plain', url: 'application/x-www-form-urlencoded', form: 'multipart/form-data'},
xhrRequestDefaults = {method: 'POST', encoding: 'url', beforeCallback: function () {}, openedCallback: function () {}};
/**
* Make http request to url with object data as body
* @param {string} url
* @param {(object | string)} [data] - post data, formatting controlled by encoding
* @param {object} [options] - object of options
* @param {string} [options.encoding] - body encoding 'none', 'url', 'form', 'json', 'text' or mime (content) type, default: 'url'
* @param {string} [options.contentType] - override automatic contentType, null disable automatic without sending one
* @param {function} [options.method] - 'GET', 'POST', 'PUT', etc, default: 'POST'
#!/bin/bash
# clone a user
# usage:
# if you named this as below then
# change to the directory and run this command
# sudo bash clone-user.sh
#
# from: OregonJohn - https://unix.stackexchange.com/questions/204970/clone-linux-user-copy-user-based-on-another-one
@mscalora
mscalora / fitted-text-with-svg.markdown
Created August 26, 2018 23:31
Fitted Text with SVG
@mscalora
mscalora / tn-survey-hoisting-custom.js
Created May 26, 2018 12:57
Hoist surveys up into the article body on TownNews sites using The World Table
function twtInsertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
setTimeout(function () {
var d = document, $q = d.querySelector.bind(d), $g = d.getElementById.bind(d);
if (!$q('#twt-engagement-tool-widget, .twt-engagement-tool-widget-container')) {
var i, e = document.createElement('div');
e.id = 'twt-engagement-tool-widget';
var paras = d.querySelectorAll('[itemprop="articleBody"] p');
@mscalora
mscalora / parse-emails-from-text.js
Created March 21, 2018 16:53
Parse emails from text
parseEmails: function (emailStr) {
// remove RFC-822 adornments & normalize delimiters
var rfc822Cleaner = /(?:"[^@"'<>]*?"|(?:^|\n| |,|;)(?:[^@"'<>])+?)\s*?<([-a-z0-9_.%+]+?@[-a-z0-9.]+?.[-a-z0-9]{2,63}?)>/gi,
exchangeCleaner = /"(?:[^@"()])*?\(([-a-z0-9_.%+]+?@[-a-z0-9.]+?.[-a-z0-9]{2,63})\)(?:[^@"()])*?"\s*?<\1>/gi,
cleanedStr = emailStr.replace(rfc822Cleaner, ' $1 ').replace(exchangeCleaner, ' $1 ').replace(/(\n|[;, ])+/g, ';'),
emailList = _.split(cleanedStr, ';');
var re = /(?:^|\s|\n|[;,<"'(])([-a-z0-9_.%+]+@[-a-z0-9.]+?.[-a-z0-9]{2,63})(?:^|\s|\n|[;,>"')])/gi,
emails = [],
dummy = emailStr.replace(re, function (match, email) { emails.push(email); return '[' + email + ']';});
@mscalora
mscalora / cmpdirs.py
Created February 18, 2018 22:07
Based on Andriy Makukha's script from https://askubuntu.com/questions/421712/comparing-the-contents-of-two-directories/996529#996529 with a third parameter of a skip/exclude regular expression
#!/usr/bin/env python3
import os, sys, re
skip = re.compile(sys.argv[3] if len(sys.argv) > 3 else '$.')
def compare_dirs(d1: "old directory name", d2: "new directory name"):
def print_local(a, msg):
print('DIR ' if a[2] else 'FILE', a[1], msg)
# ensure validity
@mscalora
mscalora / geti
Created February 18, 2018 00:01
Case-insensitive get like lodash's _.get()
function geti (value, prop) {
if (_.isPlainObject(value)) {
if (_.isString(prop) && prop !== '') {
return geti(value, prop.split('.'));
} else if (_.isArray(prop) && prop.length) {
const key = _.toLower(prop.shift()),
val = Object.keys(value).reduce(function (a, k) {
if (a !== undefined) {
return a;
}
@mscalora
mscalora / humanizeDuration.php
Created January 9, 2018 12:25
Return a duration (in seconds) in a humanized string format like: "2 seconds" or "1 year"
function humanizeDuration ($secs) {
$secs = ($secs<1)? 1 : $secs;
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
@mscalora
mscalora / build-nano.md
Created October 28, 2017 13:33
Building nano editor from source recipe for mac and debian ubuntu mint

build nano

nano build dependencies

on a generic Debian distro

sudo apt-get install autoconf autopoint automake gettext git groff pkg-config texinfo gcc make libncursesw5-dev

on a mac