array('html'))); $twig->addFilter('textile', $function); // -- Work -------------------------------------- // Fetch data from pom.xml $params = parsePOM(); // Create target dir if needed if (!file_exists(TARGET_DIR)) { echo "Creating target directory\n"; mkdir(TARGET_DIR, 0777, true); } // Render pages $files = scandir(PAGES_DIR); foreach($files as $file) { if (preg_match('/.twig$/', $file)) { $filename = str_replace('.twig', '.html', $file); $target = TARGET_DIR . $filename; echo "Rendering template: $filename\n"; $template = $twig->loadTemplate($file); $page = $template->render($params); file_put_contents($target, $page); } } // Copy resources copyDir(RESOURCES_DIR, TARGET_DIR); echo "Done.\n"; // -- Helpers ----------------------------------- /** Extracts information from pom.xml required for rendering the site. */ function parsePOM() { $project = simplexml_load_file(POM_PATH); return array( 'project' => array( 'name' => (string) $project->name, 'url' => (string) $project->url, 'inceptionYear' => (string) $project->inceptionYear, ) ); } /** Recursively copies directory contents. */ function copyDir($source, $target) { $source = trim($source, '/'); $target = trim($target, '/'); $files = scandir($source); foreach($files as $file) { if ($file == '.' || $file == '..') continue; // Skip hidden files such as .svn if ($file[0] == '.') continue; if (is_dir("$source/$file")) { if (!is_dir("$target/$file")) { mkdir("$target/$file"); } copyDir("$source/$file", "$target/$file"); } else { echo "Copying resource: $target/$file\n"; copy("$source/$file", "$target/$file"); } } } /** Renders given textile to XHTML. */ function renderTextile($data) { $textile = new Textile(); return $textile->TextileThis(trim($data)); }