Highlighting Text fin red rom DB in view

Hi Forum,

I have various trap messages stored as text in my DB and accessable in
my view as @trap. (trap.ipadd, trap.desc etc…)

I would like to highlight certain key words in red if they exist in the
text.
I was thinking of trying to use gsub and add tags when displaying
in my view.

Can anyone think of an easier way to do this?

thanks

jackster

Can anyone think of an easier way to do this?

How about using the ‘highlight’ helper method in your view instead?

I was just looking into Text::HTMLHighlighter…is that what you mean?

Aaron C. wrote:

Can anyone think of an easier way to do this?

How about using the ‘highlight’ helper method in your view instead?

I was thinking about the one in ActionView::Helpers::TextHelper. The
description is as follows:

highlight(text, phrases, highlighter = ‘\1</
strong>’)

Highlights one or more phrases everywhere in text by inserting it into
a highlighter string. The highlighter can be specialized by passing
highlighter as a single-quoted string with \1 where the phrase is to
be inserted (defaults to ‘\1’)

hmmmm…that looks like it might work…thanks

How do I include that Rails application. I tried just putting this in my
view and it does nothing:

highlight(‘this is sample text’, ‘text’)

How does rail know about it…maybe I should requiring action_view ?

Aaron C. wrote:

I was thinking about the one in ActionView::Helpers::TextHelper. The
description is as follows:

highlight(text, phrases, highlighter = ‘\1</
strong>’)

Highlights one or more phrases everywhere in text by inserting it into
a highlighter string. The highlighter can be specialized by passing
highlighter as a single-quoted string with \1 where the phrase is to
be inserted (defaults to ‘\1’)

sorry for that…I didn’t even try it with the <%= at the
beginning…it does work for me too now…thanks for the help on
this.

The only issue I have is that it makes the text strong bold…what I
need is to make it highlighted in red or other colors…I don’t see
anything in the doco to do that.

I installed Text::HTMLHighlighter and all the tests ran successfully but
I can’t seem to figure out how to get it to work in my view. There is an
.rb file that I’m supposed to call somehow and I can’t figure you how to
do it. The directions seem to make me think Text::HTMLHighlighter is
made to work with other ruby files and not specifically for Rails…not
sure…

jackster

Aaron C. wrote:

It should be available in your view. I just tried adding this to my
view and it worked as advertised:

<%= highlight(“this is sample text”, “text”) %>

I’m using Rails 2.0.2.

On Jan 30, 5:29 pm, jackster the jackle <rails-mailing-l…@andreas-

On Jan 30, 10:11 pm, jackster the jackle <rails-mailing-l…@andreas-
s.net> wrote:

The only issue I have is that it makes the text strong bold…what I
need is to make it highlighted in red or other colors…I don’t see
anything in the doco to do that.

As stated in the docs, you can specialize the highlighter by providing
a value for the 3rd argument. The default is to highlight using
text, but you can get the effect you want by doing something
like:

<%= highlight(‘this is sample text’, ‘text’, '<span class=‘highlight">
\1’) %>

and adding this to your CSS file:

.highlight {color: red}

It should be available in your view. I just tried adding this to my
view and it worked as advertised:

<%= highlight(“this is sample text”, “text”) %>

I’m using Rails 2.0.2.

On Jan 30, 5:29 pm, jackster the jackle <rails-mailing-l…@andreas-

On Jan 31, 5:37 am, jackster the jackle [email protected] wrote:

The last question I have is what happens when I want to highlight more
than one word if it appears in my variable?

Specifying an array for the 2nd argument should do the trick:

<%= highlight(trap.datetime.to_s, [‘Thu’, ‘2008’]) %>

You may have to add spaces to your phrase strings to prevent the
highlighter from highlighting things you didn’t intend. For example,
I tried this

<%= highlight(‘foo bar foobar’, [‘foo’, ‘bar’]) %>

and it highlighted the ‘foobar’ substring since ‘foo’ matched the
first part of the string and ‘bar’ matched the last part of it.
Changing the phrase array to ['foo ', 'bar '] (note the embedded
whitespace) fixed it.

Burning the candle at both ends, I see from the timestamp of your last
message :slight_smile:

You’ve helped me alot, Aaron…and thank you for that.

As you have pointed out…defining highlight in my CSS works perfectly.
In fact, I found that as soon as I defined it, the following statement
works:

<%= highlight(‘this is simple text’, ‘text’) %>

And I didn’t have to add the part although
that worked as well.

The last question I have is what happens when I want to highlight more
than one word if it appears in my variable?

For instance, I have a variable that contains a date, what I need to be
able to do is highlight Thu when it’s Thursday but always highlight
2008.
This works to always highlight 2008:

<%= highlight(trap.datetime.to_s, ‘2008’) %>

I was hoping I could do something like this but it doesn’t work because
it looks for both of them in a string:

<%= highlight(trap.datetime.to_s, ‘Thu, 2008’) %>

What I’m actually trying to accomplish here, is to highlight certain
words in my network traps in red. For instance, if I got a trap that
contained the word “BGP”, I want that highlighted in red as well as if
the word “Spanning-tree” is contained in the trap. This is why I was
hoping to be able to specify several words to be highlighted if they
exist in my variable.

thanks alot Aaron,

jackster

Aaron C. wrote:

On Jan 30, 10:11 pm, jackster the jackle <rails-mailing-l…@andreas-
s.net> wrote:

The only issue I have is that it makes the text strong bold…what I
need is to make it highlighted in red or other colors…I don’t see
anything in the doco to do that.

As stated in the docs, you can specialize the highlighter by providing
a value for the 3rd argument. The default is to highlight using
text, but you can get the effect you want by doing something
like:

<%= highlight(‘this is sample text’, ‘text’, '<span class=‘highlight">
\1’) %>

and adding this to your CSS file:

.highlight {color: red}

I can’t thank you enough for your help on this…I’m up and running on
this now and all is looking good.

jackster

Aaron C. wrote:

On Jan 31, 5:37 am, jackster the jackle [email protected] wrote:

The last question I have is what happens when I want to highlight more
than one word if it appears in my variable?

Specifying an array for the 2nd argument should do the trick:

<%= highlight(trap.datetime.to_s, [‘Thu’, ‘2008’]) %>

You may have to add spaces to your phrase strings to prevent the
highlighter from highlighting things you didn’t intend. For example,
I tried this

<%= highlight(‘foo bar foobar’, [‘foo’, ‘bar’]) %>

and it highlighted the ‘foobar’ substring since ‘foo’ matched the
first part of the string and ‘bar’ matched the last part of it.
Changing the phrase array to ['foo ', 'bar '] (note the embedded
whitespace) fixed it.

Aaron…I wanted to run one more thing by you on this subject if you
don’t mind?

I am using the following command which works nicely to highlight key
words in red if they are contained in my network trap:

<%= highlight(trap.desc4.to_s,[‘DOWN’, ‘COLD’, ‘BGP’, ‘RESTART’,
‘SPAN’]) %>

Can you think of an easy way to highlight some words in red and others
in another color?

I have never used a conditional if statement in a view before and don’t
even know if that’s possible…if it was, I was thinking something like
this might be the a solution:

if
<%= highlight(trap.desc4.to_s,[‘DOWN’, ‘COLD’, ‘BGP’, ‘RESTART’,
‘SPAN’]) %>
else
<%= highlight_blue(trap.desc4.to_s,[‘UP’, ‘HOT’, ‘OSPF’, ‘START’,
‘LINK’]) %>

…or perhaps you might know of an easier solution which I am
overlooking?

thanks for all the help

jackster

jackster the jackle wrote:

I can’t thank you enough for your help on this…I’m up and running on
this now and all is looking good.

jackster

Aaron C. wrote:

On Jan 31, 5:37 am, jackster the jackle [email protected] wrote:

The last question I have is what happens when I want to highlight more
than one word if it appears in my variable?

Specifying an array for the 2nd argument should do the trick:

<%= highlight(trap.datetime.to_s, [‘Thu’, ‘2008’]) %>

You may have to add spaces to your phrase strings to prevent the
highlighter from highlighting things you didn’t intend. For example,
I tried this

<%= highlight(‘foo bar foobar’, [‘foo’, ‘bar’]) %>

and it highlighted the ‘foobar’ substring since ‘foo’ matched the
first part of the string and ‘bar’ matched the last part of it.
Changing the phrase array to ['foo ', 'bar '] (note the embedded
whitespace) fixed it.

Just off the top of my head, you could write your own helper something
like this:

def highlight_with_color(text, phrases, color_spec)
highlight(text, phrases, ‘\1</
span>’)
end

and then use it in your view like this:

<%= highlight_with_color(“foo bar”, [“foo, bar”], “#abcdef”) %>

Disclaimer: I haven’t actually tried this code, but I think something
like this should work. Someone in this group may also have a better
suggestion…

On Jan 31, 4:22 pm, jackster the jackle <rails-mailing-l…@andreas-

perhaps I either did a poor job explaining or don’t understand your
code, Aaron…please excuse.

here’s an example:

I have an instance variable:

@variable = “this is some text”

How can I make the word “this” be red and the word “some” be green?

If I define highlight in my CSS, I can make it both of them red or both
green with this:

<%= highlight(@variable,[‘this’, ‘some’]) %>

but can’t see a way to make each a different color.

jackster

Aaron C. wrote:

Just off the top of my head, you could write your own helper something
like this:

def highlight_with_color(text, phrases, color_spec)
highlight(text, phrases, ‘\1</
span>’)
end

and then use it in your view like this:

<%= highlight_with_color(“foo bar”, [“foo, bar”], “#abcdef”) %>

Disclaimer: I haven’t actually tried this code, but I think something
like this should work. Someone in this group may also have a better
suggestion…

On Jan 31, 4:22 pm, jackster the jackle <rails-mailing-l…@andreas-

I got lucky and figured this one out. The following sets all the words
in the array the color of highlight as defined in my CSS, the outer
highlight method sets the word “CDP” to the color of my blue.css:

<%=highlight(highlight(trap.desc4.to_s, [‘LOST’, ‘DOWN’, ‘COLD’, ‘BGP’,
‘RESTART’, ‘SPAN’])‘CDP’) %>

This works nicely…but do I wonder if I have to add another iteration
of the highlight method on top of that for every additional word that I
want to make blue?

jackster

jackster the jackle wrote:

perhaps I either did a poor job explaining or don’t understand your
code, Aaron…please excuse.

here’s an example:

I have an instance variable:

@variable = “this is some text”

How can I make the word “this” be red and the word “some” be green?

If I define highlight in my CSS, I can make it both of them red or both
green with this:

<%= highlight(@variable,[‘this’, ‘some’]) %>

but can’t see a way to make each a different color.

jackster

Aaron C. wrote:

Just off the top of my head, you could write your own helper something
like this:

def highlight_with_color(text, phrases, color_spec)
highlight(text, phrases, ‘\1</
span>’)
end

and then use it in your view like this:

<%= highlight_with_color(“foo bar”, [“foo, bar”], “#abcdef”) %>

Disclaimer: I haven’t actually tried this code, but I think something
like this should work. Someone in this group may also have a better
suggestion…

On Jan 31, 4:22 pm, jackster the jackle <rails-mailing-l…@andreas-