b2evolution PHP Cross Reference Blogging Systems

Source: /plugins/_wacko.plugin.php - 215 lines - 5430 bytes - Summary - Text - Print

Description: This file implements the Wacko plugin for b2evolution Wacko style formatting

   1  <?php
   2  /**
   3   * This file implements the Wacko plugin for b2evolution
   4   *
   5   * Wacko style formatting
   6   *
   7   * b2evolution - {@link http://b2evolution.net/}
   8   * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
   9   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
  10   *
  11   * @package plugins
  12   * @ignore
  13   */
  14  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  15  
  16  
  17  /**
  18   * @package plugins
  19   */
  20  class wacko_plugin extends Plugin
  21  {
  22      var $code = 'b2evWcko';
  23      var $name = 'Wacko formatting';
  24      var $priority = 30;
  25      var $version = '5.0.0';
  26      var $group = 'rendering';
  27      var $short_desc;
  28      var $long_desc;
  29      var $help_url = 'http://b2evolution.net/man/technical-reference/renderer-plugins/wacko-plugin';
  30      var $number_of_installs = 1;
  31  
  32      /**
  33       * GreyMatter formatting search array
  34       *
  35       * @access private
  36       */
  37      var $search = array(
  38              '#( ^ | \s ) ====== (.+?) ====== #x',
  39              '#( ^ | \s ) ===== (.+?) ===== #x',
  40              '#( ^ | \s ) ==== (.+?) ==== #x',
  41              '#( ^ | \s ) === (.+?) === #x',
  42              '#( ^ | \s ) == (.+?) == #x',
  43              '#^ \s* --- \s* $#xm',    // multiline start/stop checking
  44              '/ %%%
  45                  ( \s*? \n )?                 # Eat optional blank line after %%%
  46                  (.+?)
  47                  ( \n \s*? )?                 # Eat optional blank line before %%%
  48                  %%%
  49              /sxe'        // %%%escaped codeblock%%%
  50          );
  51  
  52      /**
  53       * HTML replace array
  54       *
  55       * @access private
  56       */
  57      var $replace = array(
  58              '$1<h6>$2</h6>',
  59              '$1<h5>$2</h5>',
  60              '$1<h4>$2</h4>',
  61              '$1<h3>$2</h3>',
  62              '$1<h2>$2</h2>',
  63              '<hr />',
  64              '\'<div class="codeblock"><pre><code>\'.
  65              htmlspecialchars(stripslashes(\'$2\'),ENT_NOQUOTES).
  66              \'</code></pre></div>\''
  67          );
  68  
  69      /**
  70       * Init
  71       */
  72  	function PluginInit( & $params )
  73      {
  74          $this->short_desc = T_('Wacko style formatting');
  75          $this->long_desc = T_('Accepted formats:<br />
  76              == h2 ==<br />
  77              === h3 ===<br />
  78              ==== h4 ====<br />
  79              ===== h5 =====<br />
  80              ====== h6 ======<br />
  81              --- (horinzontal rule)<br />
  82              %%%codeblock%%%<br />');
  83      }
  84  
  85  
  86      /**
  87       * Define here default collection/blog settings that are to be made available in the backoffice.
  88       *
  89       * @param array Associative array of parameters.
  90       * @return array See {@link Plugin::get_coll_setting_definitions()}.
  91       */
  92  	function get_coll_setting_definitions( & $params )
  93      {
  94          $default_params = array_merge( $params, array( 'default_post_rendering' => 'opt-in' ) );
  95          return parent::get_coll_setting_definitions( $default_params );
  96      }
  97  
  98  
  99      /**
 100       * Perform rendering
 101       *
 102       * @param array Associative array of parameters
 103       *   'data': the data (by reference). You probably want to modify this.
 104       *   'format': see {@link format_to_output()}. Only 'htmlbody' and 'entityencoded' will arrive here.
 105       * @return boolean true if we can render something for the required output format
 106       */
 107  	function RenderItemAsHtml( & $params )
 108      {
 109          $content = & $params['data'];
 110  
 111          $content = replace_content_outcode( $this->search, $this->replace, $content );
 112  
 113          // Find bullet lists
 114          if( stristr( $content, '<code' ) !== false || stristr( $content, '<pre' ) !== false )
 115          {    // Call replace_content() on everything outside code/pre:
 116              $content = callback_on_non_matching_blocks( $content,
 117                  '~<(code|pre)[^>]*>.*?</\1>~is',
 118                  array( $this, 'find_bullet_lists' ) );
 119          }
 120          else
 121          {    // No code/pre blocks, replace on the whole thing
 122              $content = $this->find_bullet_lists( $content );
 123          }
 124  
 125          return true;
 126      }
 127  
 128  
 129      /**
 130       * Find bullet lists
 131       *
 132       * @param string Content
 133       * @return string Content
 134       */
 135  	function find_bullet_lists( $content )
 136      {
 137          $lines = explode( "\n", $content );
 138          $lines_count = count( $lines );
 139          $lists = array();
 140          $current_depth = 0;
 141          $content = '';
 142          foreach( $lines as $l => $line )
 143          {
 144              if( ! preg_match( '#^ /s $#xm', $line ) )
 145              {     // If not blank line
 146                  $matches = array();
 147  
 148                  if( preg_match( '#^((  )+)\*(.*)$#m', $line, $matches ) )
 149                  {    // We have a list item
 150                      $req_depth = strlen( $matches[1] ) / 2;
 151                      while( $current_depth < $req_depth )
 152                      {    // We must indent
 153                          $content .= "<ul>\n";
 154                          array_push( $lists, 'ul' );
 155                          $current_depth++;
 156                      }
 157  
 158                      while( $current_depth > $req_depth )
 159                      {    // We must close lists
 160                          $content .= '</'.array_pop( $lists ).">\n";
 161                          $current_depth--;
 162                      }
 163  
 164                      $content .= $matches[1].'<li>'.$matches[3]."</li>\n";
 165                      continue;
 166                  }
 167  
 168                  if( preg_match( '#^((  )+)([0-9]+)(.*)$#m', $line, $matches ) )
 169                  {    // We have an ordered list item
 170                      $req_depth = strlen( $matches[1] ) / 2;
 171                      while( $current_depth < $req_depth )
 172                      {    // We must indent
 173                          $content .= '<ol start="'.$matches[3].'">'."\n";
 174                          array_push( $lists, 'ol' );
 175                          $current_depth++;
 176                      }
 177  
 178                      while( $current_depth > $req_depth )
 179                      {    // We must close lists
 180                          $content .= '</'.array_pop( $lists ).">\n";
 181                          $current_depth--;
 182                      }
 183  
 184                      $content .= $matches[1].'<li>'.$matches[4]."</li>\n";
 185                      continue;
 186                  }
 187  
 188                  // Normal line.
 189  
 190                  if( $current_depth )
 191                  { // We must go back to 0
 192                      $content .= '</'.implode( ">\n</", $lists ).">\n";
 193                      $lists = array();
 194                      $current_depth = 0;
 195                  }
 196  
 197                  $content .= $line;
 198                  if( $l < $lines_count - 1 )
 199                  {    // Don't append a newline at the end, because it will create an unnecessary newline that didn't exist in source content
 200                      $content .= "\n";
 201                  }
 202  
 203              }
 204          }
 205  
 206          if( $current_depth )
 207          { // We must go back to 0
 208              $content .= '</'.implode( ">\n</", $lists ).">\n";
 209          }
 210  
 211          return $content;
 212      }
 213  }
 214  
 215  ?>

title

Description

title

Description

title

Description

title

title

Body