We love open source and we invest in continuous learning. We give back our knowledge to the community.

José Costa

Dumb Script to Reassemble HTML + SSI Pages

Comments

While working on our website(WyeWorks website), we wanted to serve some dynamic content but it just didn’t justify having some “big” dynamic technology behind to achieve this. Also as programmers, we like our code (even our static website content) to be nicely separated so we can keep things in order, better maintenance, blah blah blah. Seriously, we really like those things :)

So that’s when we came across the Apache Server Side Includes (Apache SSI) (a much more interesting topic than the addressed here). So, then we divided our static pages to separate the header, menu, content, footer, and we placed a bunch of page includes.

One drawback: checking the content and styles as a whole is now a little bit difficult if you don’t have your Apache SSI capable on handy to reassemble all back together. So this tiny dumb script library (plus some bunch of code not shown here) did that for us.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
require 'fileutils'

module SSI

  def self.generate(source, dest)
    file = File.new(source)
    lines = file.readlines
    file.close
    lines.each do |line|
      if line =~ /<!--\s*#include\s*virtual\s*=\s*('|")(.+)('|")\s*-->/
        path_to_included_file = $2
        unless path_to_included_file[0..0] == "/"
          path_to_included_file = File.join(File.dirname(source),path_to_included_file)
        end
        partial = File.new(path_to_included_file)
        lines_to_include = partial.readlines
        partial.close
        line.gsub!(/<!--\s*#include\s*virtual\s*=\s*('|")(.+)('|")\s*-->/, lines_to_include.join(""))
      end
    end
    if File.directory?(dest)
      dest = File.join(dest, File.basename(source))
    end
    file = File.new(dest, "w")
    lines.each do |line|
      file.write(line)
    end
    file.close
  end

end

It behaves pretty much like any standard copy command, adding the replacement of the included files for their real content. Note there is no error checking whatsoever.

1
SSI.generate(source_file, dest_file_or_directory)

Hopefully it will save a minute or two to someone somewhere.

Comments