浏览代码

Merged Upstream (ModRewrite,curr_dir_url,Unicode page names patches carried forward).

Balázs Indig 11 年之前
父节点
当前提交
2869ef13a6
共有 5 个文件被更改,包括 71 次插入31 次删除
  1. 1 1
      README.md
  2. 9 0
      changelog.txt
  3. 17 1
      composer.json
  4. 33 23
      lib/pico.php
  5. 11 6
      plugins/pico_plugin.php

+ 1 - 1
README.md

@@ -3,4 +3,4 @@ Pico
 
 Pico is a stupidly simple, blazing fast, flat file CMS. See http://pico.dev7studios.com for more info.
 
-Want to say thanks? [Consider tipping me](https://www.gittip.com/gilbitron).
+[![I Love Open Source](http://www.iloveopensource.io/images/logo-lightbg.png)](http://www.iloveopensource.io/projects/524c55dcca7964c617000756)

+ 9 - 0
changelog.txt

@@ -1,5 +1,14 @@
 *** Pico Changelog ***
 
+2013.10.23 - version 0.8
+ * [New] Added ability to set template in content meta
+ * [New] Added before_parse_content and after_parse_content hooks
+ * [Changed] content_parsed hook is now depreciated
+ * [Changed] Moved loading the config to nearer the beginning of the class
+ * [Changed] Only append ellipsis in limit_words() when word count exceeds max
+ * [Changed] Made private methods protected for better inheritance
+ * [Fixed] Fixed get_protocol() method to work in more situations
+
 2013.09.04 - version 0.7
  * [New] Added before_read_file_meta and get_page_data plugin hooks to customize page meta data
  * [Changed] Make get_files() ignore dotfiles

+ 17 - 1
composer.json

@@ -1,6 +1,22 @@
 {
+    "name": "gilbitron/pico",
+    "type": "library",
+    "description": "Pico is a flat file CMS, this means there is no administration backend and database to deal with. You simply create .md files in the \"content\" folder and that becomes a page.",
+    "keywords": ["cms"],
+    "homepage": "http://pico.dev7studios.com",
+    "license": "MIT",
+    "authors": [
+        {
+            "name": "Gilbert Pellegrom",
+            "email": "gilbert@pellegrom.me"
+        }
+    ],
     "require": {
+        "php": ">=5.2.4",
         "twig/twig": "1.12.*",
-		"michelf/php-markdown": "1.3"
+        "michelf/php-markdown": "1.3"
+    },
+    "autoload": {
+        "files": ["lib/pico.php"]
     }
 }

+ 33 - 23
lib/pico.php

@@ -7,7 +7,7 @@ use \Michelf\MarkdownExtra;
  * @author Gilbert Pellegrom
  * @link http://pico.dev7studios.com
  * @license http://opensource.org/licenses/MIT
- * @version 0.7
+ * @version 0.8
  */
 class Pico {
 
@@ -19,15 +19,14 @@ class Pico {
 	 */
 	public function __construct()
 	{
+		// Load plugins
+		$this->load_plugins();
+		$this->run_hooks('plugins_loaded');
+
 		// Load the settings
 		$settings = $this->get_config();
 		$this->run_hooks('config_loaded', array(&$settings));
-		global $config;
 
-		// Load plugins
-		$this->load_plugins();
-		$this->run_hooks('plugins_loaded');
-		
 		// Get request url and script url
 		$url = '';
 		$request_url = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : '';
@@ -62,8 +61,11 @@ class Pico {
 
 		$meta = $this->read_file_meta($content);
 		$this->run_hooks('file_meta', array(&$meta));
+
+		$this->run_hooks('before_parse_content', array(&$content));
 		$content = $this->parse_content($content,$file);
-		$this->run_hooks('content_parsed', array(&$content));
+		$this->run_hooks('after_parse_content', array(&$content));
+		$this->run_hooks('content_parsed', array(&$content)); // Depreciated @ v0.8
 		
 		// Get all the pages
 		$pages = $this->get_pages($settings['base_url'], $settings['pages_order_by'], $settings['pages_order'], $settings['excerpt_length']);
@@ -102,8 +104,10 @@ class Pico {
 			'next_page' => $next_page,
 			'is_front_page' => $url ? false : true,
 		);
-		$this->run_hooks('before_render', array(&$twig_vars, &$twig));
-		$output = $twig->render('index.html', $twig_vars);
+
+		$template = (isset($meta['template']) && $meta['template']) ? $meta['template'] : 'index';
+		$this->run_hooks('before_render', array(&$twig_vars, &$twig, &$template));
+		$output = $twig->render($template .'.html', $twig_vars);
 		$this->run_hooks('after_render', array(&$output));
 		echo $output;
 	}
@@ -111,7 +115,7 @@ class Pico {
 	/**
 	 * Load any plugins
 	 */
-	private function load_plugins()
+	protected function load_plugins()
 	{
 		$this->plugins = array();
 		$plugins = $this->get_files(PLUGINS_DIR, '.php');
@@ -133,7 +137,7 @@ class Pico {
 	 * @param string $content the raw txt content
 	 * @return string $content the Markdown formatted content
 	 */
-	private function parse_content($content,$page)
+	protected function parse_content($content,$page)
 	{
 		$content = preg_replace('#/\*.+?\*/#s', '', $content); // Remove comments and meta
 		$content = str_replace('%base_url%', $this->base_url(), $content);
@@ -149,7 +153,7 @@ class Pico {
 	 * @param string $content the raw txt content
 	 * @return array $headers an array of meta values
 	 */
-	private function read_file_meta($content)
+	protected function read_file_meta($content)
 	{
 		global $config;
 		
@@ -158,7 +162,8 @@ class Pico {
 			'description' 	=> 'Description',
 			'author' 		=> 'Author',
 			'date' 			=> 'Date',
-			'robots'     	=> 'Robots'
+			'robots'     	=> 'Robots',
+			'template'      => 'Template'
 		);
 
 		// Add support for custom headers by hooking into the headers array
@@ -182,7 +187,7 @@ class Pico {
 	 *
 	 * @return array $config an array of config values
 	 */
-	private function get_config()
+	protected function get_config()
 	{
 		global $config;
 		@include_once(ROOT_DIR .'config.php');
@@ -213,7 +218,7 @@ class Pico {
 	 * @param string $order order "asc" or "desc"
 	 * @return array $sorted_pages an array of pages
 	 */
-	private function get_pages($base_url, $order_by = 'alpha', $order = 'asc', $excerpt_length = 50)
+	protected function get_pages($base_url, $order_by = 'alpha', $order = 'asc', $excerpt_length = 50)
 	{
 		global $config;
 		
@@ -273,7 +278,7 @@ class Pico {
 	 * @param string $hook_id the ID of the hook
 	 * @param array $args optional arguments
 	 */
-	private function run_hooks($hook_id, $args = array())
+	protected function run_hooks($hook_id, $args = array())
 	{
 		if(!empty($this->plugins)){
 			foreach($this->plugins as $plugin){
@@ -289,7 +294,7 @@ class Pico {
 	 *
 	 * @return string the base url
 	 */
-	private function base_url()
+	protected function base_url()
 	{
 		global $config;
 		if(isset($config['base_url']) && $config['base_url']) return $config['base_url'];
@@ -308,10 +313,13 @@ class Pico {
 	 *
 	 * @return string the current protocol
 	 */
-	private function get_protocol()
+	protected function get_protocol()
 	{
-		preg_match("|^HTTP[S]?|is",$_SERVER['SERVER_PROTOCOL'],$m);
-		return strtolower($m[0]);
+		$protocol = 'http';
+		if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off'){
+			$protocol = 'https';
+		}
+		return $protocol;
 	}
 	     
 	/**
@@ -321,7 +329,7 @@ class Pico {
 	 * @param string $ext optional limit to file extensions
 	 * @return array the matched files
 	 */ 
-	private function get_files($directory, $ext = '')
+	protected function get_files($directory, $ext = '')
 	{
 	    $array_items = array();
 	    if($handle = opendir($directory)){
@@ -347,10 +355,12 @@ class Pico {
 	 * @param int $word_limit the number of words to limit to
 	 * @return string the limited string
 	 */ 
-	private function limit_words($string, $word_limit)
+	protected function limit_words($string, $word_limit)
 	{
 		$words = explode(' ',$string);
-		return trim(implode(' ', array_splice($words, 0, $word_limit))) .'...';
+		$excerpt = trim(implode(' ', array_splice($words, 0, $word_limit)));
+		if(count($words) > $word_limit) $excerpt .= '…';
+		return $excerpt;
 	}
 
 }

+ 11 - 6
plugins/pico_plugin.php

@@ -12,6 +12,11 @@ class Pico_Plugin {
 	public function plugins_loaded()
 	{
 		
+	}
+
+	public function config_loaded(&$settings)
+	{
+		
 	}
 	
 	public function request_url(&$url)
@@ -39,22 +44,22 @@ class Pico_Plugin {
 		
 	}
 	
-	public function config_loaded(&$settings)
+	public function before_read_file_meta(&$headers)
 	{
 		
 	}
 	
-	public function before_read_file_meta(&$headers)
+	public function file_meta(&$meta)
 	{
 		
 	}
-	
-	public function file_meta(&$meta)
+
+	public function before_parse_content(&$content)
 	{
 		
 	}
 	
-	public function content_parsed(&$content)
+	public function after_parse_content(&$content)
 	{
 		
 	}
@@ -74,7 +79,7 @@ class Pico_Plugin {
 		
 	}
 	
-	public function before_render(&$twig_vars, &$twig)
+	public function before_render(&$twig_vars, &$twig, &$template)
 	{
 		
 	}