Created
February 24, 2011 03:40
-
-
Save kolber/841711 to your computer and use it in GitHub Desktop.
Stacey, A quick and dirty HTML export script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Export</title> | |
<style> div { display: none; }</style> | |
</head> | |
<body> | |
<?php | |
# Usage: | |
# - Place this file in the top level of your stacey site (alongside 'app', 'content', etc.) | |
# - Create a folder named 'export' in the top level of your stacey site | |
# - Change the permissions on the 'export' folder to '777' | |
# - Hit 'http://yoursite.com/export.php' | |
# require helpers class so we can use rglob | |
require_once './app/helpers.inc.php'; | |
# require the yaml parser | |
require_once './app/parsers/yaml/sfYaml.php'; | |
# include any php files which sit in the app folder | |
foreach(Helpers::rglob('./app/**.inc.php') as $include) include_once $include; | |
# include any custom extensions | |
foreach(Helpers::rglob('./extensions/**.inc.php') as $include) include_once $include; | |
Class Export { | |
function __construct($dir) { | |
if(function_exists('date_default_timezone_set')) date_default_timezone_set('Australia/Melbourne'); | |
# delete everything in the export folder | |
self::delete('./export'); | |
$this->export($dir); | |
# copy across public & content folders | |
self::delete('./export/content/'); | |
self::copyr('./content', './export/content'); | |
self::delete('./export/public'); | |
self::copyr('./public', './export/public'); | |
} | |
function delete($dir) { | |
if (is_dir($dir)) { | |
$objects = scandir($dir); | |
foreach ($objects as $object) { | |
if ($object != "." && $object != "..") { | |
if (filetype($dir."/".$object) == "dir") self::delete($dir."/".$object); else unlink($dir."/".$object); | |
} | |
} | |
reset($objects); | |
} | |
} | |
function copyr($source, $dest) { | |
// Simple copy for a file | |
if (is_file($source)) return copy($source, $dest); | |
// Make destination directory | |
if (!is_dir($dest)) mkdir($dest); | |
// Loop through the folder | |
$dir = dir($source); | |
while (false !== $entry = $dir->read()) { | |
// Skip pointers | |
if ($entry == '.' || $entry == '..') continue; | |
// Deep copy directories | |
self::copyr("$source/$entry", "$dest/$entry"); | |
} | |
// Clean up | |
$dir->close(); | |
return true; | |
} | |
function export($dir) { | |
foreach(glob($dir.'/*') as $path) { | |
$file = basename($path); | |
if(substr($file, 0, 1) == ".") continue; | |
if(is_dir($path)) { | |
self::export($path); | |
$this->create_globals($path); | |
global $current_page_template_file; | |
if(preg_match('/\.html$/', $current_page_template_file)) { | |
$this->create_page($path); | |
} | |
} | |
} | |
} | |
function create_globals($path) { | |
global $current_page_file_path; | |
$current_page_file_path = 'export/'.$path; | |
global $current_page_template_file; | |
$current_page_template_file = Page::template_file(Page::template_name($path)); | |
} | |
function create_page($path) { | |
$page = new Page(Helpers::file_path_to_url($path)); | |
if($page->url_path == 'index') $page->url_path = ''; | |
if ($page->url_path && !file_exists('./export/'.$page->url_path)) { | |
mkdir('./export/'.$page->url_path, 0777, true); | |
} | |
echo '"./export/'.$page->url_path.'" created.<br>'; | |
$path = preg_replace('/[^\/]+/', '..', $page->url_path).'/'; | |
if ($path == '/') $path = './'; | |
echo '<div>'; | |
ob_start(null); | |
echo $page->parse_template(); | |
$fp = fopen('./export/'.$page->url_path.'/index.html', 'w'); | |
# $content = ob_get_contents(); | |
$content = preg_replace('/(href|src)=\"(?:\.+\/+)+(\?\/?)?/', '\\1="'.$path, ob_get_contents()); | |
$content = preg_replace('/href=\"(.+)\/\"/', 'href="\\1/index.html"', $content); | |
fwrite($fp, $content); | |
fclose($fp); | |
ob_end_flush(); | |
echo '</div>'; | |
if($page->url_path) chmod('./export/'.$page->url_path, 0777); | |
chmod('./export/'.$page->url_path.'/index.html', 0777); | |
} | |
} | |
if (is_writable('./export')) { | |
new Export('./content'); | |
} else { | |
echo 'Error. "./export" could not be written to.<br>'; | |
} | |
?> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment