Browse Source

Merge pull request #1 from gilbitron/master

update head
Frank Nägler 11 năm trước cách đây
mục cha
commit
a3f5b70d1b
3 tập tin đã thay đổi với 54 bổ sung29 xóa
  1. 9 0
      changelog.txt
  2. 34 23
      lib/pico.php
  3. 11 6
      plugins/pico_plugin.php

+ 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

+ 34 - 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 {
 
@@ -22,7 +22,11 @@ class Pico {
 		// Load plugins
 		$this->load_plugins();
 		$this->run_hooks('plugins_loaded');
-		
+
+		// Load the settings
+		$settings = $this->get_config();
+		$this->run_hooks('config_loaded', array(&$settings));
+
 		// Get request url and script url
 		$url = '';
 		$request_url = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : '';
@@ -51,15 +55,14 @@ class Pico {
 			$this->run_hooks('after_404_load_content', array(&$file, &$content));
 		}
 		$this->run_hooks('after_load_content', array(&$file, &$content));
-		
-		// Load the settings
-		$settings = $this->get_config();
-		$this->run_hooks('config_loaded', array(&$settings));
 
 		$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);
-		$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']);
@@ -98,8 +101,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;
 	}
@@ -107,7 +112,7 @@ class Pico {
 	/**
 	 * Load any plugins
 	 */
-	private function load_plugins()
+	protected function load_plugins()
 	{
 		$this->plugins = array();
 		$plugins = $this->get_files(PLUGINS_DIR, '.php');
@@ -129,7 +134,7 @@ class Pico {
 	 * @param string $content the raw txt content
 	 * @return string $content the Markdown formatted content
 	 */
-	private function parse_content($content)
+	protected function parse_content($content)
 	{
 		$content = preg_replace('#/\*.+?\*/#s', '', $content); // Remove comments and meta
 		$content = str_replace('%base_url%', $this->base_url(), $content);
@@ -144,7 +149,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;
 		
@@ -153,7 +158,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
@@ -177,7 +183,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');
@@ -207,7 +213,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;
 		
@@ -265,7 +271,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){
@@ -281,7 +287,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'];
@@ -300,10 +306,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;
 	}
 	     
 	/**
@@ -313,7 +322,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)){
@@ -339,10 +348,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)
 	{
 		
 	}