Переглянути джерело

Merge b403f5a04158fbf728e34e79d6c6785a08d6cf96 into aa59661ff81dd52c3a2596988372a214b0fc31b9

Frank Nägler 11 роки тому
батько
коміт
2e4b627330
5 змінених файлів з 168 додано та 34 видалено
  1. 4 1
      .gitignore
  2. 2 0
      index.php
  3. 108 0
      lib/Pico/Model/AbstractModel.php
  4. 45 0
      lib/Pico/Model/Meta.php
  5. 9 33
      lib/pico.php

+ 4 - 1
.gitignore

@@ -14,10 +14,13 @@ Icon?
 Thumbs.db
 *.swp
 
+# PHPStorm settings
+.idea
+
 # User themes
 themes/*
 !themes/index.html
 !themes/default/*
 
 # User config
-config.php
+config.php

+ 2 - 0
index.php

@@ -9,5 +9,7 @@ define('THEMES_DIR', ROOT_DIR .'themes/');
 define('CACHE_DIR', LIB_DIR .'cache/');
 
 require(ROOT_DIR .'vendor/autoload.php');
+require_once LIB_DIR . 'Pico/Model/AbstractModel.php';
+require_once LIB_DIR . 'Pico/Model/Meta.php';
 require(LIB_DIR .'pico.php');
 $pico = new Pico();

+ 108 - 0
lib/Pico/Model/AbstractModel.php

@@ -0,0 +1,108 @@
+<?php
+
+namespace Pico\Model;
+
+/**
+ * Abstract model which implements the ArrayAccess interface
+ *
+ * @author Frank Nägler
+ * @link http://pico.dev7studios.com
+ * @license http://opensource.org/licenses/MIT
+ * @version 0.1
+ */
+class AbstractModel implements \ArrayAccess {
+	protected $data  = array();
+
+	public function get($key) {
+		$key = ucfirst($key);
+		return (isset($this->data[$key])) ? $this->data[$key] : null;
+	}
+
+	public function set($key, $value) {
+		$this->data[$key] = $value;
+	}
+
+	public function __get($name) {
+		return $this->get($name);
+	}
+
+	public function __set($name, $value) {
+		$this->set($name, $value);
+	}
+
+	public function __call($name, $args) {
+		if (strpos($name, 'get') !== false) {
+			$name = substr($name, 3);
+			return $this->get($name);
+		}
+		if (strpos($name, 'set') !== false) {
+			$name = substr($name, 3);
+			return $this->set($name, $args[0]);
+		}
+
+	}
+
+	/**
+	 * (PHP 5 &gt;= 5.0.0)<br/>
+	 * Whether a offset exists
+	 *
+	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
+	 * @param mixed $offset <p>
+	 *                      An offset to check for.
+	 *                      </p>
+	 * @return boolean true on success or false on failure.
+	 *                      </p>
+	 *                      <p>
+	 *                      The return value will be casted to boolean if non-boolean was returned.
+	 */
+	public function offsetExists($offset) {
+		return (isset($this->data[$offset]));
+	}
+
+	/**
+	 * (PHP 5 &gt;= 5.0.0)<br/>
+	 * Offset to retrieve
+	 *
+	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
+	 * @param mixed $offset <p>
+	 *                      The offset to retrieve.
+	 *                      </p>
+	 * @return mixed Can return all value types.
+	 */
+	public function offsetGet($offset) {
+		return $this->get($offset);
+	}
+
+	/**
+	 * (PHP 5 &gt;= 5.0.0)<br/>
+	 * Offset to set
+	 *
+	 * @link http://php.net/manual/en/arrayaccess.offsetset.php
+	 * @param mixed $offset <p>
+	 *                      The offset to assign the value to.
+	 *                      </p>
+	 * @param mixed $value  <p>
+	 *                      The value to set.
+	 *                      </p>
+	 * @return void
+	 */
+	public function offsetSet($offset, $value) {
+		$this->set($offset, $value);
+	}
+
+	/**
+	 * (PHP 5 &gt;= 5.0.0)<br/>
+	 * Offset to unset
+	 *
+	 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
+	 * @param mixed $offset <p>
+	 *                      The offset to unset.
+	 *                      </p>
+	 * @return void
+	 */
+	public function offsetUnset($offset) {
+		if ($this->offsetExists($offset)) {
+			unset($this->data[$offset]);
+		}
+	}
+}

+ 45 - 0
lib/Pico/Model/Meta.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace Pico\Model;
+
+/**
+ * Meta model
+ *
+ * @author Frank Nägler
+ * @link http://pico.dev7studios.com
+ * @license http://opensource.org/licenses/MIT
+ * @version 0.1
+ */
+class Meta extends AbstractModel {
+	public function __construct($rawData = null) {
+		if ($rawData !== null) {
+			$this->setRawData($rawData);
+		}
+	}
+
+	public function setRawData($rawData) {
+		$this->parseRawData($rawData);
+	}
+	
+	public function getFormattedDate() {
+		global $config;
+		if (isset($this->data['date'])) {
+			return date($config['date_format'], strtotime($this->data['date']));
+		}
+		return null;
+	}
+
+	protected function parseRawData($rawData) {
+		$metaPart   = substr($rawData, 0, strpos($rawData, '*/'));
+		if (strpos($metaPart, '/*') == 0) {
+			$metaPart   = trim(substr($metaPart, 2));
+			$headers    = explode("\n", $metaPart);
+			foreach ($headers as $line) {
+				$parts  = explode(':', $line);
+				$key    = array_shift($parts);
+				$val    = implode($parts);
+				$this->set($key, trim($val));
+			}
+		}
+	}
+}

+ 9 - 33
lib/pico.php

@@ -70,7 +70,7 @@ class Pico {
 		$current_page = array();
 		$next_page = array();
 		while($current_page = current($pages)){
-			if((isset($meta['title'])) && ($meta['title'] == $current_page['title'])){
+			if($meta->Title && ($meta->Title == $current_page['title'])) {
 				break;
 			}
 			next($pages);
@@ -102,7 +102,7 @@ class Pico {
 			'is_front_page' => $url ? false : true,
 		);
 
-		$template = (isset($meta['template']) && $meta['template']) ? $meta['template'] : 'index';
+		$template = ($meta->Template !== null) ? $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));
@@ -151,31 +151,7 @@ class Pico {
 	 */
 	protected function read_file_meta($content)
 	{
-		global $config;
-		
-		$headers = array(
-			'title'       	=> 'Title',
-			'description' 	=> 'Description',
-			'author' 		=> 'Author',
-			'date' 			=> 'Date',
-			'robots'     	=> 'Robots',
-			'template'      => 'Template'
-		);
-
-		// Add support for custom headers by hooking into the headers array
-		$this->run_hooks('before_read_file_meta', array(&$headers));
-
-	 	foreach ($headers as $field => $regex){
-			if (preg_match('/^[ \t\/*#@]*' . preg_quote($regex, '/') . ':(.*)$/mi', $content, $match) && $match[1]){
-				$headers[ $field ] = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $match[1]));
-			} else {
-				$headers[ $field ] = '';
-			}
-		}
-		
-		if(isset($headers['date'])) $headers['date_formatted'] = date($config['date_format'], strtotime($headers['date']));
-
-		return $headers;
+		return new \Pico\Model\Meta($content);
 	}
 
 	/**
@@ -240,11 +216,11 @@ class Pico {
 			$url = str_replace('index'. CONTENT_EXT, '', $url);
 			$url = str_replace(CONTENT_EXT, '', $url);
 			$data = array(
-				'title' => isset($page_meta['title']) ? $page_meta['title'] : '',
+				'title' => ($page_meta->Title !== null) ? $page_meta->Title : '',
 				'url' => $url,
-				'author' => isset($page_meta['author']) ? $page_meta['author'] : '',
-				'date' => isset($page_meta['date']) ? $page_meta['date'] : '',
-				'date_formatted' => isset($page_meta['date']) ? date($config['date_format'], strtotime($page_meta['date'])) : '',
+				'author' => ($page_meta->Author !== null) ? $page_meta>Author : '',
+				'date' => ($page_meta->Date) ? $page_meta->Date : '',
+				'date_formatted' => ($page_meta->Date) ? date($config['date_format'], strtotime($page_meta->Date)) : '',
 				'content' => $page_content,
 				'excerpt' => $this->limit_words(strip_tags($page_content), $excerpt_length)
 			);
@@ -252,8 +228,8 @@ class Pico {
 			// Extend the data provided with each page by hooking into the data array
 			$this->run_hooks('get_page_data', array(&$data, $page_meta));
 
-			if($order_by == 'date' && isset($page_meta['date'])){
-				$sorted_pages[$page_meta['date'].$date_id] = $data;
+			if($order_by == 'date' && $page_meta->Date !== null){
+				$sorted_pages[$page_meta->Date.$date_id] = $data;
 				$date_id++;
 			}
 			else $sorted_pages[] = $data;