Skip to content

Instantly share code, notes, and snippets.

View mdchaney's full-sized avatar

Michael Chaney mdchaney

View GitHub Profile
@mdchaney
mdchaney / fix_encoding.rb
Last active August 2, 2025 11:35
Fix encoding to deal with mixed UTF-8 / Latin-1
def fix_encoding(str)
# The "b" method returns a copied string with encoding ASCII-8BIT
str = str.b
# Strip UTF-8 BOM if it's at start of file
if str.byteslice(0..2) == "\xEF\xBB\xBF".b
str = str.byteslice(3..-1)
end
if str.ascii_only?
@mdchaney
mdchaney / webpage-screenshot.rb
Created July 17, 2025 18:29
Take a picture of a web page from the command line
#!/usr/bin/env ruby
require 'ferrum'
require 'optparse'
require 'uri'
class WebScreenshotter
def initialize(options = {})
@options = {
width: 1920,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Five Squares Puzzle Demo</title>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
@mdchaney
mdchaney / english_casual.ths
Last active March 4, 2025 15:20
English dictionary for PostgreSQL search
gonna : going to
wanna : want to
gotta : got to
ain't : am not : are not : is not : has not : have not
lemme : let me
gimme : give me
dunno : do not know
kinda : kind of
lotta : lot of
outta : out of
@mdchaney
mdchaney / unaccent.js
Last active October 14, 2024 05:00
JavaScript unaccent - remove diacritics from utf-8 strings
const ACCENT_MAP = {
" ":" ","¨":" ","ª":"a","¯":" ","²":"2","³":"3","´":" ","¸":" ","¹":"1",
"º":"o","À":"A","Á":"A","Â":"A","Ã":"A","Ä":"A","Å":"A","Æ":"AE","Ç":"C",
"È":"E","É":"E","Ê":"E","Ë":"E","Ì":"I","Í":"I","Î":"I","Ï":"I","Ñ":"N",
"Ò":"O","Ó":"O","Ô":"O","Õ":"O","Ö":"O","Ø":"O","Ù":"U","Ú":"U","Û":"U",
"Ü":"U","Ý":"Y","à":"a","á":"a","â":"a","ã":"a","ä":"a","å":"a","æ":"ae",
"ç":"c","è":"e","é":"e","ê":"e","ë":"e","ì":"i","í":"i","î":"i","ï":"i",
"ñ":"n","ò":"o","ó":"o","ô":"o","õ":"o","ö":"o","ø":"o","ù":"u","ú":"u",
"û":"u","ü":"u","ý":"y","ÿ":"y","Ā":"A","ā":"a","Ă":"A","ă":"a","Ą":"A",
"ą":"a","Ć":"C","ć":"c","Ĉ":"C","ĉ":"c","Ċ":"C","ċ":"c","Č":"C","č":"c",
@mdchaney
mdchaney / hash_fields_helper.rb
Created September 17, 2024 23:19
Turn an arbitrary hash into hidden fields on a Rails form
module HashFieldsHelper
def flatten_hash(hash, ancestor_names = [])
hash.map do |k, v|
case v
when Hash
flatten_hash(v, ancestor_names + [k])
when Array
v.map { |item| flatten_hash(item, ancestor_names + [k, '']) }.inject(:merge)
else
{flat_hash_key(ancestor_names + [k]) => v.to_s}
import { Controller } from "@hotwired/stimulus"
// Connects to data-controller="table-sortable-headers"
export default class extends Controller {
static targets = [ "sortingHeader", "sortableRow", "sortableTbody" ];
static values = {
sortColumn: { type: String, default: "" },
sortDirection: { type: String, default: "" }
};
@mdchaney
mdchaney / _form.html.erb.tt
Created July 11, 2024 17:26
Bootstrap-styled view generators for Ruby on Rails
<%%= form_for([<%= model_resource_name %>]) do |form| %>
<%%= show_errors_for form.object %>
<% attributes.each do |attribute| -%>
<div class="mb-3">
<% if attribute.password_digest? -%>
<%%= form.label :password, class: "form-label" %>
<%%= form.password_field :password, class: "form-control", required: form.object.new_record? %>
</div>
@mdchaney
mdchaney / fix_encoding.rb
Created June 25, 2024 16:09
fix_encoding_2
module FixEncoding
def FixEncoding.remove_bom(str)
if str.byteslice(0..2) == "\xEF\xBB\xBF".b
return str.byteslice(3..-1)
else
return str
end
end
def FixEncoding.has_utf8?(str)
@mdchaney
mdchaney / fix_encoding.rb
Last active June 25, 2024 04:16
Fix encoding of dubious string in Ruby
module FixEncoding
def FixEncoding.fix_encoding(str)
# The "b" method returns a copied string with encoding ASCII-8BIT
str = str.b
# Strip UTF-8 BOM if it's at start of file
if str =~ /\A\xEF\xBB\xBF/n
str = str.gsub(/\A\xEF\xBB\xBF/n, '')
end
if str =~ /([\xc0-\xff][\x80-\xbf]{1,3})+/n
# String has actual UTF-8 characters