MediaWiki

MediaWiki:HighlightTerms.js

Revision as of 16:16, 31 July 2008 by Acyuta (talk | contribs) (Reverted edits by Acyuta (Talk) to last version by Eivind)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
//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&section=12
  search2 = new RegExp('<span class=(?:"|\')highlight_terms(?:"|\')>(.+?)</span>', 'img');
  search3 = new RegExp('&lt;span class=(?:"|\')highlight_terms(?:"|\')&gt;(.+?)&lt;/span&gt;', '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);
    
  }
});