HighlightTerms.js: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
/******* Vars ********/ | /******* Vars ********/ | ||
$ | $jquery_dir = 'http://vanipedia.org/w/index.php?title=MediaWiki:'; | ||
// Dependencies | // Dependencies | ||
document.write('<script type="text/javascript" src="'+$ | document.write('<script type="text/javascript" src="'+$jquery_dir+'jquery.highlight_sanskrit.js"></' + 'script>'); | ||
//Jquery highlighting function taking terms from page header inside <span class="terms" id="termN"> | //Jquery highlighting function taking terms from page header inside <span class="terms" id="termN"> |
Revision as of 16:15, 10 June 2008
/******* Vars ********/ $jquery_dir = 'http://vanipedia.org/w/index.php?title=MediaWiki:'; // Dependencies document.write('<script type="text/javascript" src="'+$jquery_dir+'jquery.highlight_sanskrit.js"></' + 'script>'); //Jquery highlighting function taking terms from page header inside <span class="terms" id="termN"> function high_this(key) { $('p b a').parent().parent().highlight_sanskrit(key, { global: false}); $('p').filter(function() { return ! $(this).children().size() > 0; }).highlight_sanskrit(key, {global: false}); } //BEGIN!!! $(document).ready(function() { //First check if the current page is an Article and that we are on view mode if (wgCanonicalNamespace == "" && wgAction == 'view') { //if spans with class terms exist if ($('span.terms').length) { //iterate hi_term = new Array(); $('span.terms').each(function(i) { //sanitize hi_term for search hi_term[i] = $(this).text().replace(/"/g, ""); hi_term[i] = hi_term[i].replace(/(^ *| *$)/mg, ""); hi_term[i] = hi_term[i].replace(/ {2,}/g, ' '); high_this(hi_term[i]); }); } else { //if no terms found, use wgTitle(title of article) as the search term for highlighting hi_term = wgTitle; high_this(hi_term); } } });//Jquery highlighting function taking terms from page header inside <span class="terms" id="termN"> //Define functions //function for Highlighting high_this = function($hi_term) { //regex search including "hi_term" which is the current term in the loop search1 = new RegExp('([^a-zA-Z])('+$hi_term+')([^a-zA-Z])', 'ig'); //search and replace where term is found in html copy span = "$1<span class='highlight_terms'>$2</span>$3"; new_html = new_html.replace(search1, span); } //Functions for unhighlighting //Unhighlighting would not be necessary if we could process the page element by element(DOM elements) but proved to be extremely slow when attempted. //unhigh_plus will fix anything that was highlighted(or was attempted to) within links(in their attributes), category divs and other infoboxes, etc. //All the unhigh functions will be called from the necesary objects found at the bottom of this app. unhigh_plus = function() { $now = $(this).html(); //alert($now); ///wiki/index.php?title=%3Cspan%20class=%27highlight_terms%27%3EManagement%3C/span%3E_%28Letters%29&action=edit§ion=12 search2 = new RegExp('<span class=(?:"|\')highlight_terms(?:"|\')>(.+?)</span>', 'img'); search3 = new RegExp('<span class=(?:"|\')highlight_terms(?:"|\')>(.+?)</span>', 'img'); search4 = new RegExp('%3Cspan%20class=%27highlight_terms%27%3E(.+?)%3C/span%3E', 'img') $now = $now.replace(search2, '$1'); $now = $now.replace(search3, '$1'); $now = $now.replace(search4, '$1'); $(this).html($now); } //Simple unhighlighting for 1 level ancestor html replacement unhigh_simple = function() { text = $(this).text(); $(this).text(text); } //Functions for debug, alert and window.write (use for debugging only) alert_this = function() { return alert($(this).html()); } //win1 = window.open(); print_this = function() { win1.document.write($(this).html()+'<br/>'); } //BEGIN!!! $(document).ready(function() { //First check if the current page is an Article and that we are on view mode if (wgCanonicalNamespace == "" && wgAction == 'view') { //make copy of body html new_html = $('#bodyContent').html(); //if spans with class terms exist if ($('span.terms').length) { //iterate hi_term = new Array(); $('span.terms').each(function(i) { //sanitize hi_term for search hi_term[i] = $(this).text().replace(/"/g, ""); hi_term[i] = hi_term[i].replace(/(^ *| *$)/mg, ""); hi_term[i] = hi_term[i].replace(/(\w+)[\ ]{2,}(\w+)/g, "$1 $2"); high_this(hi_term[i]); }); } else { //if no terms found, use wgTitle(title of article) as the search term for highlighting hi_term = wgTitle; high_this(hi_term); } //replace html body with new html coming from high_this() function $('#bodyContent').html(new_html); //We dont want titles to be highlighted ... (tough one)..jQuery to the rescue.. //Find every <b> with children <span class=hightlight_terms that do NOT have an anchor <a sibling... jQuery KiJaya! $('b span.highlight_terms').filter(function() { return !$(this).siblings('a').length; }).removeClass('highlight_terms'); //Fix terms by unhighlighting them with the unhigh function $('span.highlight_terms').parent('span').each(unhigh_simple); //Fix edit links //$('a').parent('span').each(unhigh_plus); //TODO exclude from category links <div id="catlinks"><p class="catlinks" //<span class="smwfactboxhead" $('#bodyContent').children().not('p').each(unhigh_plus); //$('p.catlinks').each(unhigh_plus); } });