Wikulator.php
changeset 2 88265c8715d0
parent 1 616d046e3bd9
child 3 98ccd8815a02
equal deleted inserted replaced
1:616d046e3bd9 2:88265c8715d0
    14  *
    14  *
    15  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
    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.
    16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    17  */
    17  */
    18 
    18 
    19 $plugins->attachHook('render_wikiformat_pre', 'mediafier_draw_toc($text);');
    19 $plugins->attachHook('render_wikiformat_posttemplates', 'mediafier_draw_toc($text);');
    20 $plugins->attachHook('render_wikiformat_post', 'mediafy($result);');
    20 $plugins->attachHook('render_wikiformat_post', 'mediafy($result);');
    21 $plugins->attachHook('compile_template', 'mediafier_add_headers();');
    21 $plugins->attachHook('compile_template', 'mediafier_add_headers();');
    22 $plugins->attachHook('html_attribute_whitelist', '$whitelist["ref"] = array(); $whitelist["references"] = array("/");');
    22 $plugins->attachHook('html_attribute_whitelist', '$whitelist["ref"] = array(); $whitelist["references"] = array("/");');
    23 
    23 
    24 function mediafy(&$text)
    24 function mediafy(&$text)
    64         $toc .= '</dl>';
    64         $toc .= '</dl>';
    65         $levels--;
    65         $levels--;
    66         unset($treenum[count($treenum)-1]);
    66         unset($treenum[count($treenum)-1]);
    67       }
    67       }
    68     }
    68     }
    69     $treenum[count($treenum)-1]++;
    69     if ( isset($treenum[count($treenum)-1]) )
       
    70       $treenum[count($treenum)-1]++;
    70     if ( $i > 0 )
    71     if ( $i > 0 )
    71       $toc .= '</dd>';
    72       $toc .= '</dd>';
    72     $toc .= '<dd><a href="#toc' . ($i + 1) . '">' . implode('.', $treenum) . ' ' . htmlspecialchars($matches[2][$i]) . '</a>';
    73     $toc .= '<dd><a href="#toc' . ($i + 1) . '">' . implode('.', $treenum) . ' ' . htmlspecialchars($matches[2][$i]) . '</a>';
    73     $prev = $head;
    74     $prev = $head;
    74   }
    75   }
    79   }
    80   }
    80   $toc_body = "<nowiki><div class=\"toc mdg-comment\">
    81   $toc_body = "<nowiki><div class=\"toc mdg-comment\">
    81                 <dl><dd><b>Contents</b> <small>[<a href=\"#\" onclick=\"collapseTOC(this); return false;\">hide</a>]</small></dd></dl>
    82                 <dl><dd><b>Contents</b> <small>[<a href=\"#\" onclick=\"collapseTOC(this); return false;\">hide</a>]</small></dd></dl>
    82                 <div>$toc</div>
    83                 <div>$toc</div>
    83               </div></nowiki>";
    84               </div></nowiki>";
    84               
    85     
    85   if ( strstr($text, '__TOC__') )
    86   if ( strstr($text, '__TOC__') )
       
    87   {
    86     $text = str_replace_once('__TOC__', $toc_body, $text);
    88     $text = str_replace_once('__TOC__', $toc_body, $text);
    87   else if ( ($text = preg_replace('/^=/', "$toc_body\n\n=", $text)) === $text )
    89   }
       
    90   else if ( $text === ($rtext = preg_replace('/^=/', "$toc_body\n\n=", $text)) )
       
    91   {
    88     $text = str_replace_once("\n=", "\n$toc_body\n=", $text);
    92     $text = str_replace_once("\n=", "\n$toc_body\n=", $text);
       
    93   }
       
    94   else
       
    95   {
       
    96     $text = $rtext;
       
    97     unset($rtext);
       
    98   }
    89 }
    99 }
    90 
   100 
    91 function mediafier_add_headers()
   101 function mediafier_add_headers()
    92 {
   102 {
    93   global $db, $session, $paths, $template, $plugins; // Common objects
   103   global $db, $session, $paths, $template, $plugins; // Common objects
   200   }
   210   }
   201   
   211   
   202   // highlight matches
   212   // highlight matches
   203   foreach ( $words as $word )
   213   foreach ( $words as $word )
   204   {
   214   {
   205     $result = preg_replace('/([\W]|^)(' . preg_quote($word) . ')([\W])/i', "\\1<span class=\"highlight\">\\2</span>\\3", $result);
   215     $result = preg_replace('/([\W]|^)(' . str_replace('/', '\/', preg_quote($word)) . ')([\W])/i', "\\1<span class=\"highlight\">\\2</span>\\3", $result);
   206   }
   216   }
   207   
   217   
   208   // restore HTML
   218   // restore HTML
   209   $i = 0;
   219   $i = 0;
   210   foreach ( $html_matches[0] as $match )
   220   foreach ( $html_matches[0] as $match )
   254   $refsdiv .= '<table border="0" width="100%"><tr><td valign="top">';
   264   $refsdiv .= '<table border="0" width="100%"><tr><td valign="top">';
   255   $count = ( count($refs) >= 20 ) ? floor(count($refs) / 2) : 99;
   265   $count = ( count($refs) >= 20 ) ? floor(count($refs) / 2) : 99;
   256   foreach ( $refs as $i => $ref )
   266   foreach ( $refs as $i => $ref )
   257   {
   267   {
   258     $reflink = '<span id="ref_' . $i . '" name="ref_' . $i . '"><sup><b><a onclick="refToTop(\'' . $i . '\');" href="#cite_' . $i . '">^</a></b></sup> </span>';
   268     $reflink = '<span id="ref_' . $i . '" name="ref_' . $i . '"><sup><b><a onclick="refToTop(\'' . $i . '\');" href="#cite_' . $i . '">^</a></b></sup> </span>';
       
   269     $ref = trim($ref);
   259     $refsdiv .= "<div class=\"refbottom\" id=\"ref_{$i}_b\">$reflink $i. $ref</div>";
   270     $refsdiv .= "<div class=\"refbottom\" id=\"ref_{$i}_b\">$reflink $i. $ref</div>";
   260     if ( $i == $count )
   271     if ( $i == $count )
   261       $refsdiv .= '</td><td valign="top">';
   272       $refsdiv .= '</td><td valign="top">';
   262   }
   273   }
   263   $refsdiv .= '</td></tr></table>';
   274   $refsdiv .= '</td></tr></table>';