MediaWiki

HighlightTerms.js: Difference between revisions

m (Reverted edits by Acyuta (Talk) to last version by Eivind)
No edit summary
 
Line 1: Line 1:
/*******  Vars  ********/
$jquery_dir = 'http://vanipedia.org/w/index.php?title=MediaWiki:';
$raw_mode = '&action=raw&ctype=text/javascript';
// Dependencies
document.write('<script type="text/javascript" src="'+$jquery_dir+'Jquery.highlight_sanskrit.js'+$raw_mode+'"></' + '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">
//Jquery highlighting function taking terms from page header inside <span class="terms" id="termN">
//Define functions
function high_this(key) {
//function for Highlighting
   $('p b a').parent().parent().highlight_sanskrit(quotemeta(key));
high_this = function($hi_term) {
    
   //regex search including "hi_term" which is the current term in the loop
   $('p').filter(function() {
  search1 = new RegExp('([^a-zA-Z])('+$hi_term+')([^a-zA-Z])', 'ig');
    return ! $(this).children().size() > 0;
  //search and replace where term is found in html copy
  }).highlight_sanskrit(quotemeta(key));
  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();
function quotemeta (string) {
print_this = function() {
    return string.replace(/([\(\)])/g, "\\$1");
  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 54: Line 32:
         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(/(\w+)[\ ]{2,}(\w+)/g, "$1 $2");
         hi_term[i] = hi_term[i].replace(/ {2,}/g, ' ');
         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);
      
      
   }
   }
 
});
});

Latest revision as of 16:18, 31 July 2008

/*******  Vars  ********/

$jquery_dir = 'http://vanipedia.org/w/index.php?title=MediaWiki:';
$raw_mode = '&action=raw&ctype=text/javascript';

// Dependencies
document.write('<script type="text/javascript" src="'+$jquery_dir+'Jquery.highlight_sanskrit.js'+$raw_mode+'"></' + '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) {
  $('p b a').parent().parent().highlight_sanskrit(quotemeta(key));
  
  $('p').filter(function() {
    return ! $(this).children().size() > 0;
  }).highlight_sanskrit(quotemeta(key));
}
function quotemeta (string) {
    return string.replace(/([\(\)])/g, "\\$1");
}
//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);
    }
    
  }
  
});