Skip to content

Instantly share code, notes, and snippets.

@luminarious

luminarious/static.php

Last active Jul 26, 2018
Embed
What would you like to do?
Getting a static site out of Cockpit CMS
<?php
use Pug; // https://github.com/pug-php/pug
use Cockpit;
require_once __DIR__.'/vendor/autoload.php'; // Composer
require_once __DIR__.'/cockpit/bootstrap.php'; // https://github.com/COCOPi/cockpit
chdir(dirname(__FILE__)); // Keep this directory if called directly or from Cockpit
//setlocale(LC_TIME, 'et_EE', 'et', 'EE', 'ET', 'Estonia');
//define('DATE_FORMAT', "%e. %B %G");
if (basename(__FILE__) == basename($_SERVER["SCRIPT_FILENAME"])) define ('INCLUDED', true);
// Cleanup
array_map('unlink', glob("public/uudised/*html*"));
array_map('unlink', glob("public/teenused/*html*"));
array_map('unlink', glob("public/*html*"));
// Sensible defaults
$default_params = [
'site'=>[
'base_url'=>'/'
],
'page'=>[
'title'=>'Default',
'title_slug'=>'default', // May be worth it to generate here?
'description'=>'Default',
'content'=>'Default'
],
'template'=>'page.pug',
'filename'=>'page.html'
];
$default_params['site']['favicon'] = getFavicon('faviconData.json');
// Initialize
$export = configure($default_params);
$date = prettyDate([
'et_EE',
\IntlDateFormatter::LONG,
\IntlDateFormatter::NONE,
'Europe/Tallinn'
]);
// Pages
$pages = array_map(function($page) use ($date) { // Transform pages
$page['date'] = $date($page['created']);
return $page;
}, cockpit("collections")->find('Lehed')->toArray());
foreach($pages as $page) { // Export pages
$params = [
'page'=>$page,
'filename'=>$page['name_slug'].'.html'
];
if ($page['name_slug'] == 'index') $params['template'] = 'index.pug';
$export($params);
}
// Error pages
$export([
'page'=>[
'title'=>'Viga',
'content'=>'Vabandust, tundub et meil on tekkinud mingi probleem.',
],
'filename'=>'500.html'
]);
$export([
'page'=>[
'title'=>'Lehte pole',
'content'=>'Vabandust, tundub et seda lehte praegu pole.',
],
'filename'=>'404.html'
]);
// News
// $news = cockpit('collections')->find('news', ['sort'=> ['created'=>1]]);
$news = array_map(function($item) use ($date) { // Transform items
$item['date'] = $date($item['created']);
return $item;
}, cockpit("collections")->find('Uudised')->toArray());
foreach ($news as $item){ // Export items
$export([
'page'=>$item,
'filename'=>'uudised/'.$item['title_slug'].'.html'
]);
}
// News: index
$links = array_map(function($item) use ($date) { // Transform items
$item['date'] = $date($item['created']);
return $item;
}, cockpit("collections")->find('Meedialingid')->toArray());
$news = array_merge($news, $links);
$export([
'page'=>[
'title'=>'Uudised',
'description'=>'SMITis juhtub',
],
'news'=>$news,
'template'=>'news.pug',
'filename'=>'uudised/index.html'
]);
// Static generator configurator
function configure($default_params) {
$pug = new Pug();
return function(...$params) use ($default_params, $pug) {
$params = array_reduce($params, "array_merge", $default_params);
if (defined('INCLUDED')) {
echo '<pre>'.htmlspecialchars(print_r($params, true)).'</pre>';
}
$pug = new Pug();
$html = $pug->render(__DIR__.'/source/views/'.$params['template'], $params);
file_put_contents(__DIR__.'/public/'.$params['filename'], $html);
file_put_contents(__DIR__.'/public/'.$params['filename'].'.gz', gzencode($html, 9)); // http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html
};
}
// Formatted date
function prettyDate($config){
$formatter = datefmt_create(...$config);
return function($timestamp) use ($formatter) {
return datefmt_format($formatter, $timestamp);
};
}
// Favicon html (http://realfavicongenerator.net/)
function getFavicon($filename){
$favicon = json_decode(file_get_contents($filename), true);
return str_replace(array("\r", "\n"), '', $favicon['favicon']['html_code']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment