[dokuwiki] [PATCH] Explicit "read more" marker for include's firstsec option

  • From: Stephen Warren <swarren@xxxxxxxxxxxxx>
  • To: dokuwiki@xxxxxxxxxxxxx
  • Date: Tue, 02 Mar 2010 22:52:57 -0700

For my blog, I intend to use the firstsec option to limit the main blog
page to small excerpts of the posts. I think that I won't always want
this excerpt to be exactly the first section of the article as define by
headlines.

So, I implemented support for writing a ~~READMORE~~ marker in the
article, which if present, will mark the end of the excerpt posted to
the main blog page, and if missing, include will fall back to the old
"first section" logic.

Does this seem reasonable? In particular, I could see some people not
liking this being enabled under the firstsec option, but want it to
require some other option. However, I think the automatic fallback makes
it nice and easy to optionally use.
diff -urN dokuwiki-orig/lib/plugins/include/helper.php 
dokuwiki-readmore/lib/plugins/include/helper.php
--- dokuwiki-orig/lib/plugins/include/helper.php        2010-03-02 
20:18:03.000000000 -0700
+++ dokuwiki-readmore/lib/plugins/include/helper.php    2010-03-02 
22:46:27.000000000 -0700
@@ -314,6 +314,7 @@
      * @author Michael Klier <chi@xxxxxxxxxxx>
      */
     function _convert_instructions(&$ins, $lvl, $page, $sect, $flags) {
+        global $ID;
 
         // filter instructions if needed
         if(!empty($sect)) {
@@ -321,9 +322,11 @@
         }
 
         if($flags['firstsec']) {
-            $this->_get_firstsec($ins, $page);  // only first section 
+            if(!$this->_get_pre_readmore($ins, $page)) { // only up to 
readmore marker
+                $this->_get_firstsec($ins, $page);  // only first section
+            }
         }
-        
+
         $ns  = getNS($page);
         $num = count($ins);
 
@@ -531,6 +534,18 @@
     } 
 
     /**
+     * Add a "read more" marker to a set of instructions
+     * 
+     * @author Michael Klier <chi@xxxxxxxxxxx>
+     */
+    function _add_readmore(&$ins, $page) {
+        $ins[] = array('p_open', array());
+        $ins[] = array('internallink',array($page, 
$this->getLang('readmore')));
+        $ins[] = array('p_close', array());
+        $ins[] = array('section_close');
+    }
+
+    /**
      * Only display the first section of a page and a readmore link
      *
      * @author Michael Klier <chi@xxxxxxxxxxx>
@@ -544,16 +559,53 @@
             }
             if(($first_sect) && ($ins[$i][0] == 'section_open')) {
                 $ins = array_slice($ins, 0, $first_sect);
-                $ins[] = array('p_open', array());
-                $ins[] = array('internallink',array($page, 
$this->getLang('readmore')));
-                $ins[] = array('p_close', array());
-                $ins[] = array('section_close');
+                $this->_add_readmore($ins, $page);
                 return;
             }
         }
     }
 
     /**
+     * Only display the portion of a page up to a readmore tag
+     *
+     * @author Stephen Warren <swarren@xxxxxxxxxxxxx>
+     */
+    function _get_pre_readmore(&$ins, $page) {
+        $num = count($ins);
+        for($i=0; $i<$num; $i++) {
+            if(($ins[$i][0] == 'plugin') && ($ins[$i][1][0] == 
'include_readmore')) {
+                $ins = array_slice($ins, 0, $i);
+                $num = count($ins);
+                $section_level = 0;
+                $p_level = 0;
+                for($i=0; $i<$num; $i++) {
+                    if($ins[$i][0] == 'section_open') {
+                        $section_level++;
+                    }
+                    if($ins[$i][0] == 'section_close') {
+                        $section_level--;
+                    }
+                    if($ins[$i][0] == 'p_open') {
+                        $p_level++;
+                    }
+                    if($ins[$i][0] == 'p_close') {
+                        $p_level--;
+                    }
+                }
+                for($i=0; $i<$p_level; $i++) {
+                    $ins[] = array('p_close');
+                }
+                for($i=0; $i<$section_level-1; $i++) {
+                    $ins[] = array('section_close');
+                }
+                $this->_add_readmore($ins, $page);
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
      * Makes user or date dependent includes possible
      */
     function _apply_macro($id) {
Binary files dokuwiki-orig/lib/plugins/include/.helper.php.swp and 
dokuwiki-readmore/lib/plugins/include/.helper.php.swp differ
diff -urN dokuwiki-orig/lib/plugins/include/syntax/readmore.php 
dokuwiki-readmore/lib/plugins/include/syntax/readmore.php
--- dokuwiki-orig/lib/plugins/include/syntax/readmore.php       1969-12-31 
17:00:00.000000000 -0700
+++ dokuwiki-readmore/lib/plugins/include/syntax/readmore.php   2010-03-02 
22:44:44.000000000 -0700
@@ -0,0 +1,48 @@
+<?php
+/**
+ * Include plugin (editbtn header component)
+ *
+ * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
+ * @author  Stephen Warren <swarren@xxxxxxxxxxxxx>
+ */
+
+if (!defined('DOKU_INC'))
+    define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
+if (!defined('DOKU_PLUGIN'))
+    define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
+require_once (DOKU_PLUGIN . 'syntax.php');
+
+class syntax_plugin_include_readmore extends DokuWiki_Syntax_Plugin {
+
+    function getInfo() {
+        return array (
+            'author' => 'Stephen Warren',
+            'email' => 'swarren@xxxxxxxxxxxxx',
+            'date' => @file_get_contents(DOKU_PLUGIN . 'blog/VERSION'),
+            'name' => 'Include Plugin (readmore component)',
+            'desc' => 'Provides a readmore marker',
+            'url' => 'http://dokuwiki.org/plugin:include',
+        );
+    }
+
+    function getType() {
+        return 'formatting';
+    }
+    
+    function getSort() {
+        return 50;
+    }
+
+    function connectTo($mode) {
+        
$this->Lexer->addSpecialPattern('~~READMORE~~',$mode,'plugin_include_readmore');
+    }
+
+    function handle($match, $state, $pos, &$handler) {
+       return '';
+    }
+
+    function render($mode, &$renderer, $data) {
+       return true;
+    }
+}
+// vim:ts=4:sw=4:et:enc=utf-8:

Other related posts: