bebraw
5/24/2011 - 11:45 AM

Precompiler thingy for Dwoo (converts templates to static HTML)

Precompiler thingy for Dwoo (converts templates to static HTML)

<?php
include('src/dwoo/dwooAutoload.php');

/*
 * This script converts a bunch of Dwoo templates to proper HTML.
 * Execute it like this "php generate_site.php".
 * 
 * Use this file to inject variables to your templates. In order to make it
 * easier to deal with paths, I defined "pathPrefix" variable. It contains
 * prefix relative to the root. This in turn may be used to get some nice
 * links done esp. if you use a hierarchical template layout.
 * 
 * It is probably possible to use the same approach for rendering templates
 * of other engines too. There's some hacky code but in my tests it worked
 * out just fine.
 * 
 * Read through the code for more detail.
 */

$dwoo = new Dwoo(); 

$data = array(
    'shortAppName' => 'appname',
    'bdd' => '<abbr title="Behavior Driven Development">BDD</abbr>',
    'gpl' => '<abbr title="GNU General Public License">GPL</abbr>',
    'mit' => '<abbr title="Massachusetts Institute of Technology">MIT</abbr>'
);

// do your hacks here if you need to
//date_default_timezone_set('Europe/Helsinki');

// set up some dirs (input/output)
$cssDirectory = './css/';
$imageDirectory = './images/';
$templateDirectory = './templates/';
$outputDirectory = './site/';
$outputSuffix = 'html';

// oh yeah, let's push some bits around
$dir = new RecursiveDirectoryIterator($templateDirectory);
$iter = new RecursiveIteratorIterator($dir);
$regex = new RegexIterator(
    $iter,
    '/^.+\.tpl$/i',
    RecursiveRegexIterator::GET_MATCH
);

if(!is_dir($outputDirectory)) {
    mkdir($outputDirectory);
}

foreach($regex as $dir) {
    $file = $dir[0];
    
    // XXX: a bit hacky...
    $outPath = substr_replace($file, $outputDirectory, 0, strlen($templateDirectory));
    $outPath = substr_replace($outPath, $outputSuffix, -strlen($outputSuffix) + 1);
    $outDir = dirname($outPath);

    $amountOfDirs = count(split('/', $outDir)) - 1;
    
    if($amountOfDirs > 2) {
        $prefix = str_repeat('../', $amountOfDirs - 2) . '..';
    }
    else {
        $prefix = str_repeat('.', $amountOfDirs);
    }
    
    $data['pathPrefix'] = $prefix;
    $out = $dwoo->get($file, $data);
    
    echo "Writing $outPath\n";

    if(!is_dir($outDir)) {
        // we may need to create nested dirs here!
        // idea: http://webobserve.blogspot.com/2011/03/how-to-create-nested-directory-using.html
        $dirs = explode('/',$outDir);
        
        $tmpPath = '';
        foreach($dirs as $key=>$name) {
            $tmpPath .= $name;
            
            if(!file_exists($tmpPath)) {
                mkdir($tmpPath);    
            }
            
            $tmpPath .= DIRECTORY_SEPARATOR;
        }
    }
    
    // file_put_contents
    $fh = fopen($outPath, 'w') or die("can't open file");
    fwrite($fh, $out);
    fclose($fh);
}

function copyToDir($pattern, $dir) {
    foreach (glob($pattern) as $file) {
        if(!is_dir($file) && is_readable($file)) {
            if(!is_dir($dir)) { 
                mkdir($dir);
            }
            
            $dest = $dir . DIRECTORY_SEPARATOR . basename($file);
            
            copy($file, $dest);
        }
    }    
}

// copy some CSS and images to right places
copyToDir($cssDirectory . '*.css', $outputDirectory . 'css');
copyToDir($imageDirectory . '*.jpg', $outputDirectory . 'images');
copyToDir($imageDirectory . '*.png', $outputDirectory . 'images');

?>