These days, I’m pretty enamored of Haml, it’s more like HTML (unlike many other formatting engines) and it’s fast to write (unlike many other formatting engines).
1. Install Haml
Haml installs as a gem…1 gem install --no-ri haml
and a plugin… haml --rails path/to/your/wiki
2. Create Your Haml View
Open up app/views/show.html.erb
and convert it to Haml. Replacing it to:
.content
%h1= @page.title.gsub('_', ' ')
= link_to('Edit', page_edit_path(:page_title => @page.title))
= link_to('History', page_history_path(:page_title => @page.title))
= "Last Updated #{time_ago_in_words(@page.revisions.last.created_on)} ago by #{Person.find(@page.revisions.last.person_id).name}"
%p= auto_link(@page.revisions.last.parsed_contents)
Now, your wiki should look exactly like it did before we plugged in Haml.
3. Add Haml Support for Revisions
Open up app/models/revision.rb and update def parsed_contents with:
def parsed_contents
contents = contents.gsub(/(w*_w*)/) {|match| "<a href='#{match.downcase}'>#{match.gsub('_', ' ')}</a>"}
h = Haml::Engine.new(contents)
h.to_html
end
4. Try It Out
Load up a page in your wiki, click ‘edit’, and add in some simple Haml, something like:
%strong this should be bold
5. Add in Some More (non-Haml) Formatting
Once you’re comfortable with Haml, maybe add in some other simple formatting rules into revision.rb
#Auto-renders any image URL
contents = self.contents.gsub(/s(.*.(jpg|gif|png))s/, '<img src="1"/>')
# Bolds text inside asterisks
contents = contents.gsub(/*(.*)*/, '<strong>1</strong>')
# Wraps <code> around text inside '@'
contents = contents.gsub(/@(.*)@/, '<code>1</code>')
# Blockquotes text inside double-quotes
contents = contents.gsub(/s"(.*)"s/, '<blockquote>"1"</blockquote>')
# Italicizes text inside underscores
contents = contents.gsub(/s_(.*)_s/, '<em>1</em>')
6. Make it Better
After playing around with Haml and other formatting, you’ll quickly see some quirks – some room for improvement. Go for it, and let me know when you get something interesting.
Here’s how to build a very simple Ruby on Rails-based wiki engine 1.
1. Create the Rails App
rails wiki
and cd wiki to get inside the app.
Remember: create your database (i.e. wiki_development) and update config/database.ymlappropriately
2. Generate the Scaffolding
There are 3 nouns in this wiki system: People, Pages, Revisions. Let’s create the models for each of them.
run script/generate scaffold Person
Now open up wiki/db/migrate/*_create_people.rb and add: t.column "name", :string inside the create_table block to store the Person’s name.
Add Person.create :name => "First Person"
after the create_table block to give us an initial Person.
run script/generate scaffold Page
Now open up wiki/db/migrate/*_create_pages.rb and add: t.column "title", :string inside the create_table to store the Page’s title and add Page.create :title => "Home_Page" after the create_table block to give us an initial Page.
run script/generate scaffold Revision
Now open up wiki/db/migrate/*_create_revisions.rb and add:
# Reference to the Person that created the Revision
t.column "person_id", :integer
# The contents of the Revision
t.column "contents", :text
# Reference to the Page this Revision belongs to
t.column "page_id", :integer
3. Run the database migrations
rake db:migrate
4. Describe the relationships between the Models
People should be able to create many Revisions.
Open /app/models/person.rb and add – after the first line: has_many :revisions
Pages should have many Revisions
Open /app/models/page.rb and add – after the first line: has_many :revisions
Revisions should reflect they belong to both Pages and People
Open /app/models/revision.rb and add – after the first line:
belongs_to :page
belongs_to :person
5. Draw up the Routes
Open up config/routes.rb and update them to handle wiki-fied words. Copy the following after the map.resources block
map.root :controller => 'pages', :action => 'show', :id => 1
Now run script/server and load up a browser.
If things are working correctly, you should see simply ‘Edit | Back’ as links
6. Create the Views
Open /app/views/revisions/new.html.erb and add
< %= f.text_area :contents %>
< % fields_for :person do |p| %>
< %= p.text_field :name %>
< % end %>
right after < %= f.error_messages %> for the Revision’s content, the corresponding Page and the author’s name
Loading up http://0.0.0.0:3000/revisions/new should give you the form with the text area, a text field, and a submit button you just created.
Try entering something into the form and click ‘Create’. You should see ‘Revision was successfully created.’ Now, if you pop into your database, you should see the new row in the Revisions table with the contents you entered, but NULL values for person_id and page_id.
Let’s remedy that.
Open /app/controllers/revisions_controller.rb and add this line right after the @revision declaration in both def new and def create
@revision.update_attribute('person_id', Person.find_or_create_by_name(params[:person][:name]).id)
Reload http://0.0.0.0:3000/revisions/new and re-fill out the form. After hitting ‘Create’ you should have another Revision record in your database with some digit in the person_id field
Now, let’s tie the Revision form we created to a Page.
Open up /app/views/revisions/new.html.erb and change the form_for to: < % form_for @revision, :url => new_revision_path, :html => {:method => :post} do |f| %>
Next, right after < %= f.text_area :contents %> add: < %= f.hidden_field :page_id, :value => @page.id %>
Now, rename /app/views/revisions/new.html.erb to /app/views/revisions/_new.html.erb. This turns the form into a partial, which is fine, because Revisions only exist within Pages, never on their own.
Next, replace everything in /app/views/pages/edit.html.erb with
<h1>Update "< %= @page.title.gsub('_', ' ') %>"</h1>
< %= render :partial => 'revisions/new' %>
If you load up 0.0.0.0:3000/pages/1/edit right now, you’ll get a ‘Called id for nil’ error. That’s because the Revision partial is referencing @revision, which doesn’t exist in /pages. Yet.
Open up /app/controllers/pages_controller.rb and scroll to ‘def edit’.
Add @revision = @page.revisions.last || Revision.new right after the @page declaration
Refreshing 0.0.0.0:3000/pages/1/edit should now correctly render the form.
Fill it out, hit ‘Create’ and check the database. You should have a new record with a number in the page_id column.
But we can’t see it because 0.0.0.0:3000/pages/1 doesn’t show anything.
Open up /app/views/pages/show.html.erb and add:
<h2>< %= @page.title.gsub('_', ' ') %></h2>
<p>< %= @page.revisions.last.contents %></p>
<p>last edited by < %= @page.revisions.last.person.name %> on < %= time_ago_in_words(@page.revisions.last.created_at) %> ago</p>
to the first line and save.
Refreshing 0.0.0.0:3000/pages/1 should show the text you entered.
7. Update the Model, Controller, Views, and Routes to Acknowledge Wiki-fied Words (separated with an underscore ‘_’)
Open up /app/models/revision.rb and add:
def parsed_contents
contents = self.contents.gsub(/(w*_w*)/) {|match| "<a href='#{match.downcase}'>#{match.gsub('_', ' ')}</a>"}
end
Open up /app/controllers/pages_controllers.rb, find def show and change: @page = Page.find(params[:id])
to
@page = Page.find_or_create_by_title(params[:title])
return redirect_to page_edit_url(:title => @page.title) if @page.revisions.empty?
Open up /app/controllers/revisions_controllers.rb, find def create and change each : format.html... line to format.html { redirect_to page_base_url(:page_title => @revision.page.title) }
Next, scroll down to def edit and change: @page = Page.find(params[:id])
to @page = Page.find_by_title(params[:title])
Now, in /app/views/pages/show.html.erb change the Edit link to build the new route: < %= link_to 'Edit', page_edit_url(:title => @page.title) %> |
1. There are leaner Ruby frameworks to build wikis in (Camping or Merb come to mind.). I picked Rails for 2 reasons; My servers and deployment process was already set up for Rails, I wanted to loosely integrate this wiki into another existing Rails-based application (Cullect.com).
First off, a caveat: Basing a vote on potential personal financial changes is as one-sided as basing a vote on gender, skin pigment, or hair color. It’s one factor and one that I hope to argue it is a wash to vast majority of Americans. For both plans call for significant cuts for the vast majority of Americans – households making less than $603k/yr (99% of Americans). For those remaining 1%, taxes will either go up or down. I’m part of the 99%, and I suspect you are as well1.
Income (% of Taxpayers)
Plan Difference in $
Plan Difference as % of Income
Favors
< $603k (10)
7,869
1.3
McCain
< $227k (10)
1,591
0.7
McCain
< $161k (10)
410
0.25
McCain
< $111k (10)
281
0.25
Obama
< $ 66k (20)
723
1.1
Obama
< $ 38k (20)
779
2.0
Obama
< $ 19k (20)
548
2.9
Obama
While much attention has been made to how different these plans are at the poles, it surprises me how close the two plans are for the middle 60% of tax payers (<2% delta).
I’m assuming both plans are drafts and would have to pass Congress to be enacted 2. If so, then I assume getting them passed through Congress would change the plans – perhaps even making them more similar.
Does this betray how similar their policies are/will be for the majority of Americans?
1. If you’re not, can I has monie? kthxbye.
2. Confirming we shouldn’t be investing too much in the candidates plans:
Our Series2 TiVo is on its last legs. With each passing day, its over-the-air recordings (we’re a no cable household) are more and more unwatchable, while digital over-the-air recordings are getting more stable. But seriously, TV without TiVo is like email without a spam filter.
The Mini’s secondary job is to play Netflix disks and does so with far fewer curse words1 than the dedicated DVD player it replaced.
In a world where; there’s a computer already plugged into our TV, NBC is back on iTunes, Hulu.com gaining traction, and the Roku box, dropping $200+ on a HD TiVo for over-the-air programming seems questionable.
What would you do?
UPDATE 12 JUNE 2009
The Tivo HD is now on order. Lifetime service.
1. I won’t get into my complaints about iTunes and Front Row here – they deserve their own post. 😉
Finally, Fish-n-Flush is bringing the toilet aquarium to market!
This proves: if an idea’s good enough, and you wait long enough, someone else will do it for you.
The aquarium toilet is one of my longest held It’d-Be-Cool-If ideas. I still remember the moment 15yrs ago when the idea called me. Something about the fish bringing calm and reflection to some often stressful moments. This use case means the tank isn’t the best candidate for the aquarium. I hold the stool-as-aquarium would be more interesting for all involved, but the tank-as-aquarium is close enough to justify me holding onto the idea this long.
I just picked up a new pair of glasses from George over at Look + See Eyecare in Minneapolis.
Until I found Look + See, I was weary of eyewear places. It was a classic case of the paradox of choice. Lots of potential options and difficulty discerning differences without trying on every pair in the store.
George and I actively ignored the vast majority of the frames in the store. We may have gone through 5 frames – out of the hundreds on the walls. Five. Cause, really, how many times do you want to say ‘No’?
Plus, picking 1 from the 5 George recommended was easy.
While part of my final bill was for frames and lenses, part is also for George’s expert curation and recommendations. I don’t want discount that. Remember, the fire hose is free….
Looks like Microsoft’s Internet Explorer will accept any format a web server is willing to give it.
This doesn’t play nicely with Rails’ 2.0+ respond_to feature. A slick little bit of code that asks the browser what it wants and replies accordingly.
Here’s a conversation between Rails & Firefox
Firefox: “Hey Rails, I want this url”
Rails: “No problem, which format would you like it in?”
Firefox: “HTML, please.”
Rails: “Here you go.”
Here’s the same conversation with Internet Explorer
IE: “Hey Rails, I want this url”
Rails: “No problem, which format would you like it in?”
IE: “Whatcha got?”
Rails: “I’ve got Atom, and…”
IE: (interputting) “OK THANKS!”
Rails: “…um, what? I wasn’t finished, really? ok, here you go.”
I had ordered my code alphabetically, so ‘atom‘ came before ‘html‘, like this:
respond_to do |format|
format.atom
format.html
…
end
Because IE is so, um, accepting, I’ve needed to put ‘html‘ first:
respond_to do |format|
format.html
format.atom
…
end
If memory serves, the internet was originally developed as a national defense mechanism. A way to keep communications – in a distributed manner – flowing after a nuclear attack.
Each node a client and a server, a receiver and producer.
Today, not only are the vast majority of Americans online (receivers), but a good chunk – 10% – are actively engaged in making online a better place (producers).
While our communications and communities are distributed, our energy is still centralized.
Broadcast if you will.
From where you’re sitting, can you see the power plant generating the electricity you’re using to read this?
Probably not.
So, we don’t see energy being generated, it’s far away, feedback takes a billing cycle, and our only way to reduce costs is to reduce demand.
And we’re surprised selling energy efficiency to the American public is an uphill battle?
But what if?
What if, just 10% of us were also putting energy back onto the grid.
1 household per block (another take on the block-by-blog idea) sucking down solar, wind, or geothermal. Covering their energy needs and putting the surplus on the grid.
Reducing demand by increasing the number of suppliers – even if they’re only nano-suppliers, cover a few households.
This may even minimize the outages from minor disasters.
The economics are tough from a private citizen doing this on their own, so I wonder – what if this was part of being a energy customer. The energy company finances and maintains the solar panels on your suburban roof. Like sitting in the exit row on an airplane. But for your neighborhood’s electricity.
The boy and I have been making bread (almost) every morning for the past few weeks. I find it a relaxing way to start the morning as he picks at breakfast. The loaf in the photo above, I made this morning.
The simplicity of bread-making is compelling. 4 ingredients: flour, water, yeast, salt.
Separate they don’t taste like much (still, each time we make dough, the boy tastes a licked-finger full of each ingredient).
You’ve probably got those 4 items lying around your kitchen. I did.
No HFCS, no extra flavors. Still, this very simple (and forgiving) recipe makes the best bread I’ve had in a decade. Easy.
Custard-y interior with a hard, crusty exterior.
Perfect for a creamy cheese or a thick swath of Nutella.
And I’ve yet to hit reach 5 min/day, it usually takes fewer.