Wikulator.php
author Dan
Fri, 29 May 2009 14:32:47 -0400
changeset 2 88265c8715d0
parent 1 616d046e3bd9
child 3 98ccd8815a02
permissions -rwxr-xr-x
Improvements to parsing for TOC
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
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    19
$plugins->attachHook('render_wikiformat_posttemplates', '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
    }
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    69
    if ( isset($treenum[count($treenum)-1]) )
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    70
      $treenum[count($treenum)-1]++;
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    71
    if ( $i > 0 )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    72
      $toc .= '</dd>';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    73
    $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
    74
    $prev = $head;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    75
  }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    76
  while ( $levels > 0 )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    77
  {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    78
    $toc .= '</dd></dl>';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    79
    $levels--;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    80
  }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    81
  $toc_body = "<nowiki><div class=\"toc mdg-comment\">
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    82
                <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
    83
                <div>$toc</div>
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    84
              </div></nowiki>";
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    85
    
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    86
  if ( strstr($text, '__TOC__') )
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    87
  {
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    88
    $text = str_replace_once('__TOC__', $toc_body, $text);
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    89
  }
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    90
  else if ( $text === ($rtext = preg_replace('/^=/', "$toc_body\n\n=", $text)) )
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    91
  {
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    92
    $text = str_replace_once("\n=", "\n$toc_body\n=", $text);
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    93
  }
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    94
  else
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    95
  {
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    96
    $text = $rtext;
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    97
    unset($rtext);
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    98
  }
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    99
}
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   100
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   101
function mediafier_add_headers()
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   102
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   103
  global $db, $session, $paths, $template, $plugins; // Common objects
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   104
  $template->add_header("<style type=\"text/css\">
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   105
                        .highlight  { background-color: #FFFFC0; font-weight: bold; }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   106
                        .refbak     { background-color: #E0E0FF; font-weight: bold; }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   107
                        .references { font-size: smaller; }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   108
                      </style>");
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   109
  $ref_script = <<<EOF
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   110
      <enano:no-opt>
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   111
      <style type="text/css">
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   112
      div.toc {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   113
        display: table;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   114
        max-width: 70%;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   115
        padding: 0.7em 1.7em 0.7em 0.7em;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   116
        margin: 10px 0 0 0;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   117
      }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   118
      div.toc dl {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   119
        margin: 2px 0;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   120
      }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   121
      div.toc dd {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   122
        margin-left: 1em;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   123
      }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   124
      </style>
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   125
      <script type="text/javascript">
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   126
      // <![CDATA[
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   127
        function refsOff()
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
          var divs = getElementsByClassName(document, '*', 'refbottom');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   130
          for ( var i in divs )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   131
          {
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   132
            \$dynano(divs[i]).rmClass('refbak');
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   133
          }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   134
          divs = getElementsByClassName(document, '*', 'reftop');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   135
          for ( var i in divs )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   136
          {
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   137
            \$dynano(divs[i]).rmClass('refbak');
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   138
          }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   139
        }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   140
        function refToBottom(id)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   141
        {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   142
          refsOff();
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   143
          \$dynano('ref_'+id+'_b').addClass('refbak');
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   144
        }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   145
        function refToTop(id)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   146
        {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   147
          refsOff();
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   148
          \$dynano('cite_'+id).addClass('refbak');
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   149
        }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   150
        function collapseTOC(el)
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   151
        {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   152
          var toc_inner = el.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('div')[0];
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   153
          if ( toc_inner.style.display == 'none' )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   154
          {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   155
            el.innerHTML = 'hide';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   156
            toc_inner.style.display = 'block';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   157
          }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   158
          else
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   159
          {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   160
            el.innerHTML = 'show';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   161
            toc_inner.style.display = 'none';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   162
          }
0
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
        // ]]>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   165
      </script>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   166
      </enano:no-opt>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   167
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   168
EOF;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   169
  $template->add_header($ref_script);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   170
}
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
function mediafy_highlight_search_words(&$result)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   173
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   174
  global $db, $session, $paths, $template, $plugins; // Common objects
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   175
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   176
  if ( !isset($_SERVER['HTTP_REFERER']) && !isset($_GET['highlight']) )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   177
    return false;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   178
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   179
  $referer = ( isset($_GET['highlight']) ) ? $_SERVER['REQUEST_URI'] : $_SERVER['HTTP_REFERER'];
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   180
  $term = ( isset($_GET['highlight']) ) ? 'highlight' : '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
  preg_match('/(\?|&)'.$term.'=(.+?)(&|$)/', $referer, $match);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   183
  if ( !isset($match[2]) )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   184
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   185
    return false;
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
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   188
  $words = $match[2];
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   189
  $words = urldecode($words);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   190
  if ( $term == 'q' )
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
    // it's from a search query - extract terms
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   193
    require_once(ENANO_ROOT . '/includes/search.php');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   194
    $words = parse_search_query($words, $warnings);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   195
    $words = array_merge($words['any'], $words['req']);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   196
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   197
  else
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   198
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   199
    $words = explode(' ', $words);
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
  // strip HTML out of the rendered text
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   203
  $rand_seed = $session->dss_rand();
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   204
  preg_match_all('/<.+?>/', $result, $html_matches);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   205
  $i = 0;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   206
  foreach ( $html_matches[0] as $match )
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
    $i++;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   209
    $result = str_replace($match, "{HIGHLIGHT_PLACEHOLDER:$i:$rand_seed}", $result);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   210
  }
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
  // highlight matches
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   213
  foreach ( $words as $word )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   214
  {
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   215
    $result = preg_replace('/([\W]|^)(' . str_replace('/', '\/', preg_quote($word)) . ')([\W])/i', "\\1<span class=\"highlight\">\\2</span>\\3", $result);
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   216
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   217
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   218
  // restore HTML
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   219
  $i = 0;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   220
  foreach ( $html_matches[0] as $match )
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
    $i++;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   223
    $result = str_replace("{HIGHLIGHT_PLACEHOLDER:$i:$rand_seed}", $match, $result);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   224
  }
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
  // add "remove highlighting" link
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   227
  $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
   228
            $result;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   229
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   230
}
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
function mediafy_process_references(&$text)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   233
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   234
  global $db, $session, $paths, $template, $plugins; // Common objects
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
  // 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
   237
  if ( !preg_match('#<references(([ ]*)/)?>#', $text) )
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
    //die('no match');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   240
    return false;
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
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   243
  // Retrieve references
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   244
  preg_match_all('#<ref>(.+?)</ref>#s', $text, $matches);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   245
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   246
  // Init counter
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   247
  $i = 0;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   248
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   249
  // Init refs array
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   250
  $refs = array();
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
  // main parser loop
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   253
  foreach ( $matches[0] as $j => $match )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   254
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   255
    $i++;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   256
    $inner =& $matches[1][$j];
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   257
    $refs[$i] = $inner;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   258
    $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
   259
    $text = str_replace($match, $reflink, $text);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   260
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   261
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   262
  // compile refs div
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   263
  $refsdiv = '<div class="references">';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   264
  $refsdiv .= '<table border="0" width="100%"><tr><td valign="top">';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   265
  $count = ( count($refs) >= 20 ) ? floor(count($refs) / 2) : 99;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   266
  foreach ( $refs as $i => $ref )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   267
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   268
    $reflink = '<span id="ref_' . $i . '" name="ref_' . $i . '"><sup><b><a onclick="refToTop(\'' . $i . '\');" href="#cite_' . $i . '">^</a></b></sup> </span>';
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   269
    $ref = trim($ref);
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   270
    $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
   271
    if ( $i == $count )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   272
      $refsdiv .= '</td><td valign="top">';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   273
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   274
  $refsdiv .= '</td></tr></table>';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   275
  $refsdiv .= '</div>';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   276
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   277
  preg_match('#<references(([ ]*)/)?>#', $text, $match);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   278
  $text = str_replace_once($match[0], $refsdiv, $text);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   279
}
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   280
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   281
?>