Skip to content

Instantly share code, notes, and snippets.

@mudge
mudge / pdftk.rb
Created Apr 23, 2021
Homebrew Cask for PDFtk
View pdftk.rb
cask "pdftk" do
version "2.02"
sha256 "c33cf95151e477953cd57c1ea9c99ebdc29d75f4c9af0d5f947b385995750b0c"
url "https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-#{version}-mac_osx-10.11-setup.pkg"
name "PDFtk"
desc "Tool for doing everyday things with PDF documents"
homepage "https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/"
pkg "pdftk_server-2.02-mac_osx-10.11-setup.pkg"
@mudge
mudge / data_uri.rb
Last active Mar 1, 2021
A Ruby regular expression to parse data URIs based on RFC 2397.
View data_uri.rb
require 'base64'
class DataUri
REGEXP = %r{
data:
(?<mediatype>
(?<mimetype> .+? / .+? )?
(?<parameters> (?: ; .+? = .+? )* )
)?
(?<extension>;base64)?
@mudge
mudge / blocklist.sh
Last active Oct 6, 2020
A Bash script to generate an Unbound configuration to block all domains on The Firebog's "The Big Blocklist Collection" and allow all domains on Anudeep's list
View blocklist.sh
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
# Download all ticked blocklists from The Firebog's "The Big Blocklist
# Collection" [0] and block access to them with Unbound by redirecting traffic
# to 0.0.0.0.
#
# [0]: https://firebog.net
(
@mudge
mudge / pythagorean_means.rb
Last active May 20, 2021
A Ruby refinement to add methods to Enumerable for calculating the three Pythagorean means.
View pythagorean_means.rb
# A refinement to add methods to Enumerables for calculating the three
# Pythagorean means.
#
# See https://en.wikipedia.org/wiki/Pythagorean_means
module PythagoreanMeans
# Note that due to a bug refining modules in Ruby 2.7 [1], we can't `refine
# Enumerable` so we `refine Array` instead.
#
# See also https://interblah.net/why-is-nobody-using-refinements
@mudge
mudge / auto.sh
Created Aug 26, 2019
Auto-switch Node.js versions with chnode by reading .node-version files
View auto.sh
# Based off chruby's auto.sh: https://github.com/postmodern/chruby#auto-switching
unset NODE_AUTO_VERSION
function chnode_auto() {
local dir="$PWD/" version
until [[ -z "$dir" ]]; do
dir="${dir%/*}"
if { read -r version <"$dir/.node-version"; } 2>/dev/null || [[ -n "$version" ]]; then
@mudge
mudge / use-debounce.js
Last active Oct 16, 2021
A custom React Hook for a debounced click handler with a given callback and delay.
View use-debounce.js
/*
* Inspired by Dan Abramov's "Making setInterval Declarative with React Hooks",
* this is a custom hook for debouncing a callback (e.g. for click handlers) such
* that a callback will not be fired until some delay has passed since the last click.
* The callback will automatically be updated with the latest props and state on every
* render meaning that users don't need to worry about stale information being used.
*
* See https://overreacted.io/making-setinterval-declarative-with-react-hooks/ for the
* original inspiration.
*/
@mudge
mudge / README.md
Last active May 11, 2021
Compiling & installing cloudflared for DNS-over-HTTPS on a Raspberry Pi Model B
View README.md
@mudge
mudge / dijkstra_with_fibonacci_heap.rb
Last active Jul 15, 2018
Implementing Dijkstra's algorithm with Fibonacci Heap
View dijkstra_with_fibonacci_heap.rb
require 'fibonacci_heap'
# Assuming `graph` is an object with the following interface that stores vertices as `FibonacciHeap::Node`
# instances and `source` is a `FibonacciHeap::Node`:
#
# * `graph.vertices`: return an Enumerable of all vertices in the graph
# * `graph.neighbours(u)`: return an Enumerable of all vertices that neighbour a given vertex `u`
# * `graph.length(u, v)`: return the numeric weight of the edge between two given vertices `u` and `v`
def dijkstra(graph, source)
dist = Hash.new(Float::INFINITY)
@mudge
mudge / fibonacci_heap.rb
Last active Jun 24, 2018
A Ruby implementation of a Fibonacci heap
View fibonacci_heap.rb
class CircularDoublyLinkedList
include Enumerable
class Node
include Comparable
attr_accessor :key, :next, :prev
alias right next
alias left prev