You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Peter Dawn <pe...@gmail.com> on 2006/08/17 07:38:39 UTC

Search Text

guys,
is there a component which allows text search. its sort of similar to
what the tassel site has, but i want the user to search through text,
as in general paragraphs. this info is not stored in the db but
locally within that page. so the ability to search within the content
of a page.
any ideas.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: Search Text

Posted by albartell <al...@gmail.com>.
>i would prefer to do it through tapestry. and if not is there a JS library
i can use.
Here is how I do it on my pages. Note that <c:out/> is a JSF component but
it could be just as easily done in Tapestry.

Hope that helps,
Aaron Bartell


...
<body onload="highlightSearchTerms('<c:out
value="${DocumentCtl.searchValue}"/>');">
...
/*
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 */
function doHighlight(bodyText, searchTerm, highlightStartTag,
highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<b>";
    highlightEndTag = "</b>";
  }
  
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >=
lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag +
bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
}


/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure,
highlightStartTag, highlightEndTag)
{

  if(searchText.length == 0){
    return false;
  }
  
  

  treatAsPhrase = true;
  
  // if the treatAsPhrase parameter is true, then we should search for 
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  
  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable.
Searching will not work.");
    }
    return false;
  }
  
  var bodyText = document.body.innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag,
highlightEndTag);
  }
  
  document.body.innerHTML = bodyText;
  return true;
}


/*
 * This displays a dialog box that allows a user to enter their own
 * search terms to highlight on the page, and then passes the search
 * text or phrase to the highlightSearchTerms function. All parameters
 * are optional.
 */
function searchPrompt(defaultText, treatAsPhrase, textColor, bgColor)
{
  // This function prompts the user for any words that should
  // be highlighted on this web page
  if (!defaultText) {
    defaultText = "";
  }
  
  // we can optionally use our own highlight tag values
  if ((!textColor) || (!bgColor)) {
    highlightStartTag = "";
    highlightEndTag = "";
  } else {
    highlightStartTag = "<font style='color:" + textColor + ";
background-color:" + bgColor + ";'>";
    highlightEndTag = "</font>";
  }
  
  if (treatAsPhrase) {
    promptText = "Please enter the phrase you'd like to search for:";
  } else {
    promptText = "Please enter the words you'd like to search for, separated
by spaces:";
  }
  
  searchText = prompt(promptText, defaultText);

  if (!searchText)  {
    alert("No search terms were entered. Exiting function.");
    return false;
  }
  
  return highlightSearchTerms(searchText, treatAsPhrase, true,
highlightStartTag, highlightEndTag);
}


/*
 * This function takes a referer/referrer string and parses it
 * to determine if it contains any search terms. If it does, the
 * search terms are passed to the highlightSearchTerms function
 * so they can be highlighted on the current page.
 */
function highlightGoogleSearchTerms(referrer)
{
  // This function has only been very lightly tested against
  // typical Google search URLs. If you wanted the Google search
  // terms to be automatically highlighted on a page, you could
  // call the function in the onload event of your <body> tag, 
  // like this:
  //   <body onload='highlightGoogleSearchTerms(document.referrer);'>
  
  //var referrer = document.referrer;
  if (!referrer) {
    return false;
  }
  
  var queryPrefix = "q=";
  var startPos = referrer.toLowerCase().indexOf(queryPrefix);
  if ((startPos < 0) || (startPos + queryPrefix.length == referrer.length))
{
    return false;
  }
  
  var endPos = referrer.indexOf("&", startPos);
  if (endPos < 0) {
    endPos = referrer.length;
  }
  
  var queryString = referrer.substring(startPos + queryPrefix.length,
endPos);
  // fix the space characters
  queryString = queryString.replace(/%20/gi, " ");
  queryString = queryString.replace(/\+/gi, " ");
  // remove the quotes (if you're really creative, you could search for the
  // terms within the quotes as phrases, and everything else as single
terms)
  queryString = queryString.replace(/%22/gi, "");
  queryString = queryString.replace(/\"/gi, "");
  
  return highlightSearchTerms(queryString, false);
}


/*
 * This function is just an easy way to test the highlightGoogleSearchTerms
 * function.
 */
function testHighlightGoogleSearchTerms()
{
  var referrerString =
"http://www.google.com/search?q=javascript%20highlight&start=0";
  referrerString = prompt("Test the following referrer string:",
referrerString);
  return highlightGoogleSearchTerms(referrerString);
}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Search Text

Posted by Peter Dawn <pe...@gmail.com>.
i would prefer to do it through tapestry. and if not is there a JS
library i can use.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Search Text

Posted by Gurps <g...@gurpal.co.uk>.
maybe do it using javascript highlighting or more complicated put some sort
of post-filter after rendering the content (don't know how to do it in
tapestry but know via JSP/JSTL)...


Peter Dawn wrote:
> 
> guys,
> is there a component which allows text search. its sort of similar to
> what the tassel site has, but i want the user to search through text,
> as in general paragraphs. this info is not stored in the db but
> locally within that page. so the ability to search within the content
> of a page.
> any ideas.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Search-Text-tf2119682.html#a5847713
Sent from the Tapestry - User forum at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Search Text

Posted by Peter Dawn <pe...@gmail.com>.
i am going to have a look at lucene and try to get that to work.
otherwise might use a simple JS implementation. through tapestry would
be good but if its too time consuming then no.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: Search Text

Posted by Mark Stang <ms...@pingidentity.com>.
I guess I may have to reduce my expectations to only searching components.


-----Original Message-----
From: Jesse Kuhnert [mailto:jkuhnert@gmail.com]
Sent: Thu 8/17/2006 4:58 PM
To: Tapestry users
Subject: Re: Search Text
 
There is a special type of component that people don't get exposed to
normall that contains any non managed text (ie free flow typed text in
html), that might be a possibility..(I think it's called
TextToken..something like that)..

You're not going to be able to search a whole page in the sense of searching
what might be dynamic content generated from components though...Well...You
could, but it wouldn't be very performant.

On 8/17/06, Mark Stang <ms...@pingidentity.com> wrote:
>
> I would like to be able to search all my pages for text.  Which is
> different from the current page.
>
> Thoughts?
>
>
> -----Original Message-----
> From: James Carman [mailto:james@carmanconsulting.com]
> Sent: Thu 8/17/2006 3:55 PM
> To: 'Tapestry users'
> Subject: RE: Search Text
>
> Okay, cool.  I am one of those guys who try to use the "simplest solution
> that works."  I was just checking if Ctrl-F applied. :-)
>
>
> -----Original Message-----
> From: Peter Dawn [mailto:petedawn@gmail.com]
> Sent: Thursday, August 17, 2006 5:50 PM
> To: Tapestry users
> Subject: Re: Search Text
>
> i will try albartell's approach and see if it works. i was hoping
> there would be a tapestry component i could use. and James i dont want
> to use ctrl+f. i mean i can, but i would like to create something more
> specific to my web app.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>
>


-- 
Jesse Kuhnert
Tapestry/Dojo/(and a dash of TestNG), team member/developer

Open source based consulting work centered around
dojo/tapestry/tacos/hivemind.


Re: Search Text

Posted by Jesse Kuhnert <jk...@gmail.com>.
There is a special type of component that people don't get exposed to
normall that contains any non managed text (ie free flow typed text in
html), that might be a possibility..(I think it's called
TextToken..something like that)..

You're not going to be able to search a whole page in the sense of searching
what might be dynamic content generated from components though...Well...You
could, but it wouldn't be very performant.

On 8/17/06, Mark Stang <ms...@pingidentity.com> wrote:
>
> I would like to be able to search all my pages for text.  Which is
> different from the current page.
>
> Thoughts?
>
>
> -----Original Message-----
> From: James Carman [mailto:james@carmanconsulting.com]
> Sent: Thu 8/17/2006 3:55 PM
> To: 'Tapestry users'
> Subject: RE: Search Text
>
> Okay, cool.  I am one of those guys who try to use the "simplest solution
> that works."  I was just checking if Ctrl-F applied. :-)
>
>
> -----Original Message-----
> From: Peter Dawn [mailto:petedawn@gmail.com]
> Sent: Thursday, August 17, 2006 5:50 PM
> To: Tapestry users
> Subject: Re: Search Text
>
> i will try albartell's approach and see if it works. i was hoping
> there would be a tapestry component i could use. and James i dont want
> to use ctrl+f. i mean i can, but i would like to create something more
> specific to my web app.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>
>


-- 
Jesse Kuhnert
Tapestry/Dojo/(and a dash of TestNG), team member/developer

Open source based consulting work centered around
dojo/tapestry/tacos/hivemind.

RE: Search Text

Posted by Mark Stang <ms...@pingidentity.com>.
I would like to be able to search all my pages for text.  Which is different from the current page.

Thoughts?


-----Original Message-----
From: James Carman [mailto:james@carmanconsulting.com]
Sent: Thu 8/17/2006 3:55 PM
To: 'Tapestry users'
Subject: RE: Search Text
 
Okay, cool.  I am one of those guys who try to use the "simplest solution
that works."  I was just checking if Ctrl-F applied. :-)


-----Original Message-----
From: Peter Dawn [mailto:petedawn@gmail.com] 
Sent: Thursday, August 17, 2006 5:50 PM
To: Tapestry users
Subject: Re: Search Text

i will try albartell's approach and see if it works. i was hoping
there would be a tapestry component i could use. and James i dont want
to use ctrl+f. i mean i can, but i would like to create something more
specific to my web app.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org



RE: Search Text

Posted by James Carman <ja...@carmanconsulting.com>.
Okay, cool.  I am one of those guys who try to use the "simplest solution
that works."  I was just checking if Ctrl-F applied. :-)


-----Original Message-----
From: Peter Dawn [mailto:petedawn@gmail.com] 
Sent: Thursday, August 17, 2006 5:50 PM
To: Tapestry users
Subject: Re: Search Text

i will try albartell's approach and see if it works. i was hoping
there would be a tapestry component i could use. and James i dont want
to use ctrl+f. i mean i can, but i would like to create something more
specific to my web app.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Search Text

Posted by Peter Dawn <pe...@gmail.com>.
i will try albartell's approach and see if it works. i was hoping
there would be a tapestry component i could use. and James i dont want
to use ctrl+f. i mean i can, but i would like to create something more
specific to my web app.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: Search Text

Posted by James Carman <ja...@carmanconsulting.com>.
Hitting Ctrl-F on your browser won't do it?  If it's the currently-displayed
HTML document, your browser can do the searching for you, no?


-----Original Message-----
From: Peter Dawn [mailto:petedawn@gmail.com] 
Sent: Thursday, August 17, 2006 8:53 AM
To: Tapestry users
Subject: Re: Search Text

yes. thats the idea. i have been reading about lucene too. anyone tried
that.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Search Text

Posted by Peter Dawn <pe...@gmail.com>.
yes. thats the idea. i have been reading about lucene too. anyone tried that.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: Search Text

Posted by James Carman <ja...@carmanconsulting.com>.
Are you talking about searching for text within the page that you're
reading? 

-----Original Message-----
From: Peter Dawn [mailto:petedawn@gmail.com] 
Sent: Thursday, August 17, 2006 1:39 AM
To: tapestry-user@jakarta.apache.org
Subject: Search Text

guys,
is there a component which allows text search. its sort of similar to
what the tassel site has, but i want the user to search through text,
as in general paragraphs. this info is not stored in the db but
locally within that page. so the ability to search within the content
of a page.
any ideas.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org