Pārlūkot izejas kodu

Merge 4421c7083b2527e188c9dbb1a1f2125570abe5e6 into 070cb92661005379ba08397a24511d85e0d4016a

ghuger 12 gadi atpakaļ
vecāks
revīzija
23b821ce37
1 mainītis faili ar 22 papildinājumiem un 0 dzēšanām
  1. 22 0
      lib/pico.php

+ 22 - 0
lib/pico.php

@@ -53,12 +53,34 @@ class Pico {
 
 	function parse_content($content)
 	{
+		$content = $this->parse_snippets($content);
 		$content = str_replace('%base_url%', $this->base_url(), $content);
 		$content = Markdown($content);
 
 		return $content;
 	}
+	function parse_snippets($content) 
+	{
+		$pattern  = '|\{\{ snippet (.+)\}\}|U'; // match things between '{{ snippet ' and '}}'
+		preg_match_all($pattern, $content, $matches);
+		$matchCount = isset($matches[0]) ? count($matches[0]) : 0;
+		if ($matchCount) {
+			foreach (range(0, $matchCount -1) as $index) {
+				$matched_str = $matches[0][$index];
+				$snippet = strtolower(trim($matches[1][$index]));
+				
+				// load the replacement text from file
+				$file = CONTENT_DIR . '/snippets/' . $snippet . '.txt';
+				$replacement = file_exists($file) ? file_get_contents($file) : '';
+							
+				// replace the {{ snippet snippet-name }} string with the loaded text
+				$content = str_replace($matched_str, $replacement, $content);				
+			}
+		}
 
+		return $content;
+	}
+	
 	function read_file_meta($content)
 	{
 		$headers = array(