The Accidental Rubyist

invalid byte sequence in UTF-8

Publishing code on this blog through Ecto

leave a comment »

Background: All attempts to publish some simple source code on this blog failed. Be it directly on WP or through Ecto. PRE and CODE tags made no difference. How many tags I might have to manually change, i do not know. But < and \ we certainly disappearing !

And so now, I run the code samples (sniff) through vim’s TOhtml plugin, and copy the resultant HTML here. Now I know that this has generated an AWFUL lot of code for just a few lines of ruby. I give you inter.rb:

#!/usr/bin/env ruby
file = ARGV[0];
text = File.open(file,"r").readlines.join;
urlmap = {"w" => "http://en.wikipedia.org/wiki/%s",
  "g" => "http://google.com/search?q=%s",
  "dict" => "http://m-w.com/dictionary/%s",
  "http" => "http:%s",
  "th" => "http://thesaurus.reference.com/search?q=%s"
}
text.gsub!(/\\[\\[(w+?):(.+?)\\]\\]/) { |match|
  path=urlmap[$1]
  if path == nil then
    puts "%s not found in urlmap" % $1
    match
  else
    str = $2.split('|')
    if str.size == 1 then
      str << str[0];
    end
    ('<a href="' + path + '">%s</a>') % [str[0], str[1]]
  end
}
#puts text
File.open(file, "w").puts text;

And now here is textile.pl:

#!/usr/bin/perl
# take textile text as stdin and output as html
# textile.psh < filename.html
# :%!textile.psh  (in vim)
use Text::Textile qw(textile);
my $text = "";
my $entryfile = $ARGV[0];

while (<>) {
 $text .= $_;
}
# procedural usage
my $html = textile($text);
#print $html;
open ( FILE, ">$entryfile" ) or die;
print FILE $html;
close FILE;
# OOP usage
#    my $textile = new Text::Textile;
#    $html = $textile->process($text);
#    print $html;

And finally here is the markdown plugin script:

#!/usr/bin/env ruby
# Markdown'ise things.
# Take Markdown markup on STDIN, and output HTML to STDOUT
require 'rubygems'
require 'bluecloth'
#print (BlueCloth.new STDIN.read).to_html
file = ARGV[0];
text = File.open(file,"r").readlines.join;
html = (BlueCloth.new text).to_html
File.open(file, "w").puts html;

Hope this comes through fine.

 

Advertisements

Written by totalrecall

February 28, 2007 at 9:28 pm

Posted in ecto, Mac OS X, 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: