The Accidental Rubyist

invalid byte sequence in UTF-8

Archive for February 2007

some more testing of a script that will do the code formatting

leave a comment »

I have hacked a script which converts the selected text in Ecto to the following formatted text.

while (<>) {
 $text .= $_;
}
# procedural usage
my $html = textile($text);
#print $html; <---- this would get printed in a dialog box
open ( FILE, ">$entryfile" ) or die;
print FILE $html;
close FILE;

[Update: If you look carefully, syntax highliting done by the hack above is not as comprehensive as the one done to textile.pl in this post. This is because in those cases, since the entire file went through the filter, the filter knew the programming language and could thus do a language specific highlighting. In the case of highlighting code within ecto and running the script, the filter invoked does not know the language. That can be further taken care of by examining the first line of the sample, and determining language from a <code>”#!/bin/env ruby”</code> like statement.]

However, before i fall asleep let me apologize. I notice that one entry of mine posted in the morning (inter.rb) seems to have the code intact. In that case, I pasted the code into the rich text editor. It did the required conversions. This appears to work fine.

It is only when i pasted the code into the HTML editor, in order to put the PRE and CODE tags, that i began to lose characters. So a lot of testing and time has gone waste. But i do have a neat hack ready now, and I have learned a couple of things about a couple of things.
I will share this hack tomorrow morning.

Back to sleep after a bit of reading The Northern Light. Oh, did I mention that I have mapped ^B and ^I to do bold-facing and italicizing.

COOL !!!

Advertisements

Written by totalrecall

February 28, 2007 at 11:13 pm

Posted in ecto, Mac OS X, ruby

Automatic hyperlinking to wikipedia and others using ecto scripting

leave a comment »

Ecto is a feature-rich desktop blogging client for Mac OS X and Windows, supporting a wide range of weblog systems, such as Drupal. It allows me to plug-in my own scripts writing in programming languages such as ruby, perl, Bash and i suppose python also.

This feature allows for some nifty stuff such as filtering the blog contents.

Lets link to a google search for our favorite blogging client now

The above text was hyperlinked using the script “inter” (source) based on how the interwiki module of drupal works. The input text was as follows:

[[w:ecto|Ecto]] is a feature-rich desktop blogging client for [[w:Mac OS X]] and Windows, supporting a wide range of weblog systems, such as [[http:drupal.org|Drupal]]. It allows me to plug-in my own scripts writing in [[w:programming languages]] such as [[w:ruby]], [[w:perl]], [[w:bash|Bash]] and i suppose [[w:python]] also.

This feature allows for some [[dict:nifty]] stuff such as filtering the blog contents.

[[w:ecto|Ecto]] inserts a hyperlink to wikipedia with ecto as the parameter, and Ecto as the display text of the hyperlink. On the other hand, [[w:ecto]] would use “ecto” both as parameter and display text.

Other current options are:

g: for google search

dict: for a m-w.com dictionary search

th: thesaurus.reference.com

http: a link

One can add more to the ruby script which does this replacement.

I shall try to post the ruby script in the next post. But cannot guarantee since blogsome is messing up code.

Written by totalrecall

February 28, 2007 at 10:34 pm

Posted in ecto, Mac OS X, ruby

where can one comment on ecto

leave a comment »

Now that i have begun to write some scripts for ecto, i would like to start sharing them on the ecto site. But i cant find any place on the ecto site to leave a comment. Could someone help me by giving me a link.

Written by totalrecall

February 28, 2007 at 10:08 pm

Posted in ecto, ruby

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.
Read the rest of this entry »

Written by totalrecall

February 28, 2007 at 9:28 pm

Posted in ecto, Mac OS X, ruby

ecto and Markdown

leave a comment »

#!/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;

I must first admit that since ecto supports a WYSIWYG editor therefore a script that does textile or markdown filtering does not make much sense. Also, there is the possibility of the confusion being created. Thus the textile and Markdown examples are more of examples of what can be done. A more practical example is the inter.rb given somewhere in the previous post.

Anyway, that said, the above example requires ruby, rubygems and the BlueCloth gem to be installed. If you are running Unix or Mac OS X (which you should be), you would have ruby. Install rubygems, and then install BlueCloth as:

sudo gem install BlueCloth

(sudo is required for OS X.)

Written by totalrecall

February 28, 2007 at 7:05 pm

Posted in ecto, Mac OS X, ruby

ecto script: inter.rb

leave a comment »

To learn how to do hyperlinking with inter.rb see this.

#!/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 %s') % [str[0], str[1]]
end
}
#puts text
File.open(file, "w").puts text;

Source of inter.rb (Escapes, i.e., &#92; or \, are disappearing despite all attempts to print them)

Written by totalrecall

February 28, 2007 at 7:00 pm

Posted in ecto, ruby

testing ecto scripts – ruby, perl and bash

leave a comment »

Today, i decided to be a little geeky. decided to play a little with ecto’s scripting abilities . Or rather its ability to plug in existing scripts. I do have scripts that textile-ize a file or STDIN and return the output to STDOUT. The first thing is that sadly one cannot run bash or perl or ruby scripts as-is. Surely, the folks at ecto had some strong reasons for that.
So some modifications are required. Essentially, when you invoke a script, the selected text, or the complete file (if not selection) is put into a temporary file. The name of the temporary file is sent as an argument to your script. Thus, you need to read that file for its contents, and write back to that file. Ecto does the rest.
Currently, most unix and perl/ruby scripts would be reading from STDIN and writing to STDOUT. However, ecto prints STDOUT in a dialog box. Great for messages or errors. If your perl script reads in the STDIN fine in “<>” then thats okay, but you will have to open the file and write back. Here is the sample:

    #!/usr/bin/perl
    # take textile text as stdin and output as html
    use Text::Textile qw(textile);
    my $text = "";
    my $entryfile = $ARGV[0];
    while (<>) {    $text .= $_;    }
    my $html = textile($text);
    #print $html;  <---- this line would print in a dialog box so commented off and next few lines added
    open ( FILE, ">$entryfile" ) or die;
    print FILE $html;
    close FILE;
   

In the ruby example, STDIN did not catch any output, so i had to open the file and read from it.

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

Finally, in the spirit of playing around, here is a minimal bash file to append output to your post.
#!/bin/bash
ls -ltrh >> $1
The above perl and bash sample, were created after looking at the provided samples. I hope they make it easier for the first timer. I haven’t done anything with Apple Script, sorry i am too “last-century” (as Steve Jobs would say) to learn apple-scripting 🙂

Written by totalrecall

February 28, 2007 at 6:54 pm

Posted in ecto, ruby