Wikulator.php
author Dan Fuhry <dan@enanocms.org>
Fri, 19 Nov 2010 03:31:10 -0500
changeset 6 63539b03fa5f
parent 4 a803741a5fc0
permissions -rw-r--r--
Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
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
/*
4
a803741a5fc0 Headings are now compatible with 1.1.7/1.1.8 parser
Dan
parents: 3
diff changeset
     3
Plugin Name: Wikulator
a803741a5fc0 Headings are now compatible with 1.1.7/1.1.8 parser
Dan
parents: 3
diff changeset
     4
Plugin URI: http://enanocms.org/plugin/wikulator
0
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
{
6
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    33
	static $heading_names = array();
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    34
	
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    35
  if ( strstr($text, '__NOTOC__') )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    36
    return true;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    37
  
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    38
  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
    39
    return true;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    40
  
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    41
  $heading_map = array();
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    42
  foreach ( $matches[1] as $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
    $heading_map[] = strlen($heading);
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    45
  }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    46
  
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    47
  if ( count($heading_map) < 4 && !strstr($text, '__TOC__') )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    48
    return true;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    49
  
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    50
  $prev = 0;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    51
  $levels = 0;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    52
  $treenum = array();
6
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    53
  $toc = "\n";
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    54
  foreach ( $heading_map as $i => $head )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
    55
  {
6
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    56
  	// $head = level of the heading (1-6)
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    57
  	  
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    58
  	if ( $head > $prev )
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    59
  	{
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    60
  		// deeper heading than the previous; indent
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    61
  		$toc .= "\n    <dl>\n  ";
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    62
  		$levels++;
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    63
  		$treenum[] = 0;
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    64
  	}
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    65
  	else if ( $head < $prev )
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    66
  	{
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    67
  		// shallower heading than the previous; go up by one
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    68
  		$toc .= "</dd></dl></dd>\n  ";
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    69
  		$levels--;
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    70
  		array_pop($treenum);
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    71
  	}
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    72
  	else
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    73
  	{
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    74
  		// same as previous; terminate it
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    75
  		$toc .= "</dd>\n  ";
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    76
  	}
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    77
  	
3
98ccd8815a02 Fixed a few tree construction + relative jump (1.1.7 parser) bugs in TOC
Dan
parents: 2
diff changeset
    78
    $treenum = array_values($treenum);
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    79
    if ( isset($treenum[count($treenum)-1]) )
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
    80
      $treenum[count($treenum)-1]++;
6
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    81
  	
4
a803741a5fc0 Headings are now compatible with 1.1.7/1.1.8 parser
Dan
parents: 3
diff changeset
    82
    if ( version_compare(enano_version(), '1.1.7', '>=') )
a803741a5fc0 Headings are now compatible with 1.1.7/1.1.8 parser
Dan
parents: 3
diff changeset
    83
    {
a803741a5fc0 Headings are now compatible with 1.1.7/1.1.8 parser
Dan
parents: 3
diff changeset
    84
		$tocid = sanitize_page_id(trim($matches[2][$i]));
a803741a5fc0 Headings are now compatible with 1.1.7/1.1.8 parser
Dan
parents: 3
diff changeset
    85
		$tocid = str_replace(array('[', ']'), '', $tocid);
6
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    86
		
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    87
		// conflict avoidance
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    88
		if ( isset($heading_names[$tocid]) )
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    89
		{
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    90
			$id = 2;
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    91
			while ( isset($heading_names["{$tocid}{$id}"]) )
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    92
				$id++;
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    93
			
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    94
			$tocid .= $id;
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    95
		}
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    96
		$heading_names[$tocid] = true;
4
a803741a5fc0 Headings are now compatible with 1.1.7/1.1.8 parser
Dan
parents: 3
diff changeset
    97
	}
a803741a5fc0 Headings are now compatible with 1.1.7/1.1.8 parser
Dan
parents: 3
diff changeset
    98
	else
a803741a5fc0 Headings are now compatible with 1.1.7/1.1.8 parser
Dan
parents: 3
diff changeset
    99
	{
a803741a5fc0 Headings are now compatible with 1.1.7/1.1.8 parser
Dan
parents: 3
diff changeset
   100
		$tocid = "$i";
a803741a5fc0 Headings are now compatible with 1.1.7/1.1.8 parser
Dan
parents: 3
diff changeset
   101
	}
6
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   102
	
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   103
	$toc .= '<dd><a href="#head:' . $tocid . '">' . implode('.', $treenum) . ' ' . htmlspecialchars($matches[2][$i]) . "</a>";
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   104
	
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   105
	$prev = $head;
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   106
  }
6
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   107
  // and at the end of the loop...
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   108
  $toc .= "</dd>\n";
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   109
  while ( $levels > 1 )
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   110
  {
6
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   111
  	  $toc .= "</dl></dd>\n";
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   112
  	  $levels--;
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   113
  }
6
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   114
  $toc .= "</dl>\n";
63539b03fa5f Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   115
  
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   116
  $toc_body = "<nowiki><div class=\"toc mdg-comment\">
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   117
                <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
   118
                <div>$toc</div>
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   119
              </div></nowiki>";
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   120
    
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   121
  if ( strstr($text, '__TOC__') )
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   122
  {
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   123
    $text = str_replace_once('__TOC__', $toc_body, $text);
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   124
  }
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   125
  else if ( $text === ($rtext = preg_replace('/^=/', "$toc_body\n\n=", $text)) )
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   126
  {
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   127
    $text = str_replace_once("\n=", "\n$toc_body\n=", $text);
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   128
  }
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   129
  else
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   130
  {
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   131
    $text = $rtext;
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   132
    unset($rtext);
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   133
  }
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   134
}
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   135
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   136
function mediafier_add_headers()
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   137
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   138
  global $db, $session, $paths, $template, $plugins; // Common objects
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   139
  $template->add_header("<style type=\"text/css\">
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   140
                        .highlight  { background-color: #FFFFC0; font-weight: bold; }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   141
                        .refbak     { background-color: #E0E0FF; font-weight: bold; }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   142
                        .references { font-size: smaller; }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   143
                      </style>");
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   144
  $ref_script = <<<EOF
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   145
      <enano:no-opt>
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   146
      <style type="text/css">
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   147
      div.toc {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   148
        display: table;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   149
        max-width: 70%;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   150
        padding: 0.7em 1.7em 0.7em 0.7em;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   151
        margin: 10px 0 0 0;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   152
      }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   153
      div.toc dl {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   154
        margin: 2px 0;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   155
      }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   156
      div.toc dd {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   157
        margin-left: 1em;
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   158
      }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   159
      </style>
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   160
      <script type="text/javascript">
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   161
      // <![CDATA[
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   162
        function refsOff()
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
          var divs = getElementsByClassName(document, '*', 'refbottom');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   165
          for ( var i in divs )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   166
          {
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   167
            \$dynano(divs[i]).rmClass('refbak');
0
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
          divs = getElementsByClassName(document, '*', 'reftop');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   170
          for ( var i in divs )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   171
          {
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   172
            \$dynano(divs[i]).rmClass('refbak');
0
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
        }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   175
        function refToBottom(id)
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
          refsOff();
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   178
          \$dynano('ref_'+id+'_b').addClass('refbak');
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   179
        }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   180
        function refToTop(id)
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
          refsOff();
1
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   183
          \$dynano('cite_'+id).addClass('refbak');
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   184
        }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   185
        function collapseTOC(el)
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   186
        {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   187
          var toc_inner = el.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('div')[0];
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   188
          if ( toc_inner.style.display == 'none' )
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   189
          {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   190
            el.innerHTML = 'hide';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   191
            toc_inner.style.display = 'block';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   192
          }
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   193
          else
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   194
          {
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   195
            el.innerHTML = 'show';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   196
            toc_inner.style.display = 'none';
616d046e3bd9 Table of contents support! Woooo!!
Dan
parents: 0
diff changeset
   197
          }
0
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
        // ]]>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   200
      </script>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   201
      </enano:no-opt>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   202
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   203
EOF;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   204
  $template->add_header($ref_script);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   205
}
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
function mediafy_highlight_search_words(&$result)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   208
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   209
  global $db, $session, $paths, $template, $plugins; // Common objects
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
  if ( !isset($_SERVER['HTTP_REFERER']) && !isset($_GET['highlight']) )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   212
    return false;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   213
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   214
  $referer = ( isset($_GET['highlight']) ) ? $_SERVER['REQUEST_URI'] : $_SERVER['HTTP_REFERER'];
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   215
  $term = ( isset($_GET['highlight']) ) ? 'highlight' : 'q';
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
  preg_match('/(\?|&)'.$term.'=(.+?)(&|$)/', $referer, $match);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   218
  if ( !isset($match[2]) )
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
    return false;
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
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   223
  $words = $match[2];
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   224
  $words = urldecode($words);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   225
  if ( $term == 'q' )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   226
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   227
    // it's from a search query - extract terms
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   228
    require_once(ENANO_ROOT . '/includes/search.php');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   229
    $words = parse_search_query($words, $warnings);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   230
    $words = array_merge($words['any'], $words['req']);
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
  else
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
    $words = explode(' ', $words);
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
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   237
  // strip HTML out of the rendered text
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   238
  $rand_seed = $session->dss_rand();
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   239
  preg_match_all('/<.+?>/', $result, $html_matches);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   240
  $i = 0;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   241
  foreach ( $html_matches[0] as $match )
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
    $i++;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   244
    $result = str_replace($match, "{HIGHLIGHT_PLACEHOLDER:$i:$rand_seed}", $result);
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
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   247
  // highlight matches
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   248
  foreach ( $words as $word )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   249
  {
2
88265c8715d0 Improvements to parsing for TOC
Dan
parents: 1
diff changeset
   250
    $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
   251
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   252
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   253
  // restore HTML
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   254
  $i = 0;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   255
  foreach ( $html_matches[0] as $match )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   256
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   257
    $i++;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   258
    $result = str_replace("{HIGHLIGHT_PLACEHOLDER:$i:$rand_seed}", $match, $result);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   259
  }
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
  // add "remove highlighting" link
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   262
  $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
   263
            $result;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   264
  
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
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   267
function mediafy_process_references(&$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
  global $db, $session, $paths, $template, $plugins; // Common objects
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   270
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   271
  // 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
   272
  if ( !preg_match('#<references(([ ]*)/)?>#', $text) )
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
    //die('no match');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   275
    return false;
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
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   278
  // Retrieve references
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   279
  preg_match_all('#<ref>(.+?)</ref>#s', $text, $matches);
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
  // Init counter
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   282
  $i = 0;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   283
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   284
  // Init refs array
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   285
  $refs = array();
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   286
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   287
  // main parser loop
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   288
  foreach ( $matches[0] as $j => $match )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   289
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   290
    $i++;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   291
    $inner =& $matches[1][$j];
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   292
    $refs[$i] = $inner;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   293
    $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
   294
    $text = str_replace($match, $reflink, $text);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   295
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   296
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   297
  // compile refs div
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   298
  $refsdiv = '<div class="references">';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   299
  $refsdiv .= '<table border="0" width="100%"><tr><td valign="top">';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   300
  $count = ( count($refs) >= 20 ) ? floor(count($refs) / 2) : 99;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   301
  foreach ( $refs as $i => $ref )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   302
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   303
    $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
   304
    $ref = trim($ref);
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   305
    $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
   306
    if ( $i == $count )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   307
      $refsdiv .= '</td><td valign="top">';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   308
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   309
  $refsdiv .= '</td></tr></table>';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   310
  $refsdiv .= '</div>';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   311
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   312
  preg_match('#<references(([ ]*)/)?>#', $text, $match);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   313
  $text = str_replace_once($match[0], $refsdiv, $text);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   314
}
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   315
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   316
?>