# HG changeset patch # User Dan # Date 1230130405 18000 # Node ID 1783be24148802e3c310fbc648c1735b39dfe616 First commit; half-done and a bit tempermental. diff -r 000000000000 -r 1783be241488 Wikulator.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Wikulator.php Wed Dec 24 09:53:25 2008 -0500 @@ -0,0 +1,181 @@ +attachHook('render_wikiformat_post', 'mediafy($result);'); +$plugins->attachHook('compile_template', 'mediafier_add_headers();'); +$plugins->attachHook('html_attribute_whitelist', '$whitelist["ref"] = array(); $whitelist["references"] = array("/");'); + +function mediafy(&$text) +{ + global $db, $session, $paths, $template, $plugins; // Common objects + mediafy_highlight_search_words($text); + mediafy_process_references($text); +} + +function mediafier_add_headers() +{ + global $db, $session, $paths, $template, $plugins; // Common objects + $template->add_header(""); + $ref_script = << + + + +EOF; + $template->add_header($ref_script); +} + +function mediafy_highlight_search_words(&$result) +{ + global $db, $session, $paths, $template, $plugins; // Common objects + + if ( !isset($_SERVER['HTTP_REFERER']) && !isset($_GET['highlight']) ) + return false; + + $referer = ( isset($_GET['highlight']) ) ? $_SERVER['REQUEST_URI'] : $_SERVER['HTTP_REFERER']; + $term = ( isset($_GET['highlight']) ) ? 'highlight' : 'q'; + + preg_match('/(\?|&)'.$term.'=(.+?)(&|$)/', $referer, $match); + if ( !isset($match[2]) ) + { + return false; + } + + $words = $match[2]; + $words = urldecode($words); + if ( $term == 'q' ) + { + // it's from a search query - extract terms + require_once(ENANO_ROOT . '/includes/search.php'); + $words = parse_search_query($words, $warnings); + $words = array_merge($words['any'], $words['req']); + } + else + { + $words = explode(' ', $words); + } + + // strip HTML out of the rendered text + $rand_seed = $session->dss_rand(); + preg_match_all('/<.+?>/', $result, $html_matches); + $i = 0; + foreach ( $html_matches[0] as $match ) + { + $i++; + $result = str_replace($match, "{HIGHLIGHT_PLACEHOLDER:$i:$rand_seed}", $result); + } + + // highlight matches + foreach ( $words as $word ) + { + $result = preg_replace('/([\W]|^)(' . preg_quote($word) . ')([\W])/i', "\\1\\2\\3", $result); + } + + // restore HTML + $i = 0; + foreach ( $html_matches[0] as $match ) + { + $i++; + $result = str_replace("{HIGHLIGHT_PLACEHOLDER:$i:$rand_seed}", $match, $result); + } + + // add "remove highlighting" link + $result = '
Turn off highlighting
' . + $result; + +} + +function mediafy_process_references(&$text) +{ + global $db, $session, $paths, $template, $plugins; // Common objects + + // Is there a in the wikitext? If not, just return out to avoid empty processing + if ( !preg_match('##', $text) ) + { + //die('no match'); + return false; + } + + // Retrieve references + preg_match_all('#(.+?)#s', $text, $matches); + + // Init counter + $i = 0; + + // Init refs array + $refs = array(); + + // main parser loop + foreach ( $matches[0] as $j => $match ) + { + $i++; + $inner =& $matches[1][$j]; + $refs[$i] = $inner; + $reflink = '[' . $i . ']'; + $text = str_replace($match, $reflink, $text); + } + + // compile refs div + $refsdiv = '
'; + $refsdiv .= '
'; + $count = ( count($refs) >= 20 ) ? floor(count($refs) / 2) : 99; + foreach ( $refs as $i => $ref ) + { + $reflink = '^ '; + $refsdiv .= "
$reflink $i. $ref
"; + if ( $i == $count ) + $refsdiv .= '
'; + } + $refsdiv .= '
'; + $refsdiv .= '
'; + + preg_match('##', $text, $match); + $text = str_replace_once($match[0], $refsdiv, $text); +} + +?>