The Accidental Rubyist

invalid byte sequence in UTF-8

Power Blogging from Unix (and Vim)

leave a comment »

The benefit of using a unix shell (Unix/Linux/Mac OS X) for blogging is not just that one can use one’s favorite editor, such as Vim, or even something absolutely freakingly great such as html.vim, but also …

One painful part of blogging can be creating hyperlinks. There are links in your browser bookmark file, and links in your previous blogs sitting on your system, why not use these automatically. OK, we don’t expect that fancy, shiny GUI blogging system to really work up so much sweat. But a few lines of your favorite scripting language, a.k.a ruby, can easily do some amount of hyperlinking for you.

So i have touched up 3 scripts i wrote while trying out Ecto :
1. inter.rb runs first on the body of this post. it interchanges all Drupal interwiki style formatting into links to wikipedia, google etc.
2. urlize.rb runs next on the BODY, replacing all single bracket tags with whats in my URL file, created from posts i have already sent.
3. Finally, urlize.rb takes care of any words in its list, that it finds in the post (delimited with space before and a space or comma or period after.
4. If i wish to search my bookmarks for a specific URL, F5 invokes which takes a word/phrase, searches the bookmarks file and presents me with a list. I can select one, and the URL is placed in clipboard, ready to paste in.

The URL files used by the ruby scripts are in YAML format, created by a simple one-liner on my bash shell.

Some programs allow one to get to the frontmost URL of safari (standard browser on MAC). That’s lame, really. What with multiple windows and tabs. And what of closed tabs, and my history?

The above scripts are all in files folder. I guess as i keep blogging from Vim, more will come out. I am posting this to WordPress using AND to Blogger using (which speaks the Moveable Type format pretty fluently.

The command to do the magic linking (in .vimrc) is:
nmap \l :/BODY:/,$ ! inter.rb <bar> urlify.rb <bar> urlize.rb
(that’s a “forward-slash l” in case the blog lops it off)

My apologies to any mouse-warriors and GUI-masochists reading this post.
(All links above were automatically created!)

A command like the following can be used to create the YAML file from your posts (apologies for poor scripting)

cat  *.html |
ruby -e 'm=STDIN.readlines.to_s.scan(/(http:.*?)">(.*?)<\/[aA]/) puts m' |
ruby -pe '$_ = $_.chomp + ": " + gets if $. % 2'  |
 sed 's/\(.*\):\(.*\)/\2: \1/p' > urls.txt

Line one pumps the files into the filter.
Line 2 scans out URLs and the text. Due to my poor scripting abilities, they come on separate lines.
Line 3 in ruby combines line one and two.
Line 4 in sed switches column one with two, so the text comes first. And also places a “:” in between so it becomes a YAML hash.

Actually i used a shorter sed portion to join consecutive lines:

cat  *.html |
ruby -e 'm=STDIN.readlines.to_s.scan(/(http:.*?)">(.*?)<\/[aA]/) puts m' |
sed '$!N;s/\n/: /'   |
sed 's/\(.*\): \(.*\)/\2: \1/p'

The important part is:
sed ‘$!N;s/\n/: /’

(Update: WP printed this but at up the <BAR>. Blogger kept barfing on the post due to the code samples, since it requires strict XHTML.)

[Edited in Vim using HTML.vim]


Written by totalrecall

March 15, 2007 at 12:33 am

Posted in blogging, ruby, unix

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: