Run regular expression on whole page contents

I need to execute specific Ruby code on the contents of all the pages (templates).

For example, if I had following helper in my config.rb:

def myexample(text)
	text.gsub("dog","cat")
end

And in my test.html.haml:

= myexample("Some text about a dog.")

My previewed and generated /test.html would read:

Some text about a cat.

However, I am using several different ways to output text that needs to be modified, most notably through HAML’s :markdown filter, so I would prefer not to wrap everything in the = myexample("Text") helper.

I would like to be able to run Ruby code that would take contents of all the pages (preferably) or generated HTML output (if the first option is not possible) as an argument passed to such helper.

Ideally, this code would be run in both the development and build environments, but if that’s not possible, build is enough.

Is it possible to do so?

PS. In my specific case, I use a shorthand notation to reference other pages and then I use a regular expression and eval() in order to replace them with relative links from data files.