datafy.rb

Explanation

datafy.rb is a tool to take a file and create a data: URI for it. The data: URI scheme is described in RFC 2397. In short, if its a text file (MIME type text/*), the file is URL-encoded, the URI is built like this: "data:" + MIME type + "," + URL-encoded file contents datafying "Here is some text to datafy." with text/plain as the content type looks like this: data:text/plain,Here%20is%20some%20text%20to%20datafy.%0A

For files with a binary MIME type (anything other than text/*), the file is base64 encoded and the URI is built like this: "data:" + MIME type + ";base64," + base64-encoded file contents datafying "Here is some text to datafy." with application/octet-stream as the content type looks like this: data:application/octet-stream;base64,SGVyZSBpcyBzb21lIHRleHQgdG8gZGF0YWZ5Lgo=

Usage

datafy.rb can be called with a filename and will attempt to determine the file using file(1) if it exists on your system, and supports the -i option for outputting the MIME type, otherwise the mime type must be specified with -m. datafy.rb can also read a file from stdin, the mime type must be specified in this mode.

datafy.rb can also be called with -u url and will download the url and its attached pages (img src=, link href=), datafy them, make relative a href links absolute, and send the page to stdout.

Examples:

$ echo 'Here is some text to datafy.' | ./datafy.rb -m text/plain
data:text/plain,Here%20is%20some%20text%20to%20datafy.%0A
$ echo 'Here is some text to datafy.' | ./datafy.rb -m application/octet-stream
data:application/octet-stream;base64,SGVyZSBpcyBzb21lIHRleHQgdG8gZGF0YWZ5Lgo=

Download

Get datafy.rb, you will need Ruby to run datafy.rb, if you don't have it, you can download ruby here. There are also unit tests.

Notes for Mac OSX users

Depending on what you use to download this file, you may have problems with line endings. To fix this, open the downloaded script in your favorite text editor and remove the '^M's (depending on your editor they may look like funny boxes or something). (Since I don't have a Mac, I can't give you instructions on how.)

Subpages: