highlight.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // ----------------------------------------------------------------------------
  2. // Zoom Search Engine 4.2 (11/8/2005)
  3. // Highlight & auto-scroll script
  4. //
  5. // email: zoom@wrensoft.com
  6. // www: http://www.wrensoft.com
  7. //
  8. // Copyright (C) Wrensoft 2005
  9. // ----------------------------------------------------------------------------
  10. // Use this script to allow your search matches to highlight and scroll to
  11. // the matched word on the actual web page where it was found.
  12. //
  13. // You will need to link to this JS file from each page of your site
  14. // which requires the "highlight/jump to matched word" feature.
  15. //
  16. // For example, you could paste the following HTML in your site's header or
  17. // footer:
  18. //
  19. // <style>.highlight { background: #FFFF40; }</style>
  20. // <script type="text/javascript" src="highlight.js"></script>
  21. //
  22. // Note: You will need to specify the correct path to "highlight.js" depending
  23. // on where the file is located.
  24. //
  25. // You will then need to modify the BODY tag on your page to include an "onLoad"
  26. // attribute, such as:
  27. //
  28. // <body onload="highlight();">
  29. //
  30. // If for some reason you can not modify the body tag of your page, an alternative
  31. // would be to put the following line after the </body> tag of your page:
  32. //
  33. // <script type="text/javascript">highlight();</script>
  34. //
  35. // For more information, consult the Users Guide and our support website at:
  36. // http://www.wrensoft.com/zoom/support
  37. //
  38. //
  39. // This script is licensed for use with the Zoom Search Engine. Original
  40. // development by Brett Alcock, VBasys Limited. To licence other applications
  41. // email highlight@vbasys.com with the subject "license".
  42. var CatchJSErrors = true;
  43. function catcherror() { return true; }
  44. if (CatchJSErrors)
  45. {
  46. window.onerror = catcherror;
  47. }
  48. function QueryString(key)
  49. {
  50. var value = null;
  51. for (var i=0;i<QueryString.keys.length;i++)
  52. {
  53. if (QueryString.keys[i]==key)
  54. {
  55. value = QueryString.values[i];
  56. break;
  57. }
  58. }
  59. return value;
  60. }
  61. function QueryString_Parse()
  62. {
  63. var query = window.location.search.substring(1);
  64. var pairs = query.split("&");
  65. for (var i=0;i<pairs.length;i++)
  66. {
  67. var pos = pairs[i].indexOf('=');
  68. if (pos >= 0)
  69. {
  70. var argname = pairs[i].substring(0,pos);
  71. var value = pairs[i].substring(pos+1);
  72. QueryString.keys[QueryString.keys.length] = argname;
  73. QueryString.values[QueryString.values.length] = value;
  74. }
  75. }
  76. }
  77. QueryString.keys = new Array();
  78. QueryString.values = new Array();
  79. QueryString_Parse();
  80. function getElement(id)
  81. {
  82. if (document.getElementById)
  83. return(document.getElementById(id));
  84. else if (document.all)
  85. return(document.all[id]);
  86. }
  87. function findPosY(obj)
  88. {
  89. var curtop = 0;
  90. if (obj.offsetParent)
  91. {
  92. while (obj.offsetParent)
  93. {
  94. curtop += obj.offsetTop
  95. obj = obj.offsetParent;
  96. }
  97. }
  98. else if (obj.y)
  99. curtop += obj.y;
  100. return curtop;
  101. }
  102. // regular expression version
  103. function SearchHiLite(text)
  104. {
  105. var SearchAsSubstring = 0;
  106. var hl;
  107. hl = QueryString("zoom_highlight");
  108. if (hl == "" || hl == null)
  109. {
  110. hl = QueryString("zoom_highlightsub");
  111. if (hl == "" || hl == null)
  112. return false;
  113. else
  114. SearchAsSubstring = 1;
  115. }
  116. hl = unescape(hl);
  117. hl = hl.toLowerCase();
  118. // create array of terms
  119. //var term = hl.split("+");
  120. var re = /\"(.*?)\"|[^\\+\"]+/g;
  121. var term = hl.match(re);
  122. // convert terms in regexp patterns
  123. for (var i=0;i<term.length;i++) // take each term in turn
  124. {
  125. if(term[i] != "")
  126. {
  127. if (term[i].indexOf("\"") != -1)
  128. {
  129. // contains double quotes
  130. term[i]=term[i].replace(/\"/g,"");
  131. term[i]=term[i].replace(/\+/g," ");
  132. }
  133. else
  134. {
  135. term[i]=term[i].replace(/\+/g,"");
  136. }
  137. if (term[i].indexOf("*") != -1 || term[i].indexOf("?") != -1)
  138. {
  139. // convert wildcard pattern to regexp
  140. term[i] = term[i].replace(/\\/g, " ");
  141. term[i] = term[i].replace(/\^/g, " ");
  142. //term[i] = term[i].replace(/\+/g, " "); // split on this so no point in looking
  143. term[i] = term[i].replace(/\#/g, " ");
  144. term[i] = term[i].replace(/\$/g, " ");
  145. term[i] = term[i].replace(/\./g, " ");
  146. // check if search term only contains only wildcards
  147. // if so, we will not attempt to highlight this term
  148. var wildcards = /\w/;
  149. if (wildcards.test(term[i]))
  150. {
  151. term[i] = term[i].replace(/\*/g, "[^\\s]*");
  152. term[i] = term[i].replace(/\?/g, "[^\\s]"); // insist upon one non whitespace
  153. }
  154. else
  155. term[i] = "";
  156. }
  157. if (term[i] != "")
  158. {
  159. if (SearchAsSubstring == 0)
  160. {
  161. term[i] = "(>|>[^<]+[\\b\\W])("+term[i]+")(<|[\\b\\W][^>]*<)";
  162. }
  163. else
  164. {
  165. // if term leads with wildcard then allow it to match preceeding text in word
  166. var strWB="";
  167. if(term[i].substr(0,7)=="[^\\s]*") strWB="\\b";
  168. term[i] = "(>|>[^<]+)"+strWB+"("+term[i]+")([^>]*<)";
  169. }
  170. }
  171. }
  172. }
  173. text=text.replace(/&amp;/ig, '&');
  174. text=text.replace(/&nbsp;/ig, '');
  175. for (var i=0;i<term.length;i++) // take each term in turn
  176. {
  177. if(term[i] != "")
  178. {
  179. // we need a loop for the main search to catch all between ><
  180. // and we add  before each found to ignore those done etc
  181. // todo: develop reliable single pass regexp and dispose of loop
  182. var l = 0;
  183. re = new RegExp(term[i], "gi");
  184. var count = 0; // just incase
  185. text = ">" + text + "<"; // temporary tag marks
  186. do
  187. {
  188. l=text.length;
  189. text=text.replace(re, '$1<span class="highlight" id="highlight" name="highlight">$2</span id="highlight">$3');
  190. count++;
  191. }
  192. //while(re.lastIndex>0 && count<100); lastIndex not set properly under netscape
  193. while(l!=text.length && count<100);
  194. text = text.substring(1, text.length-1); // remove temporary tags
  195. }
  196. }
  197. text = text.replace(eval("//g"), '');
  198. text = text.replace(eval("//g"), '&nbsp;');
  199. return(text);
  200. }
  201. function jumpHL()
  202. {
  203. var d=getElement("highlight");
  204. if(d)
  205. {
  206. var y=findPosY(d);
  207. // if element near top of page
  208. if(y < 100)
  209. window.scrollTo(0,0); // go to top of page
  210. else
  211. window.scrollTo(0,y-50); // show space of 50 above
  212. }
  213. }
  214. function highlight()
  215. {
  216. var x = document.body;
  217. if (x)
  218. {
  219. var strHTML=SearchHiLite(x.innerHTML);
  220. if (strHTML!=false) x.innerHTML = strHTML;
  221. jumpHL();
  222. }
  223. }