Skip to content

Instantly share code, notes, and snippets.

View henrik's full-sized avatar

Henrik Nyh henrik

View GitHub Profile
@henrik
henrik / closed_struct.rb
Created April 16, 2009 19:15
Like OpenStruct but raises for anything not passed into the constructor.
# Like OpenStruct but raises for anything not passed into the constructor.
# Unlike Struct in that it's built from a hash, and you don't need to define your own class.
#
# By Henrik Nyh <http://henrik.nyh.se> 2009-04-16. Public domain.
require 'ostruct'
class ClosedStruct < OpenStruct
def method_missing(symbol, *args)
raise(NoMethodError, "undefined method `#{symbol}' for #{self}")
end
@henrik
henrik / gfm.rb
Created April 16, 2009 23:23 — forked from mojombo/gfm.rb
GitHub Flavored Markdown
# GitHub Flavored Markdown Layer
def fix_markdown_quirks(text)
# prevent foo_bar_baz from ending up with an italic word in the middle
text.gsub!(/(\w+_\w+_\w[\w_]*)/) do |x|
x.gsub('_', '\_') if x.split('').sort.to_s[0..1] == '__'
end
# in very clear cases, let newlines become <br /> tags
text.gsub!(/(\A|\n\n)(^\w[^\n]*\n)(^\w[^\n]*\n)+/m) do |x|
x.gsub(/^(.+)\n/, "\\1 \n")
# Illustrates an RDiscount issue.
#
# Seen in the wild in Jekyll (http://github.com/mojombo/jekyll) blog posts
# where <div><pre> is used for syntax highlighting.
require 'rubygems'
require 'rdiscount'
require 'maruku'
md = DATA.read
@henrik
henrik / .htaccess
Created April 18, 2009 23:15
Insane mod_rewrite gymnastics for my blog setup.
RewriteEngine On
# Map http://henrik.nyh.se to /jekyll.
RewriteRule ^$ /jekyll/ [L]
# Map http://henrik.nyh.se/x to /jekyll/x unless there is a x in the web root.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/jekyll/
RewriteRule ^(.*)$ /jekyll/$1
@henrik
henrik / wordpress.rb
Created April 22, 2009 22:33
Standard Jekyll WordPress import script modified for tags and some custom fixups.
require 'rubygems'
require 'sequel'
require 'fileutils'
require 'cgi'
# NOTE: This converter requires Sequel and the MySQL gems.
# The MySQL gem can be difficult to install on OS X. Once you have MySQL
# installed, running the following commands should work:
# $ sudo gem install sequel
# $ sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>bundleUUID</key>
<string>5A9D4FC6-6CBE-11D9-A21B-000D93589AF6</string>
<key>command</key>
<string>#!/usr/bin/env ruby
#!/bin/bash
ssh mini "osascript -e 'tell app \"Spotify\" to activate' -e 'tell app \"System Events\" to keystroke (ASCII character 29) using {command down}'"
@henrik
henrik / database.yml
Created July 13, 2009 12:21
Simple per-branch databases in Rails dev. Also different adapters for JRuby vs. MRI.
shared: &shared
# Use different adapters with JRuby vs. MRI
adapter: <%= defined?(JRuby) ? "jdbcmysql" : "mysql" %>
encoding: utf8
username: root
password:
host: localhost
socket: /opt/local/var/run/mysql5/mysqld.sock
<%
@henrik
henrik / hash_deep_diff.rb
Created July 14, 2009 08:38
Recursively diff two Ruby hashes.
# Recursively diff two hashes, showing only the differing values.
# By Henrik Nyh <http://henrik.nyh.se> 2009-07-14 under the MIT license.
#
# Example:
#
# a = {
# "same" => "same",
# "diff" => "a",
# "only a" => "a",
# "nest" => {
@henrik
henrik / application.html.erb
Created August 26, 2009 14:14
Simple breadcrumb trail for Rails. MIT license. Will likely spec and pluginize later.
You are here:
<%= trail.to_html(response) %>