MediaWiki

MediaWiki:Jquery.highlight sanskrit.js

Revision as of 11:19, 5 August 2008 by Acyuta (talk | contribs)

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.
/**
  Highlight_sanskrit.js plug-in for jQuery
  This plug-in is used to highlight terms used in every vaniquotes article and to highlight terms passed
  from a vaniquote to a vanisource page.
  Usage: $(elem).highlight_sanskrit('Krsna', { global: false, i: true });
  options are global: boolean(true|false) and i(case insensitive): boolean(true|false). By default they are both true.
**/
// Dependencies
document.write('<script type="text/javascript" src="'+$jquery_dir+'Unify.js"></' + 'script>');
document.write('<script type="text/javascript" src="'+$jquery_dir+'UniToASCII.js"></' + 'script>');

(function($) {
  jQuery.fn.highlight_sanskrit = function(key, flags) {
    flags = jQuery.extend({
      i: true,
      global: true
    }, flags);
    return this.each(function() {
      var html_1 = $(this).html();
      var html_2 = UniToASCII(html_1);
      var set_flags = '';
      flags.i ? set_flags += 'i':'';
      flags.global ? set_flags += 'g':'';
      if (/ä|é|ü|å|è|ì|ï|ö|ò|ë|ç|à|ù|ÿ|û|Ä|É|Ü|Å|È|Ì|Ñ|Ï|Ö|Ò|Ë|Ç|À|Ù|ß/.test(key)) {
        key = UniToASCII(Unify(key));
      }
      if(/ā|ī|ū|ṛ|ṟ|ṅ|ṣ|ñ|ṭ|ḍ|ṇ|ś|ṁ|ḥ|ḷ|ḻ|Ā|Ī|Ū|Ṛ|Ṟ|Ṣ|Ñ|Ṭ|Ḍ|Ṇ|Ś|Ṁ|Ḥ|Ḷ/.test(key)) {
        key = UniToASCII(key);
      }
      
      html_f = html_1;
      re1 = new RegExp('\\b'+key+'\\b', set_flags);
      matches=html_2.match(re1);
      if(matches) {
        var found=0;
        var indexes=new Array(matches.length);
        for(var m=0; m < matches.length; m++){
          found=html_2.substring(0, found).length;
          indexes[m]=found+html_2.substring(found).search(re1);
          key1 = '<span class="highlight_terms">'+html_1.substr(indexes[m], key.length)+'</span>';
          key2 = '<span class="highlight_terms">'+matches[m]+'</span>';
          len = matches[m].length;
          a1 = html_1.substring(0, indexes[m]);
          a2 = html_2.substring(0, indexes[m]);
          b1 = html_1.substring(a1.length+len);
          b2 = html_2.substring(a2.length+len);
          html_1 = a1+key1+b1;
          html_2 = a2+key2+b2;        
          found=indexes[m]+key2.length;
        $(this).html(html_1);  
      }
      }
    })
  }
/* Hack derived from imatch.js
  Thanks to imatch.js in http://www.fullposter.com/?1
*/
})(jQuery);