faithfulgeek.org

Agile web development

A Mini Publishing Engine in Ruby

December 7th, 2007

Last weekend I wrote a mini publishing engine in Ruby. It’s very small, very basic, but it works, and that’s the important part. Best of all, it’s only 12 lines of code (including whitespace)! Check out the code, below:

Template:

<html>
    <head>
        <title>Test Page</title>
    </head>
    <body>
        ${body}    
    </body>
</html>

Publisher:

require 'ftools'

template = File.red('template.html').to_s

page_body = ARGV[0]

content = template.gsub(/\$\{body\}/, page_body)

File.open('index.html', File::CREAT|File::RDWR) {
    |f|
    f << content
}

So running

ruby RbPublisher.rb This is the body!
yeilds:

Output:

<html>
    <head>
        <title>Test Page</title>
    </head>
    <body>
        This is the body!    
    </body>
</html>

Pretty self explanatory. Takes in the body content on the command line, and pushes it into the template where I put the ${body} tag. This was a fun experiment, and we may use it on the CWSA website.

Questions/comments? Please leave any constructive criticism you may have! Enjoy!

Leave a Reply