浏览代码

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
Gilbert Pellegrom 12 年之前
父节点
当前提交
842c05824a
共有 4 个文件被更改,包括 95 次插入21 次删除
  1. 8 0
      changelog.txt
  2. 8 1
      config.php
  3. 23 2
      content/index.md
  4. 56 18
      lib/pico.php

+ 8 - 0
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

+ 8 - 1
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:
 

+ 23 - 2
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:
 
 <pre>&lt;ul class=&quot;nav&quot;&gt;
 	{% for page in pages %}
@@ -73,6 +92,8 @@ All themes must include an `index.html` file to define the HTML structure of the
 	{% endfor %}
 &lt;/ul&gt;</pre>
 
+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

+ 56 - 18
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;
 	}
 
 	/**