MediaWiki

MediaWiki:Jquery.highlight sanskrit.js

Revision as of 16:14, 10 June 2008 by Acyuta (talk | contribs) (New page: /** 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 p...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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);
      }
      var re1 = new RegExp('\(\\b'+key+'\\b\)', set_flags);
      // re2 = /\bkey\b/i; // Javascript is picky about this one in our  wiki pages
      if(html_2.search(re1) != -1) {
        match_index = html_2.search(re1);
        html_1 = html_1.substring(0, match_index)+'<span class="highlight_terms">'+
          html_1.substring(match_index, match_index + key.length)+
          '</span>'+ html_1.substring(match_index + key.length, html_1.length);
        $(this).html(html_1);  
      }
    })
  }
})(jQuery);