Skip to content

Instantly share code, notes, and snippets.

View tommorris's full-sized avatar

Tom Morris tommorris

View GitHub Profile
@tommorris
tommorris / gist:3736
Created August 2, 2008 13:49
detects OS versions
class OsVersion
attr_reader :ruby_platform, :os, :ident, :version, :build
def initialize
@ruby_platform = RUBY_PLATFORM
if File.exists?("/proc/version")
@os = :linux
@ident = File.open("/proc/version").readlines.join
elsif `sw_vers`.length != 0
@os = :macosx
@version = `sw_vers -productVersion`.chop!
<?php
function tidyInput($input) {
exec("echo " . escapeshellarg($input) . " | /usr/bin/tidy --indent auto --indent-spaces 2 --output-xhtml yes --show-errors 0 --show-warnings no --quiet yes --tidy-mark no --show-body-only yes", $response);
return implode("\n", $response);
}
/* this is for use in my blogging application to clean up fragments of XHTML on my own site. Clean your inputs and prevent failure. :) -tom */
?>
@tommorris
tommorris / gist:8113
Created August 30, 2008 14:36
A one-liner to clean up some messy text files
puts File.open("/Users/tommorris/tmp/latin.txt").readlines.collect {|l| l.strip }.uniq.sort * "\n"
@tommorris
tommorris / gist:8138
Created August 30, 2008 19:57
A script to check for spam on Gist
require 'open-uri'
require 'hpricot'
def isAnonymousGist(url)
(Hpricot(open(url))/"div[@class = 'file']").each { |i|
if (i/"span[@class = 'date']/a[@href]").size == 0
url = "http://gist.github.com" + (i/"div[@class = 'info']/span/a")[0]['href']
system "open \"" + url + "\""
puts url
end
@tommorris
tommorris / gist:9216
Created September 7, 2008 00:16
Example code for parsing HTML with libxml-ruby
require 'xml'
hp = XML::HTMLParser.string("<html><body><p>pooey</body></html>")
a = hp.parse
a.find("//p").to_a[0].first.to_s
<?xml version="1.0"?>
<log began="2008-09-24 10:36:03 +0100" source="irc://irc.freenode.net/%23vocamp">
<event id="T02T6X8L2Y1" name="memberParted" occurred="2008-09-24 10:46:09 +0100">
<message><span class="member">FabGando1</span> left the chat room.</message>
<who hostmask="n=fgandon@electra.wolf.ox.ac.uk" identifier="fabgando1">FabGando1</who>
<reason>Read error: 110 (Connection timed out)</reason>
</event>
<event id="A6G56H9L2Y1" name="topicChanged" occurred="2008-09-24 10:46:29 +0100">
<message>You changed the topic to "VoCampOxford".</message>
<by self="yes" hostmask="tommorris@89.193.223.220">tommorris</by>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:irc="http://irc-edelweiss.inria.fr/2008/09/irc/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns="http://www.w3.org/1999/xhtml" xmlns:sioc="http://rdfs.org/sioc/ns#">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="text()" />
<xsl:template match="log">
<html>
<head profile="http://ns.inria.fr/grddl/rdfa/">
<title>IRC Logger for <xsl:value-of select="@source"/></title>
<style type="text/css">
body {
@prefix : <http://tommorris.org/ns/vote#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sioc: <http://rdfs.org/sioc/ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<http://purl.org/dc/terms/references> a rdf:Property .
<http://purl.org/dc/terms/subject> a rdf:Property .
@tommorris
tommorris / gist:15524
Created October 8, 2008 14:29
A place where I put the stuff I reckon should go into Ruby Core
class Fixnum
def divisible?(by)
if by.class == Array
self.divisible_by_all?(by)
elsif by.class == Range
self.divisible_by_all?(by.to_a)
elsif by.class == String
self.divisible?(by.to_i)
else
self.divmod(by)[1] == 0
" ,g - create google a href for word under cursor
map ,g <Esc>Bm`:r!ghref <cword><CR>"gdd``i<C-R><C-R>g<esc>dwi<BackSpace><Esc>
vmap ,g yvgvdm`:r!ghref '<C-R>=substitute(@0, "['\n]", " ", "g")<cr>'<cr>"gdd``i<C-R><C-R>g<Esc>i<BackSpace><Esc>