The Accidental Rubyist

invalid byte sequence in UTF-8

Ecto: URLIFY script

leave a comment »

I have made two varieties of a script that will make hyperlinks from a favorites list.

The first uses the form of square brackets as in the following paragraph. Notice that I can use both [ruby] and [ruby|Ruby].

In the first case, “ruby” will be displayed; in the second case, “Ruby” will be displayed. In both cases, “ruby” is used to fetch the value from the url dictionary. The input is:

teh prog; lang; of my choice is [ruby]. Now don't [get] me wrogn, i like
[perl|Perl] and [python|Python], too. I love perl's regular expressions. And I like many
things about [ruby|Programmer's Best Friend].

Notice the hyperlinking in the output:

the programming language of my choice is ruby. Now don’t [get] me wrong, i like
Perl and Python, too. I love perl’s regular expressions. And I like many things about Programmer’s Best Friend.

If you noticed that spelllings have been fixed, yes, in eagerness I ran the “abbr” script too.

So here is the script which does the urlify’ing:

#!/usr/bin/env ruby
require "yaml"
file = ARGV[0];
text = File.open(file,"r").readlines.join;
urlmap = YAML::load( File.open("urls.yaml"));
text.gsub!(/\[(.+?)\]/) { |match|
  str = $1.split('|')
  path=urlmap[str[0]]
  if path == nil then
    puts "%s not found in urlmap" % str[0]
    match
  else
    if str.size == 1 then
      str << str[0];
    end
    ('<a href="' + path + '">%s</a>') % str[1]
  end
}
File.open(file, "w").puts text;

The URL dictionary is a YAML file in this format:

ruby: http://ruby-lang.org
perl: http://perl.org
python: http://python.org
ecto: http://ecto.kung-foo.tv/

The script and the Yaml file. For some reason, ecto looks in the root for the yaml, so it must be placed there, i.e. in “/”.

In the next post, I will show a program that does NOT require any tags to be placed in the post. It searches for the first occurrence of the URL key and replaces with URL. However, that has 2 issues which will be presented in that post.

Advertisements

Written by totalrecall

March 1, 2007 at 3:37 pm

Posted in ecto, ruby

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: