SimpleBlog

Index of all articles

Bloging with Ruby and RedCloth

Writing down some texts in textile format, which I called ”*.tt”. Used Ruby and RedCloth to render them to html.

Simplest way to deploy HTML. Make a dir with your tt-files (could be a dir tree). Run this programm. You need a header.txt and footer.txt (for all this css and layout things).

And then ftp the whole dir to your webspace. (this could be done in the script, but i dont show it to you ;-)

Modificated at 2008-06-18: (included one and a half line of code to check mtime before rerendering, so now only changed files will be renderd)


#!/bin/ruby

require 'redcloth'
require 'find'
require 'jcode'
$KCODE = 'UTF8'

@sh=open('header.txt').read
@sf=open('footer.txt').read

def render(tt)
    x=RedCloth.new(tt)
    @sh + x.to_html + @sf
end

#check allindex mtime (allindex.html is generated at th end of a run)
lastrun=File.mtime("allindex.html")

paths=[]
Find.find('./') do |path|
    if File.extname(path) == '.tt'
        ofile=path.gsub('.tt', '.html')
        paths << [ofile.split('/').length, ofile]
        if File.mtime(path) > lastrun #check for mtime
          puts "Rendering #{path}" 
          tt=open(path).read
          of=open(ofile,"w")
          of << render(tt)
          of.close
        end
    end
end

allindex="h2. All articles:\n\n" 
paths.sort.each do |a|
    allindex << '* "' << a[1] <<'":' << a[1].gsub('./','') << "\n" 
end
puts "Doing allindex ..." 
of=open("allindex.html","w")
of << render(allindex)
of.close()

Here an example for the header.txt and footer.txt without style(sheets):

header.txt:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
 </head>
 <body>

footer.txt:


 </body>
</html>

If you want to see the tt. source files of this pages (Simpleblog), simply change file extension html to tt in the url in the address field of your browser and download the source to view it with an text editor.