From 842c05824a2f64de546eb7fc31261be9adabcf44 Mon Sep 17 00:00:00 2001 From: Gilbert Pellegrom Date: Fri, 3 May 2013 15:45:42 +0100 Subject: [PATCH] v0.5 [New] Added ability to order pages by "alpha" or "date" (asc or desc) [New] Added prev_page, current_page, next_page and is_front_page template vars [New] Added "Author" and "Date" title meta fields [Changed] Added "twig_config" to settings [Changed] Updated documentation [Fixed] Query string 404 bug --- changelog.txt | 8 ++++++ config.php | 9 +++++- content/index.md | 25 ++++++++++++++-- lib/pico.php | 74 ++++++++++++++++++++++++++++++++++++------------ 4 files changed, 95 insertions(+), 21 deletions(-) diff --git a/changelog.txt b/changelog.txt index 1f0187a..d373de1 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,13 @@ *** Pico Changelog *** +2013.05.03 - version 0.5 + * [New] Added ability to order pages by "alpha" or "date" (asc or desc) + * [New] Added prev_page, current_page, next_page and is_front_page template vars + * [New] Added "Author" and "Date" title meta fields + * [Changed] Added "twig_config" to settings + * [Changed] Updated documentation + * [Fixed] Query string 404 bug + 2013.05.01 - version 0.4.1 * [New] Added CONTENT_EXT global * [Changed] Use .md files instead of .txt diff --git a/config.php b/config.php index 56b5b2a..243e358 100644 --- a/config.php +++ b/config.php @@ -6,7 +6,14 @@ $config['site_title'] = 'Pico'; // Site title $config['base_url'] = ''; // Override base URL (e.g. http://example.com) $config['theme'] = 'default'; // Set the theme (defaults to "default") -$config['enable_cache'] = false; // Enable caching +$config['date_format'] = 'jS M Y'; // Set the PHP date format +$config['twig_config'] = array( // Twig settings + 'cache' => false, // To enable Twig caching change this to CACHE_DIR + 'autoescape' => false, // Autoescape Twig vars + 'debug' => false // Enable Twig debug +); +$config['pages_order_by'] = 'alpha'; // Order pages by "alpha" or "date" +$config['pages_order'] = 'asc'; // Order pages "asc" or "desc" // To add a custom config setting: diff --git a/content/index.md b/content/index.md index 0cdfb3b..8ab8d55 100644 --- a/content/index.md +++ b/content/index.md @@ -40,6 +40,8 @@ At the top of text files you can place a block comment and specify certain attri / * Title: Welcome Description: This description will go in the meta description tag + Author: Joe Bloggs + Date: 2013/01/01 Robots: noindex,nofollow */ @@ -63,9 +65,26 @@ All themes must include an `index.html` file to define the HTML structure of the * `{{ theme_dir }}` - The path to the Pico active theme direcotry * `{{ theme_url }}` - The URL to the Pico active theme direcotry * `{{ site_title }}` - Shortcut to the site title (defined in config.php) -* `{{ meta }}` - Contains the meta values from the current page (e.g. `{{ meta.title }}`, `{{ meta.description }}`, `{{ meta.robots }}`) +* `{{ meta }}` - Contains the meta values from the current page + * `{{ meta.title }}` + * `{{ meta.description }}` + * `{{ meta.author }}` + * `{{ meta.date }}` + * `{{ meta.date_formatted }}` + * `{{ meta.robots }}` * `{{ content }}` - The content of the current page (after it has been processed through Markdown) -* `{{ pages }}` - A collection of all the content in your site. Use it like: +* `{{ pages }}` - A collection of all the content in your site + * `{{ page.title }}` + * `{{ page.url }}` + * `{{ page.author }}` + * `{{ page.date }}` + * `{{ page.date_formatted }}` +* `{{ prev_page }}` - A page object of the previous page (relative to current_page) +* `{{ current_page }}` - A page object of the current_page +* `{{ next_page }}` - A page object of the next page (relative to current_page) +* `{{ is_front_page }}` - A boolean flag for the front page + +Pages can be used like:
<ul class="nav">
 	{% for page in pages %}
@@ -73,6 +92,8 @@ All themes must include an `index.html` file to define the HTML structure of the
 	{% endfor %}
 </ul>
+Note the `{{ pages }}` objects don't contain `content`. It is mainly to be used in creating navigaiton. + ### Config You can override the default Pico settings (and add your own custom settings) by editing config.php in the root Pico directory. The config.php file diff --git a/lib/pico.php b/lib/pico.php index 5a5d305..18a4c74 100644 --- a/lib/pico.php +++ b/lib/pico.php @@ -6,7 +6,7 @@ * @author Gilbert Pellegrom * @link http://pico.dev7studios.com/ * @license http://opensource.org/licenses/MIT - * @version 0.4.1 + * @version 0.5 */ class Pico { @@ -23,6 +23,7 @@ class Pico { // Get our url path and trim the / of the left and the right if($request_url != $script_url) $url = trim(preg_replace('/'. str_replace('/', '\/', str_replace('index.php', '', $script_url)) .'/', '', $request_url, 1), '/'); + $url = preg_replace('/\?.*/', '', $url); // Strip query string // Get the file path if($url) $file = CONTENT_DIR . $url; @@ -37,23 +38,34 @@ class Pico { $content = file_get_contents(CONTENT_DIR .'404'. CONTENT_EXT); header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found'); } + + // Load the settings + $settings = $this->get_config(); $meta = $this->read_file_meta($content); $content = preg_replace('#/\*.+?\*/#s', '', $content); // Remove comments and meta $content = $this->parse_content($content); - - // Load the settings - $settings = $this->get_config(); - $env = array('autoescape' => false); - if($settings['enable_cache']) $env['cache'] = CACHE_DIR; // Get all the pages - $pages = $this->get_pages($settings['base_url']); + $pages = $this->get_pages($settings['base_url'], $settings['pages_order_by'], $settings['pages_order']); + $prev_page = array(); + $current_page = array(); + $next_page = array(); + while($current_page = current($pages)){ + if($meta['title'] == $current_page['title']){ + break; + } + next($pages); + } + $prev_page = next($pages); + prev($pages); + $next_page = prev($pages); // Load the theme Twig_Autoloader::register(); $loader = new Twig_Loader_Filesystem(THEMES_DIR . $settings['theme']); - $twig = new Twig_Environment($loader, $env); + $twig = new Twig_Environment($loader, $settings['twig_config']); + $twig->addExtension(new Twig_Extension_Debug()); echo $twig->render('index.html', array( 'config' => $settings, 'base_dir' => rtrim(ROOT_DIR, '/'), @@ -63,7 +75,11 @@ class Pico { 'site_title' => $settings['site_title'], 'meta' => $meta, 'content' => $content, - 'pages' => $pages + 'pages' => $pages, + 'prev_page' => $prev_page, + 'current_page' => $current_page, + 'next_page' => $next_page, + 'is_front_page' => $url ? false : true, )); } @@ -89,10 +105,14 @@ class Pico { */ function read_file_meta($content) { + global $config; + $headers = array( - 'title' => 'Title', - 'description' => 'Description', - 'robots' => 'Robots' + 'title' => 'Title', + 'description' => 'Description', + 'author' => 'Author', + 'date' => 'Date', + 'robots' => 'Robots' ); foreach ($headers as $field => $regex){ @@ -102,6 +122,8 @@ class Pico { $headers[ $field ] = ''; } } + + if($headers['date']) $headers['date_formatted'] = date($config['date_format'], strtotime($headers['date'])); return $headers; } @@ -122,7 +144,10 @@ class Pico { 'site_title' => 'Pico', 'base_url' => $this->base_url(), 'theme' => 'default', - 'enable_cache' => false + 'date_format' => 'jS M Y', + 'twig_config' => array('cache' => false, 'autoescape' => false, 'debug' => false), + 'pages_order_by' => 'alpha', + 'pages_order' => 'asc' ); if(is_array($config)) $config = array_merge($defaults, $config); @@ -135,11 +160,16 @@ class Pico { * Get a list of pages * * @param string $base_url the base URL of the site - * @return array $pages an array of pages + * @param string $order_by order by "alpha" or "date" + * @param string $order order "asc" or "desc" + * @return array $sorted_pages an array of pages */ - function get_pages($base_url) + function get_pages($base_url, $order_by = 'alpha', $order = 'asc') { + global $config; + $pages = $this->glob_recursive(CONTENT_DIR .'*'. CONTENT_EXT); + $sorted_pages = array(); foreach($pages as $key=>$page){ // Skip 404 if(basename($page) == '404'. CONTENT_EXT){ @@ -153,13 +183,21 @@ class Pico { $url = str_replace(CONTENT_DIR, $base_url .'/', $page); $url = str_replace('index'. CONTENT_EXT, '', $url); $url = str_replace(CONTENT_EXT, '', $url); - $pages[$key] = array( + $data = array( 'title' => $page_meta['title'], - 'url' => $url + 'url' => $url, + 'author' => $page_meta['author'], + 'date' => $page_meta['date'], + 'date_formatted' => date($config['date_format'], strtotime($page_meta['date'])) ); + if($order_by == 'date') $sorted_pages[$page_meta['date']] = $data; + else $sorted_pages[] = $data; } - return $pages; + if($order == 'desc') krsort($sorted_pages); + else ksort($sorted_pages); + + return $sorted_pages; } /**