Friday, August 04, 2006

How To: Blogger Navbar Toggle Script

The Blogger navigation bar is handy but I wanted the ability to turn it off or on if I wanted to, so I wrote up this javascript to toggle the navbar on or off.

<script language="javascript" type="text/javascript">
<!--
function toggle(targetId) {
  if (document.layers) {
    visi = (document.layers[targetId].visibility == 'show') ? 'hide' : 'show'
    document.layers[targetId].visibility = visi;
  }
  else if (document.all)
    visi = (document.all[targetId].style.visibility == 'visible') ? 'hidden' : 'visible';
    document.all[targetId].style.visibility = visi;
  }
  else if (document.getElementById) {
    visi = (document.getElementById(targetId).style.visibility == 'visible') ? 'hidden' : 'visible';
    document.getElementById(targetId).style.visibility = visi;
  }
}
// -->
</script>


To use it I just added an 'onclick' event to a link:

<a href="#" onclick="toggle('b-navbar');" style="font-weight: bold;">Toggle Navbar</a>


It is generic enough that you could link it to any event and pass it the id of any element you want to toggle.

Wednesday, August 02, 2006

WWDC has almost arrived...

Apple's World Wide Developer conference is winding up to start this Monday. What does Steve Jobs have in store for us? Fan sites are buzzing with speculation about new processors, ipods and the upcoming reincarnation of Apple's g5 towers. I feel like Apple's conferences have become my new Christmas and Steve Jobs has become my new Santa. I know it's sick but Apple's veil of secrecy makes for a fun bit of fanfare once it is lifted.

Things on my mind are: Will Apple's Macbook line recieve a Core 2 refresh? Will the iMac recieve Core 2 also? Will we the Xeon Class Core 2 CPU replace the G5 in Apple's Xserves and Power Macs? I think the Xserve and PowerMac refresh to some sort of conroe based CPU is a "well duh" thing, but my heart hopes that Merom (Core 2 mobile) finds it's way into a Macbook Pro. I am due for a new laptop at work and I'd really love to play with one of these badboys.

Anyway, on to OS updates. Leopard... I got angry when I first heard that Apple was preparing Mac OS 10.5. They had just released 10.4 and I thought "how dare they!" However, Apple's zeal for forward looking products with innovative features has finally peaked my interest. Luckily I don't have to buy many of their products with my own money. I would probably feel differently if I was shelling out money for a new iLife and iWork each year...

Leopard, what will it have in store? Will it offer built in virtualization support? Will it offer non-beta bootcamp? I've heard speculation of Rosetta incorporating windows APIs. How cool would that be to toss in a windows based product and have Windows API support through rosetta?

I have been tinkering with Universal/PPC apps on a new Mac Mini as well as a new iMac. The mini is a core solo with 1gig of ram. The iMac is a core duo with 1 gig of ram. It is barely noticeable when I load a PPC application on either of these. The average user would have no complaints. If rosetta can handle PPC emulation with no sweat, then I'm sure it woudldn't be much loss in performance to add some windows support. Performance should actually as good as windows since it wouldn't have to emulate the x86 cpu.

On to the iPod? Will we see the lusted after full screen iPod? I just recently got a broken third generation 20gig iPod. I smack it and it works for few hours then locks up. If I let the battery die then it works again. This iPod was used and has been through a lot so I don't expect much out of it. I have finally realized what an addicting product the iPod is, despite having a semi-functional one. I don't agree with Apple's monopolistic approach to iTunes and iPod, but those products are great. I love my broken iPod and my iTunes. Speculation on the iPod seems to be that we'll only see larger capacity iPod Nanos.

Displays? Apple's integrated iSight cameras have spread from iMac to Macbook Pros then to Macbook's. Will Apple update their cinema displays to incorporate a camera? The voice in my head says "yes!" Some times the voice lies, hopefully we'll know on Monday.

Front Row? How will the Mac Pro incorporate that? I don't think many people will be putting a Mac Pro into their entertainment center due to it's product placement as a workstation class system but I still think it needs front row. Will the IR reciever be placed in the tower or in the display? I would like to see it positioned in the display...

Come Monday we'll see how close the rumors come...

Friday, July 28, 2006

How To: Regular Expression Tester

It always takes me a few tries to get my regular expressions right. I thought it would be handy if I could take a regular expression and test text to match it against, edit both in real-time and see the results. I thought such a project would be pretty quick and easy in Ruby on Rails (which I'm currently teaching myself).

First I created a new project:
rails regtester

I don't need to store any data, so I added a single controller:
ruby script/generate controller Regtest index

Code
index.rhtml:
<html>
<head>
  <title>Regular Expression Tester</title>
  <%= javascript_include_tag "prototype" %>
</head>
<body>
<%= start_form_tag({:action => "#"}, :id => "regexp-form") %>
<table>
<tr>
  <td>RegExp: <%= text_field_tag :pattern %></td>
  <td> </td>
</tr>
<tr>
  <td valign="top"><%= text_area_tag :match_text, nil, :size => "40x20" %></td>
  <td id="results-area" valign="top"></td>
</tr>
</table>
<%= end_form_tag %>
<%= observe_form "regexp-form", :frequency => 1, :update => "results-area", :url => {:action => "find_matches" } %>
</body>
</html>


regtest_controller.rb:
class RegtestController < ApplicationController
  def index
  end

  def find_matches
    begin
      pattern = Regexp.new(params[:pattern])
      @results = params[:match_text].gsub(pattern, '<b>\&</b>')
    rescue RegexpError
      @results = "Invalid Regular Expression"
    end
    render :text => @results
  end
end


Improvements
One limitation right now it that the match highlighting is pretty mediocre. The highlighting may not be apparent and it doesn't highlight whitespace. Instead of bold tags you could try something like:

<span style="gt;&laquo;</span>\&<span style="color: red">&raquo;</span>