hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Tuesday

2005.08.30

YAML is Parseltongue #

by why in inspect

Hey, Python finally has a YAML implementation! PySyck came out of nowhere today. And we’re not talking a flimsy half-finished extension. This thing has a pile of features, no joke. Let us begin communication with our coiled friends.

YAML twin powers, ACTIVATE! Form of.. steam! Suddenly, it seems crazy that we were the ones to first adopt this whitespacey language. And I thought you called yourselves skypunchers. (Well done, Kirill Simonov!)

Hobix and Rails Wed Unceremoniously Off Behind the Podomatic #

by why in cult

I know you’re expecting some towering squid-glued-to-a-shark to come reaching around the buildings yelling, “BLAHHHG!” But it’s not that. I can’t say I know exactly how it works (and you’re probably going to say it rings of a hoax) but this new podcaster’s toolkit site—Podomatic—uses Rails for its controllers and templates (of course) and Hobix, the dorky little content engine used here on RedHanded, for its backend.

Justin Dossey, the Justice of the Peace, says:

We provide anyone with their own podcast blog and all the associated bits and pieces like search, folksonomy, recommendations, and an online media library. Hobix is flexible enough that it wasn’t a big stretch to add an email-like facility for sharing audio files. You can record new audio directly through the browser using a flash applicaation, and there are some flash tools on the site that allow you to create multi-part recordings (think intro and outro).

I would have never expected this. But I can see where he’s coming from. Sometimes a database is too constraining. (seen on hobix-is-the-way.)

MUD in 15 Lines of Ruby #

by why in bits

This is great. In response to a post about how writing a MUD in Ruby would be “a horrifically complex and lengthy project,” Jon Lambert dropped a 15-line MUD in the response! Now that’s a comeback.

 require 'socket';require 'yaml';def q x;$m.find{|o|o.t==:p&&x==o.n};end
 def a r,t;$m.find_all{|o|t==o.t&&(!r||r==o.l)};end;def g z;a(nil,:p).each{|p|
 p.p z};end;class O;attr_accessor :i,:n,:l,:e,:s,:t;def initialize n,l=nil,t=:r
 @n,@l,@i,@t=n,l,$d+=1,t;@e={};end;def p s;@s.puts(s)if @s;end;def y m
 v=$m.find{|o|@l==o.i};t=v.e.keys;case m;when/^q/;@s.close;@s=nil;
 File.open('d','w'){|f|YAML::dump $m,f};when/^h/;p "i,l,d,g,c,h,q,<exit>,O,R" 
 when/^i/;a(@i,:o).each{|o|p o.n};when/^c.* (.*)/;g "#{@n}:#{$1}" 
 when/^g/;a(@l,:o).each{|q|q.l=@i};when/^d/;a(@i,:o).each{|q|q.l=@l}
 when/^O (.*)/;$m<<O.new($1,@l,:o);when/^R (.*) (.*) (.*)/;$m<<d=O.new($1)
 v.e[$2]=d.i;d.e[$3]=v.i;when/^l/;p v.n;(a(@l,:p)+a(@l,:o)).each{|x|
 p x.n if x.s||x.t==:o};p t.join '|';when/(^#{t.empty? ? "\1" : t.join('|^')})/
 @l=v.e[$1];else;p "?";end;end;end;test ?e,'d'||begin;$d=0;$m=[O.new("Home")]
 end;$m=YAML::load_file 'd';$d=$m.size;z=TCPServer.new 0,4000;while k=z.accept
 Thread.new(k){|s|s.puts "Name";s.gets;l=$_.chomp;d=q l;$m<<d=O.new(l,1,:p)if !d
 d.s=s;while s.gets;d.y $_.chomp;end;};end

A bit of sleuthing on the matter and I’ve discovered that this is a derivative of Jon’s TeensyMud. He has nice, readable versions in his repository, if you like.

If you want to play with this one:

  1. Save the above to teensymud.rb.
  2. Create a YAML file named “d” (for dungeon) with a blank array in it. (Likeso: echo "[]" > d.)
  3. Run teensymud.rb.
  4. Telnet to localhost port 4000.
  5. At Name prompt, enter your name then <Enter>.
Play commands
i displays player inventory
l displays the contents of a room
dr drops all objects in your inventory into the room
g gets all objects in the room into your inventory
k (name) attempts to kill player (e.g. k bubba)
s (message) sends (message) to all players in the room
c (message) sends (message) to all players in the game
q quits the game (saves player)
(exit name) moves player through exit named (ex. south)
OLC
O (object name) creates a new object (ex. O rose)
R (room name) (exit name to) (exit name back) creates a new room and autolinks the exits using the exit names provided.

Please, tell us more, Jon. If you’re feeling nostalgic and want to egg things on, come join ruby-talk this week. The Ruby Quiz will be a MUD client. (extracted from ruby-talk:154208.)

A MouseHole Example: Showing Alt Text #

by why in inspect

I figured some of you might want to tinker with a simple MouseHole user script, so I translated one of the most basic GreaseMonkey strips: comicalt.user.js by Adam Vandenberg, which reveals the alt text for a comic strip right below the strip.

The MouseHole equivalent (userScripts/comicalt.user.rb) looks like so:

 MouseHole.script do
   # declaration
   name "Comics Alt Text" 
   namespace "http://adamv.com/greases/" 
   description 'Shows the "hover text" for some comics on the page'
   include_match %r!^http://.*achewood\.com/!
   include_match %r!^http://.*qwantz\.com/!
   version "0.2" 

   # instance variables
   @comics = {
     'achewood' => '//img[starts-with(@src, "/comic.php?date=")]',
     'qwantz'   => '//img[starts-with(@src, "/comics/")]'
   }

   # the pages flow through here
   rewrite do |req, res|
     whichSite = case req.request_uri.host
                 when /achewood/: "achewood" 
                 when /qwantz/: "qwantz" 
                 end
     return unless whichSite

     comic = document.elements[@comics[whichSite]]
     return unless comic

     if comic.attributes['title']
       div = Element.new 'div'
       div.attributes['className'] = 'msg'
       div.text = "(#{ comic.attributes['title'] })" 
       comic.parent.insert_after comic, div
     end
   end
 end

In all, very similiar to Greasemonkey. The declaration has been moved into the code. The include and exclude directives have been replaced with include_match and exclude_match, which both take regexps.

The start method receives WEBrick’s HTTPRequest and HTTPResponse objects. You can use these to read headers or to add headers.

The HTML itself is parsed by MouseHole and stuck in the user script’s @document instance variable. Modify the document with REXML and the altered document will be delivered back to the browser when you’re done. REXML is mixed into MouseHole, so you can refer to Element and Document without the module name.

Updated! For MouseHole version 1.1. More user script examples available on the wiki at UserScripts.