Просмотр исходного кода

Merge c4e2fc10e153d83c2b57d0e8811d8d069bfc2c00 into b2330ad357ca06d5c850b4cad63138f2f631314a

Miguel Angel Sosvilla Luis 11 лет назад
Родитель
Сommit
afab76c47c
3 измененных файлов с 57 добавлено и 22 удалено
  1. 3 0
      README.md
  2. 1 0
      index.php
  3. 53 22
      lib/pico.php

+ 3 - 0
README.md

@@ -3,4 +3,7 @@ Pico
 
 
 Pico is a stupidly simple, blazing fast, flat file CMS. See http://picocms.org/ for more info.
 Pico is a stupidly simple, blazing fast, flat file CMS. See http://picocms.org/ for more info.
 
 
+This fork allows Pico parse content using Twig (.html files).
+
+
 [![I Love Open Source](http://www.iloveopensource.io/images/logo-lightbg.png)](http://www.iloveopensource.io/projects/524c55dcca7964c617000756)
 [![I Love Open Source](http://www.iloveopensource.io/images/logo-lightbg.png)](http://www.iloveopensource.io/projects/524c55dcca7964c617000756)

+ 1 - 0
index.php

@@ -3,6 +3,7 @@
 define('ROOT_DIR', realpath(dirname(__FILE__)) .'/');
 define('ROOT_DIR', realpath(dirname(__FILE__)) .'/');
 define('CONTENT_DIR', ROOT_DIR .'content/');
 define('CONTENT_DIR', ROOT_DIR .'content/');
 define('CONTENT_EXT', '.md');
 define('CONTENT_EXT', '.md');
+define('CONTENT_EXT_TWIG', '.html');
 define('LIB_DIR', ROOT_DIR .'lib/');
 define('LIB_DIR', ROOT_DIR .'lib/');
 define('PLUGINS_DIR', ROOT_DIR .'plugins/');
 define('PLUGINS_DIR', ROOT_DIR .'plugins/');
 define('THEMES_DIR', ROOT_DIR .'themes/');
 define('THEMES_DIR', ROOT_DIR .'themes/');

+ 53 - 22
lib/pico.php

@@ -42,12 +42,18 @@ class Pico {
 		else $file = CONTENT_DIR .'index';
 		else $file = CONTENT_DIR .'index';
 
 
 		// Load the file
 		// Load the file
-		if(is_dir($file)) $file = CONTENT_DIR . $url .'/index'. CONTENT_EXT;
-		else $file .= CONTENT_EXT;
+		if(is_dir($file)) $file = CONTENT_DIR . $url .'/index';
 
 
 		$this->run_hooks('before_load_content', array(&$file));
 		$this->run_hooks('before_load_content', array(&$file));
-		if(file_exists($file)){
+
+		$isTwigContent=false;
+		if(file_exists($file . CONTENT_EXT)){
+			$file=$file . CONTENT_EXT;
+			$content = file_get_contents($file);
+		}elseif (file_exists ($file . CONTENT_EXT_TWIG)) {
+			$file=$file . CONTENT_EXT_TWIG;
 			$content = file_get_contents($file);
 			$content = file_get_contents($file);
+			$isTwigContent=true;
 		} else {
 		} else {
 			$this->run_hooks('before_404_load_content', array(&$file));
 			$this->run_hooks('before_404_load_content', array(&$file));
 			$content = file_get_contents(CONTENT_DIR .'404'. CONTENT_EXT);
 			$content = file_get_contents(CONTENT_DIR .'404'. CONTENT_EXT);
@@ -59,11 +65,7 @@ class Pico {
 		$meta = $this->read_file_meta($content);
 		$meta = $this->read_file_meta($content);
 		$this->run_hooks('file_meta', array(&$meta));
 		$this->run_hooks('file_meta', array(&$meta));
 
 
-		$this->run_hooks('before_parse_content', array(&$content));
-		$content = $this->parse_content($content);
-		$this->run_hooks('after_parse_content', array(&$content));
-		$this->run_hooks('content_parsed', array(&$content)); // Depreciated @ v0.8
-		
+	
 		// Get all the pages
 		// Get all the pages
 		$pages = $this->get_pages($settings['base_url'], $settings['pages_order_by'], $settings['pages_order'], $settings['excerpt_length']);
 		$pages = $this->get_pages($settings['base_url'], $settings['pages_order_by'], $settings['pages_order'], $settings['excerpt_length']);
 		$prev_page = array();
 		$prev_page = array();
@@ -80,12 +82,8 @@ class Pico {
 		$next_page = prev($pages);
 		$next_page = prev($pages);
 		$this->run_hooks('get_pages', array(&$pages, &$current_page, &$prev_page, &$next_page));
 		$this->run_hooks('get_pages', array(&$pages, &$current_page, &$prev_page, &$next_page));
 
 
-		// Load the theme
-		$this->run_hooks('before_twig_register');
-		Twig_Autoloader::register();
-		$loader = new Twig_Loader_Filesystem(THEMES_DIR . $settings['theme']);
-		$twig = new Twig_Environment($loader, $settings['twig_config']);
-		$twig->addExtension(new Twig_Extension_Debug());
+
+		/* Parse content*/
 		$twig_vars = array(
 		$twig_vars = array(
 			'config' => $settings,
 			'config' => $settings,
 			'base_dir' => rtrim(ROOT_DIR, '/'),
 			'base_dir' => rtrim(ROOT_DIR, '/'),
@@ -94,14 +92,27 @@ class Pico {
 			'theme_url' => $settings['base_url'] .'/'. basename(THEMES_DIR) .'/'. $settings['theme'],
 			'theme_url' => $settings['base_url'] .'/'. basename(THEMES_DIR) .'/'. $settings['theme'],
 			'site_title' => $settings['site_title'],
 			'site_title' => $settings['site_title'],
 			'meta' => $meta,
 			'meta' => $meta,
-			'content' => $content,
 			'pages' => $pages,
 			'pages' => $pages,
 			'prev_page' => $prev_page,
 			'prev_page' => $prev_page,
 			'current_page' => $current_page,
 			'current_page' => $current_page,
 			'next_page' => $next_page,
 			'next_page' => $next_page,
-			'is_front_page' => $url ? false : true,
+			'is_front_page' => $url ? false : true
 		);
 		);
 
 
+		$this->run_hooks('before_parse_content', array(&$content));
+		$content = $this->parse_content($content, $isTwigContent, $twig_vars);
+		$this->run_hooks('after_parse_content', array(&$content));
+		$this->run_hooks('content_parsed', array(&$content)); // Depreciated @ v0.8
+
+		
+		// Load the theme
+		$this->run_hooks('before_twig_register');
+		$loader = new Twig_Loader_Filesystem(THEMES_DIR . $settings['theme']);
+		$twig = new Twig_Environment($loader, $settings['twig_config']);
+		$twig->addExtension(new Twig_Extension_Debug());
+
+		$twig_vars['content'] = $content;
+
 		$template = (isset($meta['template']) && $meta['template']) ? $meta['template'] : 'index';
 		$template = (isset($meta['template']) && $meta['template']) ? $meta['template'] : 'index';
 		$this->run_hooks('before_render', array(&$twig_vars, &$twig, &$template));
 		$this->run_hooks('before_render', array(&$twig_vars, &$twig, &$template));
 		$output = $twig->render($template .'.html', $twig_vars);
 		$output = $twig->render($template .'.html', $twig_vars);
@@ -129,17 +140,26 @@ class Pico {
 	}
 	}
 
 
 	/**
 	/**
-	 * Parses the content using Markdown
+	 * Parses the content using Markdown or Twig
 	 *
 	 *
 	 * @param string $content the raw txt content
 	 * @param string $content the raw txt content
-	 * @return string $content the Markdown formatted content
+	 * @return string $content the Markdown or Twig formatted content
 	 */
 	 */
-	protected function parse_content($content)
+
+	protected function parse_content($content, $isTwigContent = false, $twig_vars = null)
 	{
 	{
 		$content = preg_replace('#/\*.+?\*/#s', '', $content); // Remove comments and meta
 		$content = preg_replace('#/\*.+?\*/#s', '', $content); // Remove comments and meta
-		$content = str_replace('%base_url%', $this->base_url(), $content);
-		$content = MarkdownExtra::defaultTransform($content);
+		
+		if ($isTwigContent){
+			$loader = new Twig_Loader_String();
+			$twig = new Twig_Environment($loader);
+			$content=$twig->render($content, $twig_vars);
+		}else{
+			$content = str_replace('%base_url%', $this->base_url(), $content);  
+			$content = MarkdownExtra::defaultTransform($content);	
+		}	
 
 
+	
 		return $content;
 		return $content;
 	}
 	}
 
 
@@ -218,6 +238,7 @@ class Pico {
 		global $config;
 		global $config;
 		
 		
 		$pages = $this->get_files(CONTENT_DIR, CONTENT_EXT);
 		$pages = $this->get_files(CONTENT_DIR, CONTENT_EXT);
+		$pages = array_merge($pages,$this->get_files(CONTENT_DIR, CONTENT_EXT_TWIG));
 		$sorted_pages = array();
 		$sorted_pages = array();
 		$date_id = 0;
 		$date_id = 0;
 		foreach($pages as $key=>$page){
 		foreach($pages as $key=>$page){
@@ -235,10 +256,20 @@ class Pico {
 			// Get title and format $page
 			// Get title and format $page
 			$page_content = file_get_contents($page);
 			$page_content = file_get_contents($page);
 			$page_meta = $this->read_file_meta($page_content);
 			$page_meta = $this->read_file_meta($page_content);
-			$page_content = $this->parse_content($page_content);
+
+			// Only parse content for Markdown files.
+			if ('.' . pathinfo($page,PATHINFO_EXTENSION) == CONTENT_EXT){
+				$page_content = $this->parse_content($page_content);
+			}else{
+				$page_content="";
+			}	
+
 			$url = str_replace(CONTENT_DIR, $base_url .'/', $page);
 			$url = str_replace(CONTENT_DIR, $base_url .'/', $page);
 			$url = str_replace('index'. CONTENT_EXT, '', $url);
 			$url = str_replace('index'. CONTENT_EXT, '', $url);
+			$url = str_replace('index'. CONTENT_EXT_TWIG, '', $url);
 			$url = str_replace(CONTENT_EXT, '', $url);
 			$url = str_replace(CONTENT_EXT, '', $url);
+			$url = str_replace(CONTENT_EXT_TWIG, '', $url);
+
 			$data = array(
 			$data = array(
 				'title' => isset($page_meta['title']) ? $page_meta['title'] : '',
 				'title' => isset($page_meta['title']) ? $page_meta['title'] : '',
 				'url' => $url,
 				'url' => $url,