The Accidental Rubyist

invalid byte sequence in UTF-8

Archive for the ‘ecto’ Category

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;
Advertisements

Written by totalrecall

March 2, 2007 at 11:32 am

Posted in ecto, ruby

Ecto script: ABBR

leave a comment »

Now I know, Mr Shinckel has already posted a python script to do some word replacements. And I don’t claim this is much of a help because ecto already provides spell-checking. But just in case you like using your own short forms for things, you could use this. It uses a dictionary file that is outside the script, and replaces ALL instances. So its more of a spell-checker or “abbr” like facility than the first occurrence replacer that Mr Shinckel has shared.

#!/usr/bin/env ruby
require "yaml"
file = ARGV[0];
text = File.open(file,"r").readlines.join;
mymap = YAML::load( File.open("dict.yaml")); 
mymap.each_pair{ |key, value|
  text.gsub!(key, value);
}
File.open(file, "w").puts text;

The dictionary file is as follows:

cant: can't 
dont: don't 
wrogn: wrong
the: the
prog;: programming
lang;: language

The abbr script is available here, and here is the dict. You will have to add entries in the dict. You can even add URLs and they will be globally replaced.

Written by totalrecall

March 1, 2007 at 5:00 pm

Posted in ecto, ruby

Ecto script: URLIZE

leave a comment »

Apart from the confusingly similar name, the only other difference in URLIZE and URLIFY is this that URLIZE does not require any tags in your post. It searches and replaces all keys with the corresponding value in the URL hash. It does this for only the first match, so that each occurrence of the word is not URLIZED.

One issue with this is that if you run the script twice on the same input, it will attempt to URLIZE again, and this mangles the output.

URLIZE is almost like the ABBR script (next post) except that ABBR replaces all occurrences.

By now you could be eager to see the URLIZE script:

#!/usr/bin/env ruby
require 'yaml'
file = ARGV[0];
text = File.open(file,"r").readlines.join;
mymap = YAML::load( File.open("urls.yaml")); 
mymap.each_pair{ |key, value|
  text.sub!(/(\s)#{key}/, '\1%s' % value);
}
File.open(file, "w").puts text;

Note that the script checks for a white-space before the key, so that substrings are not replaced. You may want to check for a space or comma or period after too. This wonderful program uses the urlify.yaml which goes as follows (warning, this is being processed by the blog system, see actual file):


ruby: <a href="http://ruby-lang.org">ruby</a>
perl: <a href="http://perl.org">perl</a>
python: <a href="http://python.org">python</a>
ecto: <a href="http://ecto.kung-foo.tv/">ecto</a>

Written by totalrecall

March 1, 2007 at 4:11 pm

Posted in ecto, ruby

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.

Written by totalrecall

March 1, 2007 at 3:37 pm

Posted in ecto, ruby

Pretty Print Code script (hack)

The hack presented below has been developed on a MAC OS X. It uses the TOhtml function of the Vim Editor (i use Version 7.0). It runs in HTML Mode only.

f=$1vim -f +"syn on" +"run! syntax/2html.vim" +"wq" +"q" $f

sed  -n '/<pre>/,/<\\/pre>/p' $f.html | sed 's~<pre>~<pre style="margin: 1.5em 0px; padding: 10px 10px 10px 10px; color: #333; background: #000000;color:#ffffff; font-family: monaco,monospace; font-size: 120%; border-left: solid #ccc 1px; overflow: auto;">~p' > $f

rm $f.html

Line 1. saves temp file name to a variable f.

Line 2. Invokes a vim plugin on the temporary file. Vim creates a file with an html extension.

Line 3. Extracts lines from output file between PRE tags. Then replace opening PRE tag with out own styling. This is required for me, since this blog does not allow me to modify the CSS.

Line 4. Delete the temp file created by Vim.

Note: If most of the code you post is in one language then the input file given to Vim should have that extension. That way the Vim plugin will do a more comprehensive syntax highlighting.

The other option is to detect the programming language from the first line of the temp file (assuming it has the interpreter name in it) and rename the file accordingly.

Source (please note that this program does not treat the escape character \, and WP does not print the \)

Written by totalrecall

March 1, 2007 at 1:00 pm

Posted in ecto, Mac OS X, ruby, vim

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