MediaWiki

HighlightTerms.js: Difference between revisions

No edit summary
m (Reverted edits by Acyuta (Talk) to last version by Eivind)
Line 1: Line 1:
/*******  Vars  ********/
$root_dir = '';
$img_dir = '/images';
$jquery_dir = '/jquery';
// 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">
//Jquery highlighting function taking terms from page header inside <span class="terms" id="termN">
function high_this(key) {
//Define functions
   $('p b a').parent().parent().highlight_sanskrit(quotemeta(key));
//function for Highlighting
    
high_this = function($hi_term) {
   $('p').filter(function() {
   //regex search including "hi_term" which is the current term in the loop
    return ! $(this).children().size() > 0;
  search1 = new RegExp('([^a-zA-Z])('+$hi_term+')([^a-zA-Z])', 'ig');
   }).highlight_sanskrit(quotemeta(key));
  //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());
}
}
function quotemeta (string) {
//win1 = window.open();
    return string.replace(/([\(\)])/g, "\\$1");
print_this = function() {
  win1.document.write($(this).html()+'<br/>');
}
}
//BEGIN!!!
//BEGIN!!!
$(document).ready(function() {
$(document).ready(function() {
   //First check if the current page is an Article and that we are on view mode
   //First check if the current page is an Article and that we are on view mode
   if (wgCanonicalNamespace == '' && wgAction == 'view') {
   if (wgCanonicalNamespace == "" && wgAction == 'view') {
 
      //make copy of body html
    new_html = $('#bodyContent').html();
   
     //if spans with class terms exist
     //if spans with class terms exist
     if ($('span.terms').length) {
     if ($('span.terms').length) {
Line 31: Line 54:
         hi_term[i] = $(this).text().replace(/"/g, "");
         hi_term[i] = $(this).text().replace(/"/g, "");
         hi_term[i] = hi_term[i].replace(/(^ *| *$)/mg, "");
         hi_term[i] = hi_term[i].replace(/(^ *| *$)/mg, "");
         hi_term[i] = hi_term[i].replace(/ {2,}/g, ' ');
         hi_term[i] = hi_term[i].replace(/(\w+)[\ ]{2,}(\w+)/g, "$1 $2");
         high_this(hi_term[i]);
         high_this(hi_term[i]);  
       });
       });
     } else {
     } else {
       //if no terms found, use wgTitle(title of article) as the search term for highlighting
       //if no terms found, use wgTitle(title of article) as the search term for highlighting
       hi_term = wgTitle;
       hi_term = wgTitle;
       high_this(hi_term);
       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);
      
      
   }
   }
 
});
});

Revision as of 16:16, 31 July 2008

//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);
    
  }
});