Wikulator.php
changeset 0 1783be241488
child 1 616d046e3bd9
equal deleted inserted replaced
-1:000000000000 0:1783be241488
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: Mediafier
       
     4 Plugin URI: http://enanocms.org/Mediafier
       
     5 Description: Several parser extensions that provide MediaWiki-like support for references, search highlighting, and a table of contents to Enano
       
     6 Author: Dan Fuhry
       
     7 Version: 0.1 beta 1
       
     8 Author URI: http://enanocms.org/
       
     9 */
       
    10 
       
    11 /*
       
    12  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
    13  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    14  *
       
    15  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    17  */
       
    18 
       
    19 $plugins->attachHook('render_wikiformat_post', 'mediafy($result);');
       
    20 $plugins->attachHook('compile_template', 'mediafier_add_headers();');
       
    21 $plugins->attachHook('html_attribute_whitelist', '$whitelist["ref"] = array(); $whitelist["references"] = array("/");');
       
    22 
       
    23 function mediafy(&$text)
       
    24 {
       
    25   global $db, $session, $paths, $template, $plugins; // Common objects
       
    26   mediafy_highlight_search_words($text);
       
    27   mediafy_process_references($text);
       
    28 }
       
    29 
       
    30 function mediafier_add_headers()
       
    31 {
       
    32   global $db, $session, $paths, $template, $plugins; // Common objects
       
    33   $template->add_header("<style type=\"text/css\">
       
    34                         .highlight  { background-color: #FFFFC0; font-weight: bold; }
       
    35                         .refbak     { background-color: #E0E0FF; font-weight: bold; }
       
    36                         .references { font-size: smaller; }
       
    37                       </style>");
       
    38   $ref_script = <<<EOF
       
    39       <enano:no-opt>
       
    40       <script type="text/javascript">
       
    41       // <![CDATA[
       
    42         function refsOff()
       
    43         {
       
    44           var divs = getElementsByClassName(document, '*', 'refbottom');
       
    45           for ( var i in divs )
       
    46           {
       
    47             $(divs[i]).rmClass('refbak');
       
    48           }
       
    49           divs = getElementsByClassName(document, '*', 'reftop');
       
    50           for ( var i in divs )
       
    51           {
       
    52             $(divs[i]).rmClass('refbak');
       
    53           }
       
    54         }
       
    55         function refToBottom(id)
       
    56         {
       
    57           refsOff();
       
    58           $('ref_'+id+'_b').addClass('refbak');
       
    59         }
       
    60         function refToTop(id)
       
    61         {
       
    62           refsOff();
       
    63           $('cite_'+id).addClass('refbak');
       
    64         }
       
    65         // ]]>
       
    66       </script>
       
    67       </enano:no-opt>
       
    68 
       
    69 EOF;
       
    70   $template->add_header($ref_script);
       
    71 }
       
    72 
       
    73 function mediafy_highlight_search_words(&$result)
       
    74 {
       
    75   global $db, $session, $paths, $template, $plugins; // Common objects
       
    76   
       
    77   if ( !isset($_SERVER['HTTP_REFERER']) && !isset($_GET['highlight']) )
       
    78     return false;
       
    79   
       
    80   $referer = ( isset($_GET['highlight']) ) ? $_SERVER['REQUEST_URI'] : $_SERVER['HTTP_REFERER'];
       
    81   $term = ( isset($_GET['highlight']) ) ? 'highlight' : 'q';
       
    82   
       
    83   preg_match('/(\?|&)'.$term.'=(.+?)(&|$)/', $referer, $match);
       
    84   if ( !isset($match[2]) )
       
    85   {
       
    86     return false;
       
    87   }
       
    88   
       
    89   $words = $match[2];
       
    90   $words = urldecode($words);
       
    91   if ( $term == 'q' )
       
    92   {
       
    93     // it's from a search query - extract terms
       
    94     require_once(ENANO_ROOT . '/includes/search.php');
       
    95     $words = parse_search_query($words, $warnings);
       
    96     $words = array_merge($words['any'], $words['req']);
       
    97   }
       
    98   else
       
    99   {
       
   100     $words = explode(' ', $words);
       
   101   }
       
   102   
       
   103   // strip HTML out of the rendered text
       
   104   $rand_seed = $session->dss_rand();
       
   105   preg_match_all('/<.+?>/', $result, $html_matches);
       
   106   $i = 0;
       
   107   foreach ( $html_matches[0] as $match )
       
   108   {
       
   109     $i++;
       
   110     $result = str_replace($match, "{HIGHLIGHT_PLACEHOLDER:$i:$rand_seed}", $result);
       
   111   }
       
   112   
       
   113   // highlight matches
       
   114   foreach ( $words as $word )
       
   115   {
       
   116     $result = preg_replace('/([\W]|^)(' . preg_quote($word) . ')([\W])/i', "\\1<span class=\"highlight\">\\2</span>\\3", $result);
       
   117   }
       
   118   
       
   119   // restore HTML
       
   120   $i = 0;
       
   121   foreach ( $html_matches[0] as $match )
       
   122   {
       
   123     $i++;
       
   124     $result = str_replace("{HIGHLIGHT_PLACEHOLDER:$i:$rand_seed}", $match, $result);
       
   125   }
       
   126   
       
   127   // add "remove highlighting" link
       
   128   $result = '<div style="float: right; text-align: right;"><a href="' . makeUrl($paths->page) . '" onclick="ajaxReset(); return false;">Turn off <span class="highlight">highlighting</span></a></div>' .
       
   129             $result;
       
   130   
       
   131 }
       
   132 
       
   133 function mediafy_process_references(&$text)
       
   134 {
       
   135   global $db, $session, $paths, $template, $plugins; // Common objects
       
   136   
       
   137   // Is there a <references /> in the wikitext? If not, just return out to avoid empty processing
       
   138   if ( !preg_match('#<references(([ ]*)/)?>#', $text) )
       
   139   {
       
   140     //die('no match');
       
   141     return false;
       
   142   }
       
   143   
       
   144   // Retrieve references
       
   145   preg_match_all('#<ref>(.+?)</ref>#s', $text, $matches);
       
   146   
       
   147   // Init counter
       
   148   $i = 0;
       
   149   
       
   150   // Init refs array
       
   151   $refs = array();
       
   152   
       
   153   // main parser loop
       
   154   foreach ( $matches[0] as $j => $match )
       
   155   {
       
   156     $i++;
       
   157     $inner =& $matches[1][$j];
       
   158     $refs[$i] = $inner;
       
   159     $reflink = '<sup><a class="reftop" id="cite_' . $i . '" name="cite_' . $i . '" href="#ref_' . $i . '" onclick="refToBottom(\'' . $i . '\');">[' . $i . ']</a></sup>';
       
   160     $text = str_replace($match, $reflink, $text);
       
   161   }
       
   162   
       
   163   // compile refs div
       
   164   $refsdiv = '<div class="references">';
       
   165   $refsdiv .= '<table border="0" width="100%"><tr><td valign="top">';
       
   166   $count = ( count($refs) >= 20 ) ? floor(count($refs) / 2) : 99;
       
   167   foreach ( $refs as $i => $ref )
       
   168   {
       
   169     $reflink = '<span id="ref_' . $i . '" name="ref_' . $i . '"><sup><b><a onclick="refToTop(\'' . $i . '\');" href="#cite_' . $i . '">^</a></b></sup> </span>';
       
   170     $refsdiv .= "<div class=\"refbottom\" id=\"ref_{$i}_b\">$reflink $i. $ref</div>";
       
   171     if ( $i == $count )
       
   172       $refsdiv .= '</td><td valign="top">';
       
   173   }
       
   174   $refsdiv .= '</td></tr></table>';
       
   175   $refsdiv .= '</div>';
       
   176   
       
   177   preg_match('#<references(([ ]*)/)?>#', $text, $match);
       
   178   $text = str_replace_once($match[0], $refsdiv, $text);
       
   179 }
       
   180 
       
   181 ?>