Skip to content

Instantly share code, notes, and snippets.

@robmiller
robmiller / mdtable.rb
Created February 6, 2024 17:55
Script for aligning a Markdown table neatly. Put in your PATH as mdtable then use in Vim visual mode: !mdtable
#!/usr/bin/env ruby
table = ARGF.read.chomp
parse_row = ->(row) { row.gsub(/^\||\|$/, "").split("|").map(&:strip) }
rows = table.each_line.map(&parse_row)
# Normalise column count
column_count = rows.map(&:length).max
rows = rows.map { |row| (row + ([""] * column_count)).take(column_count) }
@robmiller
robmiller / pangram.rb
Created July 28, 2023 08:43
Check if a string is a pangram in Ruby.
class String
def pangram?
(("a".."z").to_a - self.downcase.chars.uniq).empty?
end
end
["The quick brown fox jumped over the lazy dogs!", "The slow dog didn't"].each do |text|
puts "#{text}: #{text.pangram?}"
end
@robmiller
robmiller / kramdown_block_ids.rb
Created January 20, 2022 21:11
Add automatically generated unique IDs to block elements (p, ul, blockquote, etc.) generated by kramdown, which allows you to link to any paragraph on a page
class Kramdown::Converter::Html
def add_block_count(attr)
@block_count ||= 0
@block_count += 1
unless attr["id"]
attr["id"] = "b:#{@block_count}"
end
end
%i(format_as_block_html format_as_indented_block_html).each do |method|
@robmiller
robmiller / play-wordle.rb
Last active February 15, 2022 19:40
A command-line, offline version of Wordle. A new word every time you run it.
#!/usr/bin/env ruby
#
# Play a command-line version of Wordle
#
# Original game by Josh Wardle: https://www.powerlanguage.co.uk/wordle/
#
# Installation and usage:
#
# 1. Save this file somewhere as play-wordle.rb
# 2. Run `ruby play-wordle.rb`
#!/usr/bin/env ruby
#
# Trying to guess what the best first-choice word in Wordle might be.
#
# Author: Rob Miller <r@robm.me.uk>
# Boosts the score of a word that matches the first letter, with the
# rationale that getting the first letter makes the word easier for
# a human being to guess
FIRST_LETTER_WEIGHT = 2
@robmiller
robmiller / delete-tweets.rb
Created July 15, 2021 15:59
Script for deleting tweets from a Twitter data export
#!/usr/bin/env ruby
gem "twitter", "~> 7.0"
gem "http", "~> 4.4"
require "twitter"
require "http"
require "json"
require "yaml"
@robmiller
robmiller / refresh-skim.scpt
Created June 14, 2021 11:15
AppleScript for refreshing Skim PDF
tell application "Skim"
repeat with n from (count of documents) to 1 by -1
set doc to document n
if name of doc is "book-screen.pdf" then
revert doc
end if
end repeat
end tell
@robmiller
robmiller / fixes.sh
Last active February 19, 2021 13:52
Stuff that happened to me when migrating to an M1 Mac
# The macOS command line tools broke, so I had to reinstall them
# (I kept getting the error "Your CLT does not support macOS 11."
# when compiling packages from Homebrew.)
sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install
# Homebrew had old x86 libraries and object files and god knows what
# else hanging around, which caused several packages to break during install
# (including Ruby) so I reinstalled Homebrew and all my packages
brew list > ~/homebrew.tmp.txt
#!/usr/bin/env ruby
#
# A "correct horse battery staple"-style password generator. Outputs
# four random, fairly common (among the 5,000 most common words) English
# words, contracted together with hyphens.
#
# Author: Rob Miller <r@robm.me.uk>
puts DATA.each_line.to_a.sample(4).map(&:chomp).join("-")
#!/usr/bin/env ruby
#
# lightroom-groups.rb
# Author: Rob Miller <r@robm.me.uk>
#
# Mass organises presets into groups in Lightroom, based on their folder
# on disk.
#
# --
#