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 !!!

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

The Northern Lights

leave a comment »

After a long break from life (read: the illusion called life), I have opened Phillip Pullman’s The Northern Lights. This is the first of the Dark Materials trilogy and promises to be a fun and interesting read, like the second and third parts.

Written by totalrecall

February 28, 2007 at 6:51 pm

Posted in Uncategorized

Vim and Ruby

leave a comment »

Have been doing some work of late using my favorite editor VIM. A life-changing article or series of tips is this page. Using Vim with and a programming language like Ruby, makes a very powerful combination.

So I do part of my work in Vim itself, using its powerful regular expressions and other features. Once I am done in Vim, I run a ruby program over the file and my job is done.

Ruby has its glitches here and there, but still very powerful. And great fun. And very productive, especially for someone coming from the verbose world of Java. So it looks like my days (years) with Java are over! Phew !

Written by totalrecall

February 28, 2007 at 6:51 pm

Posted in ecto, Mac OS X, ruby, vim

The Illusion called Life

leave a comment »

At night we dream. The mind shows us a world. We believe we are seeing with our eyes and hearing with our ears, and that the world that we see is real and logical.
Then we wake up and declare that what we saw was false, and that this now is real. We argue that what we saw in the dream was not real enough, and not logical or consistent. There was no continuity with previous dreams etc. But in the dream itself we never argued that the dream was not consistent with previous dreams or not logical. The world in the dream seemed fine.
The point here is that every night since we were born, the mind has lied to us and made use believe we were seeing, even though our eyes were closed and we were in a dark room. Same for the other senses. And yet we wake up and believe that what we now see is true, i.e., the mind is telling us the truth now. How can we blindly trust something that has lied to us each night since birth.
Further, when we say that our waking state is logical and consistent, how do we know that the mind is not fooling us into thinking that we have “woken up”. And that the “waking state” is logical and consistent. The “waking state” is no more logical or consistent than the “dream state”. Never in the dream did we ever question why we suddenly found ourself in our childhood, or in some distant city with no recollection of how we landed there.
The mind makes each state seem logical and consistent. The mind makes us think we have crossed over from one state to another. In reality, as long as we are experiencing the world thru the mind, there is ONLY ONE STATE. And that state is an illusion. There is the illusion of dreaming and waking, there is the illusion of having senses such as sight and hearing, of a body and of pleasure and suffering.
All these lies exist only as long as we identify with the mind. As long as we attach to each thought that the mind throws at us, and think that that thought represents us. And is us. Once we realize that the mind is only a bundle of thoughts, we gain the ability of seeing ourself as apart from these random thoughts, and freeing ourselves of the control and torment of the mind.
We gain freedom and peace for the first time. We live for the first time. (This “living” has to be experienced to be known. It cannot be logically debated.) The mind on hearing this, will try to dismiss this living as being dull and unexciting, for its own existence is at stake. It will point to the great productivity of the human mind. It will not accept the alluring world it has created as being an illusion. And it will most likely succeed in keeping us in a “life” of anger, selfishness, a life-long feeling of incompleteness, loneliness with a few fleeting moments of pleasure and satisfaction. In a life of causing suffering to others, and being abused. It will most likely succeed in keeping us in suffering. Life after life.

But this post would have had its effect if the reader can remember that the solution to this problem exists, and has always existed, and is available when he/she truly accepts the problem and wants a solution.

Remember that you are NOT the body and you are NOT the mind. You merely experience these due to habit.

Written by totalrecall

February 27, 2007 at 6:52 pm

Posted in Uncategorized