Wikulator.php
author Dan
Wed, 24 Dec 2008 11:04:18 -0500
changeset 1 616d046e3bd9
parent 0 1783be241488
child 2 88265c8715d0
permissions -rwxr-xr-x
Table of contents support! Woooo!!
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     1
<?php
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     2
/*
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     3
Plugin Name: Mediafier
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     4
Plugin URI: http://enanocms.org/Mediafier
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     5
Description: Several parser extensions that provide MediaWiki-like support for references, search highlighting, and a table of contents to Enano
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     6
Author: Dan Fuhry
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     7
Version: 0.1 beta 1
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     8
Author URI: http://enanocms.org/
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     9
*/
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    10
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    11
/*
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    12
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    13
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    14
 *
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    15
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    16
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    17
 */
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    18
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    19
$plugins->attachHook('render_wikiformat_pre', 'mediafier_draw_toc($text);');
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    20
$plugins->attachHook('render_wikiformat_post', 'mediafy($result);');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    21
$plugins->attachHook('compile_template', 'mediafier_add_headers();');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    22
$plugins->attachHook('html_attribute_whitelist', '$whitelist["ref"] = array(); $whitelist["references"] = array("/");');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    23
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    24
function mediafy(&$text)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    25
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    26
  global $db, $session, $paths, $template, $plugins; // Common objects
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    27
  mediafy_highlight_search_words($text);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    28
  mediafy_process_references($text);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    29
}
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    30
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    31
function mediafier_draw_toc(&$text)
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    32
{
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    33
  if ( strstr($text, '__NOTOC__') )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    34
    return true;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    35
  
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    36
  if ( !preg_match_all('/^\s*([=]{1,6})([^\r\n]+)\\1\s*$/m', $text, $matches) )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    37
    return true;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    38
  
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    39
  $heading_map = array();
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    40
  foreach ( $matches[1] as $heading )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    41
  {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    42
    $heading_map[] = strlen($heading);
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    43
  }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    44
  
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    45
  if ( count($heading_map) < 4 && !strstr($text, '__TOC__') )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    46
    return true;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    47
  
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    48
  $prev = 0;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    49
  $levels = 0;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    50
  $treenum = array();
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    51
  $toc = '';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    52
  foreach ( $heading_map as $i => $head )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    53
  {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    54
    if ( $head > $prev )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    55
    {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    56
      $treenum[] = 0;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    57
      $levels++;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    58
      $toc .= '<dl>';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    59
    }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    60
    else if ( $head < $prev )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    61
    {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    62
      if ( $levels > 1 )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    63
      {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    64
        $toc .= '</dl>';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    65
        $levels--;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    66
        unset($treenum[count($treenum)-1]);
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    67
      }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    68
    }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    69
    $treenum[count($treenum)-1]++;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    70
    if ( $i > 0 )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    71
      $toc .= '</dd>';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    72
    $toc .= '<dd><a href="#toc' . ($i + 1) . '">' . implode('.', $treenum) . ' ' . htmlspecialchars($matches[2][$i]) . '</a>';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    73
    $prev = $head;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    74
  }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    75
  while ( $levels > 0 )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    76
  {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    77
    $toc .= '</dd></dl>';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    78
    $levels--;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    79
  }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    80
  $toc_body = "<nowiki><div class=\"toc mdg-comment\">
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    81
                <dl><dd><b>Contents</b> <small>[<a href=\"#\" onclick=\"collapseTOC(this); return false;\">hide</a>]</small></dd></dl>
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    82
                <div>$toc</div>
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    83
              </div></nowiki>";
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    84
              
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    85
  if ( strstr($text, '__TOC__') )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    86
    $text = str_replace_once('__TOC__', $toc_body, $text);
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    87
  else if ( ($text = preg_replace('/^=/', "$toc_body\n\n=", $text)) === $text )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    88
    $text = str_replace_once("\n=", "\n$toc_body\n=", $text);
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    89
}
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    90
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    91
function mediafier_add_headers()
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    92
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    93
  global $db, $session, $paths, $template, $plugins; // Common objects
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    94
  $template->add_header("<style type=\"text/css\">
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    95
                        .highlight  { background-color: #FFFFC0; font-weight: bold; }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    96
                        .refbak     { background-color: #E0E0FF; font-weight: bold; }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    97
                        .references { font-size: smaller; }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    98
                      </style>");
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    99
  $ref_script = <<<EOF
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   100
      <enano:no-opt>
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   101
      <style type="text/css">
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   102
      div.toc {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   103
        display: table;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   104
        max-width: 70%;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   105
        padding: 0.7em 1.7em 0.7em 0.7em;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   106
        margin: 10px 0 0 0;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   107
      }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   108
      div.toc dl {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   109
        margin: 2px 0;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   110
      }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   111
      div.toc dd {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   112
        margin-left: 1em;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   113
      }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   114
      </style>
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   115
      <script type="text/javascript">
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   116
      // <![CDATA[
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   117
        function refsOff()
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   118
        {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   119
          var divs = getElementsByClassName(document, '*', 'refbottom');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   120
          for ( var i in divs )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   121
          {
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   122
            \$dynano(divs[i]).rmClass('refbak');
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   123
          }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   124
          divs = getElementsByClassName(document, '*', 'reftop');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   125
          for ( var i in divs )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   126
          {
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   127
            \$dynano(divs[i]).rmClass('refbak');
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   128
          }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   129
        }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   130
        function refToBottom(id)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   131
        {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   132
          refsOff();
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   133
          \$dynano('ref_'+id+'_b').addClass('refbak');
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   134
        }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   135
        function refToTop(id)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   136
        {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   137
          refsOff();
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   138
          \$dynano('cite_'+id).addClass('refbak');
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   139
        }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   140
        function collapseTOC(el)
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   141
        {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   142
          var toc_inner = el.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('div')[0];
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   143
          if ( toc_inner.style.display == 'none' )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   144
          {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   145
            el.innerHTML = 'hide';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   146
            toc_inner.style.display = 'block';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   147
          }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   148
          else
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   149
          {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   150
            el.innerHTML = 'show';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   151
            toc_inner.style.display = 'none';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   152
          }
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   153
        }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   154
        // ]]>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   155
      </script>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   156
      </enano:no-opt>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   157
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   158
EOF;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   159
  $template->add_header($ref_script);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   160
}
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   161
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   162
function mediafy_highlight_search_words(&$result)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   163
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   164
  global $db, $session, $paths, $template, $plugins; // Common objects
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   165
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   166
  if ( !isset($_SERVER['HTTP_REFERER']) && !isset($_GET['highlight']) )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   167
    return false;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   168
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   169
  $referer = ( isset($_GET['highlight']) ) ? $_SERVER['REQUEST_URI'] : $_SERVER['HTTP_REFERER'];
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   170
  $term = ( isset($_GET['highlight']) ) ? 'highlight' : 'q';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   171
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   172
  preg_match('/(\?|&)'.$term.'=(.+?)(&|$)/', $referer, $match);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   173
  if ( !isset($match[2]) )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   174
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   175
    return false;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   176
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   177
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   178
  $words = $match[2];
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   179
  $words = urldecode($words);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   180
  if ( $term == 'q' )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   181
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   182
    // it's from a search query - extract terms
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   183
    require_once(ENANO_ROOT . '/includes/search.php');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   184
    $words = parse_search_query($words, $warnings);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   185
    $words = array_merge($words['any'], $words['req']);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   186
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   187
  else
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   188
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   189
    $words = explode(' ', $words);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   190
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   191
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   192
  // strip HTML out of the rendered text
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   193
  $rand_seed = $session->dss_rand();
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   194
  preg_match_all('/<.+?>/', $result, $html_matches);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   195
  $i = 0;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   196
  foreach ( $html_matches[0] as $match )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   197
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   198
    $i++;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   199
    $result = str_replace($match, "{HIGHLIGHT_PLACEHOLDER:$i:$rand_seed}", $result);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   200
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   201
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   202
  // highlight matches
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   203
  foreach ( $words as $word )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   204
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   205
    $result = preg_replace('/([\W]|^)(' . preg_quote($word) . ')([\W])/i', "\\1<span class=\"highlight\">\\2</span>\\3", $result);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   206
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   207
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   208
  // restore HTML
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   209
  $i = 0;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   210
  foreach ( $html_matches[0] as $match )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   211
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   212
    $i++;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   213
    $result = str_replace("{HIGHLIGHT_PLACEHOLDER:$i:$rand_seed}", $match, $result);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   214
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   215
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   216
  // add "remove highlighting" link
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   217
  $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>' .
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   218
            $result;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   219
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   220
}
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   221
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   222
function mediafy_process_references(&$text)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   223
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   224
  global $db, $session, $paths, $template, $plugins; // Common objects
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   225
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   226
  // Is there a <references /> in the wikitext? If not, just return out to avoid empty processing
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   227
  if ( !preg_match('#<references(([ ]*)/)?>#', $text) )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   228
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   229
    //die('no match');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   230
    return false;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   231
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   232
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   233
  // Retrieve references
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   234
  preg_match_all('#<ref>(.+?)</ref>#s', $text, $matches);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   235
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   236
  // Init counter
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   237
  $i = 0;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   238
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   239
  // Init refs array
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   240
  $refs = array();
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   241
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   242
  // main parser loop
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   243
  foreach ( $matches[0] as $j => $match )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   244
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   245
    $i++;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   246
    $inner =& $matches[1][$j];
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   247
    $refs[$i] = $inner;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   248
    $reflink = '<sup><a class="reftop" id="cite_' . $i . '" name="cite_' . $i . '" href="#ref_' . $i . '" onclick="refToBottom(\'' . $i . '\');">[' . $i . ']</a></sup>';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   249
    $text = str_replace($match, $reflink, $text);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   250
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   251
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   252
  // compile refs div
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   253
  $refsdiv = '<div class="references">';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   254
  $refsdiv .= '<table border="0" width="100%"><tr><td valign="top">';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   255
  $count = ( count($refs) >= 20 ) ? floor(count($refs) / 2) : 99;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   256
  foreach ( $refs as $i => $ref )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   257
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   258
    $reflink = '<span id="ref_' . $i . '" name="ref_' . $i . '"><sup><b><a onclick="refToTop(\'' . $i . '\');" href="#cite_' . $i . '">^</a></b></sup> </span>';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   259
    $refsdiv .= "<div class=\"refbottom\" id=\"ref_{$i}_b\">$reflink $i. $ref</div>";
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   260
    if ( $i == $count )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   261
      $refsdiv .= '</td><td valign="top">';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   262
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   263
  $refsdiv .= '</td></tr></table>';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   264
  $refsdiv .= '</div>';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   265
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   266
  preg_match('#<references(([ ]*)/)?>#', $text, $match);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   267
  $text = str_replace_once($match[0], $refsdiv, $text);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   268
}
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   269
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   270
?>