Created
November 16, 2011 20:35
-
-
Save alxarch/1371294 to your computer and use it in GitHub Desktop.
Cascading content proof of concept
This file contains 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
<?php | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
define('BASE', str_replace('index.php', '', $_SERVER['SCRIPT_NAME'])); | |
define('DS', DIRECTORY_SEPARATOR); | |
define('WEBDIR', dirname(__FILE__)); | |
function cache($path, $output=null){ | |
$cache = WEBDIR . DS . 'cache' . DS . str_replace('/', DS, $path) . '.html'; | |
if(null === $output){ | |
if(file_exists($cache)){ | |
readfile($cache); | |
exit(0); | |
} | |
} | |
else{ | |
if(!file_exists(dirname($cache))) mkdir(dirname($cache), 0777, true); | |
file_put_contents($cache, $output); | |
} | |
} | |
$path = trim( isset($_GET['p']) ? $_GET['p'] : 'index' , ' /'); | |
cache($path); | |
require_once 'lib/markdown.php'; | |
require_once 'lib/spyc.php'; | |
$filepath = WEBDIR . DS . 'content' . DS . str_replace('/', DS, $path); | |
define('DIR', is_dir($filepath)); | |
$script = $style = $content = $layout = null; | |
// content | |
function fixlinks($content) { return str_replace('](/', ']('.BASE, $content); } | |
$f = DIR ? $filepath . DS . 'content' : $filepath; | |
if( file_exists("$f.markdown") ) | |
$content = Markdown(fixlinks(file_get_contents("$f.markdown"))); | |
else | |
$content = @file_get_contents("$f.html"); | |
// meta | |
function meta($meta){ | |
$result = array(); | |
unset($meta['title']); | |
foreach($meta as $name => $content) | |
$result[] = sprintf('<meta name="%s" content="%s"/>', $name, $content); | |
return implode("\n",$result); | |
} | |
$f = WEBDIR . DS . 'meta.yml'; | |
if( file_exists($f) ) | |
$meta = spyc_load_file($f); | |
else | |
$meta = array(); | |
$f = DIR ? $filepath . DS . 'meta.yml' : "$filepath.yml"; | |
if( file_exists($f) ) | |
$meta += spyc_load_file($f); | |
// script | |
function script($src) { | |
return $src ? sprintf('<script src="%s"></script>', $src) : ''; | |
} | |
$f = DIR ? $filepath . DS . 'script.js' : "$filepath.js"; | |
if( file_exists($f) ) | |
$script = DIR ? BASE."content/$path/script.js" : BASE."content/$path.js"; | |
// style | |
function style($href){ | |
$link = '<link href="%s" rel="stylesheet" type="text/css">'; | |
return $href ? sprintf($link, $href) : ''; | |
} | |
$f = DIR ? $filepath . DS . 'style.css' : "$filepath.css"; | |
if( file_exists($f) ) | |
$style = DIR ? BASE."content/$path/style.css" : BASE."content/$path.css"; | |
// layout | |
$f = DIR ? $filepath . DS . 'layout' : "$filepath.layout"; | |
if(file_exists("$f.php")){ | |
include "$f.php"; | |
exit(0); | |
} | |
if( file_exists("$f.html") ) | |
$layout = @file_get_contents("$f.html"); | |
else | |
$layout = @file_get_contents('layout.html'); | |
// render | |
if($layout && $content){ | |
$output = strtr($layout, array( | |
'%script%' => script($script), | |
'%style%' => style($style), | |
'%title%' => isset($meta['title']) ? $meta['title'] : $path, | |
'%content%' => $content, | |
'%meta%' => meta($meta), | |
'%relpath%' => BASE . $path, | |
'%basepath%' => BASE | |
)); | |
cache($path, $output); | |
echo $output; | |
} | |
else{ | |
@header( CGI ? "Status: 404 Not Found" : "HTTP/1.1 404 Not Found"); | |
readfile(WEBDIR.DS.'404.html'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment