Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: "em" - Emphasize text using regular expressions

by kcott (Archbishop)
on Apr 18, 2013 at 20:45 UTC ( [id://1029417]=note: print w/replies, xml ) Need Help??


in reply to "em" - Emphasize text using regular expressions

G'day FloydATC,

"Not sure if this counts as "cool use" ..."

I thought so. I now have a copy in ~ken/local/bin/. Thanks. (++ when the Vote Fairy next visits)

"I would love for someone to adopt this and put it on CPAN so myself and others can get easy access to it"

Is there a reason you can't do this yourself? Take a look at How to submit a script to CPAN.

"There's one annoying limitation; overlapping matches don't behave the way they should, and I can't find a way to fix it."

You'll need to document this in a little more detail than "the result can be... interesting" and "don't behave the way they should".

-- Ken

Replies are listed 'Best First'.
Re^2: "em" - Emphasize text using regular expressions
by FloydATC (Deacon) on Apr 19, 2013 at 13:48 UTC
    Is there a reason you can't do this yourself?
    I'm lazy. And a little bit out of my depth.
    You'll need to document this in a little more detail
    Update: This problem has been fixed. Finally :-)

    The problem is was really simple, it's just a bit difficult to describe precisely and even more difficult to fix properly.

    For every expression that matches, the script will would prefix the match with ANSI codes to change the color, then suffix it with another ANSI code to "reset" to the default color. If this overlaps with a previous match on the same line, the ANSI "reset" code will would remove any other color codes that should still have been in effect.

    Example 1:
    echo "aabbccddee" | em bbccdd red cc blue
    would produce
    aa[RED]bb[BLUE]cc[RESET]dd[RESET]ee
    which is incorrect because the BLUE/RESET resets "dd" which should have been RED.

    Example 2:
    echo "aabbccddee" | em cc blue bbccdd red
    would produce
    aabb[BLUE]cc[RESET]ddee
    because "bbBLUEccRESETdd" does not match "bbccdd".

    I fear the only real solution would be to replace the simple regex substitute with a state machine that uses a color stack to see what color we should reset to. Unfortunately, this means we go from a quick and simple tool to a complicated piece of code that could fail even harder.

    Instead of a simple substitution, I now keep an array of the codes I would insert. For each matching rule, I now scan this array to find the proper "reset color code" instead of just inserting a plain ANSI reset. Only when all the matching has finished I apply the codes using substr().

    -- Time flies when you don't know what you're doing

      Why are you using the color function when there's colored which can limit the scope of text that you want to get colored?

        I don't see how this would solve anything but perhaps I've been staring at the problem for too long :-)

        -- Time flies when you don't know what you're doing

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1029417]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-19 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found