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