Sfoglia il codice sorgente

Add content filter to get the parsed contents of a page (lazy loading)

Daniel Rudolf 9 anni fa
parent
commit
cd7cd374bb

+ 5 - 2
content-sample/index.md

@@ -179,7 +179,7 @@ to use in your theme. Please note that paths (e.g. `{{ base_dir }}`) and URLs
 * `{{ content }}` - The content of the current page
 * `{{ content }}` - The content of the current page
                     (after it has been processed through Markdown)
                     (after it has been processed through Markdown)
 * `{{ pages }}` - A collection of all the content pages in your site
 * `{{ pages }}` - A collection of all the content pages in your site
-    * `{{ page.id }}` - The relative path to the content file
+    * `{{ page.id }}` - The relative path to the content file (unique ID)
     * `{{ page.url }}` - The URL to the page
     * `{{ page.url }}` - The URL to the page
     * `{{ page.title }}` - The title of the page (YAML header)
     * `{{ page.title }}` - The title of the page (YAML header)
     * `{{ page.description }}` - The description of the page (YAML header)
     * `{{ page.description }}` - The description of the page (YAML header)
@@ -187,7 +187,10 @@ to use in your theme. Please note that paths (e.g. `{{ base_dir }}`) and URLs
     * `{{ page.time }}` - The timestamp derived from the `Date` header
     * `{{ page.time }}` - The timestamp derived from the `Date` header
     * `{{ page.date }}` - The date of the page (YAML header)
     * `{{ page.date }}` - The date of the page (YAML header)
     * `{{ page.date_formatted }}` - The formatted date of the page
     * `{{ page.date_formatted }}` - The formatted date of the page
-    * `{{ page.raw_content }}` - The raw, not yet parsed contents of the page
+    * `{{ page.raw_content }}` - The raw, not yet parsed contents of the page;
+                                 use Twigs `content` filter to get the parsed
+                                 contents of a page by passing its unique ID
+                                 (e.g. `{{ "sub/page"|content }}`)
     * `{{ page.meta }}`- The meta values of the page
     * `{{ page.meta }}`- The meta values of the page
 * `{{ prev_page }}` - The data of the previous page (relative to `current_page`)
 * `{{ prev_page }}` - The data of the previous page (relative to `current_page`)
 * `{{ current_page }}` - The data of the current page
 * `{{ current_page }}` - The data of the current page

+ 22 - 4
lib/Pico.php

@@ -307,7 +307,7 @@ class Pico
         // parse file content
         // parse file content
         $this->triggerEvent('onContentParsing', array(&$this->rawContent));
         $this->triggerEvent('onContentParsing', array(&$this->rawContent));
 
 
-        $this->content = $this->prepareFileContent($this->rawContent);
+        $this->content = $this->prepareFileContent($this->rawContent, $this->meta);
         $this->triggerEvent('onContentPrepared', array(&$this->content));
         $this->triggerEvent('onContentPrepared', array(&$this->content));
 
 
         $this->content = $this->parseFileContent($this->content);
         $this->content = $this->parseFileContent($this->content);
@@ -786,9 +786,10 @@ class Pico
      * @see    Pico::parseFileContent()
      * @see    Pico::parseFileContent()
      * @see    Pico::getFileContent()
      * @see    Pico::getFileContent()
      * @param  string $rawContent raw contents of a page
      * @param  string $rawContent raw contents of a page
+     * @param  array  $meta       meta data to use for %meta.*% replacement
      * @return string             contents prepared for parsing
      * @return string             contents prepared for parsing
      */
      */
-    public function prepareFileContent($rawContent)
+    public function prepareFileContent($rawContent, array $meta)
     {
     {
         // remove meta header
         // remove meta header
         $metaHeaderPattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n"
         $metaHeaderPattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n"
@@ -814,9 +815,9 @@ class Pico
         $content = str_replace('%theme_url%', $themeUrl, $content);
         $content = str_replace('%theme_url%', $themeUrl, $content);
 
 
         // replace %meta.*%
         // replace %meta.*%
-        if (!empty($this->meta)) {
+        if (!empty($meta)) {
             $metaKeys = $metaValues = array();
             $metaKeys = $metaValues = array();
-            foreach ($this->meta as $metaKey => $metaValue) {
+            foreach ($meta as $metaKey => $metaValue) {
                 if (is_scalar($metaValue) || ($metaValue === null)) {
                 if (is_scalar($metaValue) || ($metaValue === null)) {
                     $metaKeys[] = '%meta.' . $metaKey . '%';
                     $metaKeys[] = '%meta.' . $metaKey . '%';
                     $metaValues[] = strval($metaValue);
                     $metaValues[] = strval($metaValue);
@@ -1073,7 +1074,24 @@ class Pico
         $twigLoader = new Twig_Loader_Filesystem($this->getThemesDir() . $this->getConfig('theme'));
         $twigLoader = new Twig_Loader_Filesystem($this->getThemesDir() . $this->getConfig('theme'));
         $this->twig = new Twig_Environment($twigLoader, $this->getConfig('twig_config'));
         $this->twig = new Twig_Environment($twigLoader, $this->getConfig('twig_config'));
         $this->twig->addExtension(new Twig_Extension_Debug());
         $this->twig->addExtension(new Twig_Extension_Debug());
+
+        // register link filter
         $this->twig->addFilter(new Twig_SimpleFilter('link', array($this, 'getPageUrl')));
         $this->twig->addFilter(new Twig_SimpleFilter('link', array($this, 'getPageUrl')));
+
+        // register content filter
+        $pico = $this;
+        $pages = &$this->pages;
+        $this->twig->addFilter(new Twig_SimpleFilter('content', function ($pageId) use ($pico, &$pages) {
+            if (isset($pages[$pageId])) {
+                $pageData = &$pages[$pageId];
+                if (!isset($pageData['content'])) {
+                    $pageData['content'] = $pico->prepareFileContent($pageData['raw_content'], $pageData['meta']);
+                    $pageData['content'] = $pico->parseFileContent($pageData['content']);
+                }
+                return $pageData['content'];
+            }
+            return '';
+        }));
     }
     }
 
 
     /**
     /**

+ 1 - 1
plugins/01-PicoParsePagesContent.php

@@ -33,7 +33,7 @@ class PicoParsePagesContent extends AbstractPicoPlugin
     public function onSinglePageLoaded(&$pageData)
     public function onSinglePageLoaded(&$pageData)
     {
     {
         if (!isset($pageData['content'])) {
         if (!isset($pageData['content'])) {
-            $pageData['content'] = $this->prepareFileContent($pageData['raw_content']);
+            $pageData['content'] = $this->prepareFileContent($pageData['raw_content'], $pageData['meta']);
             $pageData['content'] = $this->parseFileContent($pageData['content']);
             $pageData['content'] = $this->parseFileContent($pageData['content']);
         }
         }
     }
     }

+ 1 - 1
plugins/DummyPlugin.php

@@ -194,7 +194,7 @@ class DummyPlugin extends AbstractPicoPlugin
      * @param  string &$content prepared file contents for parsing
      * @param  string &$content prepared file contents for parsing
      * @return void
      * @return void
      */
      */
-    public function prepareFileContent(&$content)
+    public function onContentPrepared(&$content)
     {
     {
         // your code
         // your code
     }
     }