Author Topic: If-Then structures in filters  (Read 1525 times)

hpguru

  • Sr. Member
  • ****
  • Posts: 257
    • ICQ Messenger -
    • AOL Instant Messenger -
    • Yahoo Instant Messenger -
    • View Profile
    • http://lightning.prohosting.com/~hpguru/
    • Email
If-Then structures in filters
« on: August 21, 2002, 06:08:41 PM »
Has anyone experimented with IF-THEN logical stuctures in filters. I realize that well written filters may contain IF-THEN structures that perhaps were not thought of as such at the time of creation but has anyone created any filters where they had implied IF-THEN or IF-THEN-ELSE structures in mind? I've not seen anybody explore this idea but it might greatly simplify the process of filter creation by the use of filter templates.

Here is a comment viewer filter I wrote with an implied IF-THEN-ELSE structure.


Name = "Comment Viewer"
Active = TRUE
Limit = 4
Match = "(<!--$SET(1=<small><!--))|"
        "(-->$SET(1=--></small>))"
Replace = "1"


Written out the match logic is

IF "<!--" is matched
THEN set the variable "1" equal to "<small><!--"
ELSE IF "-->" is matched
THEN set the variable "1" equal to "--></small>"

altosax's "Add </head> tag when missing" filter uses an implied IF-THEN-ELSE structure.


Name = "Add </head> tag when missing - altosax v2"
Active = TRUE
Multi = TRUE
URL = "$TYPE(htm)"
Limit = 7
Match = "(</head>)1$STOP()|(<body)2$SET(1=</head>)"
Replace = "12$STOP()"


IF "</head>" is matched
THEN store "</head>" in variable "1" and stop using filter here
ELSE IF "<body" is matched
THEN store "<body" in variable "2" and set variable "1" equal to "</head>"

The filters are just simple examples but any ideas on the concept and how it might be extended to more complex structures?

Facing each other,
a thousand miles apart.

Edited by - hpguru on 21 Aug 2002  19:12:26
Facing each other,
a thousand miles apart.

altosax

  • Sr. Member
  • ****
  • Posts: 328
    • ICQ Messenger -
    • AOL Instant Messenger -
    • Yahoo Instant Messenger -
    • View Profile
    • http://
    • Email
If-Then structures in filters
« Reply #1 on: August 21, 2002, 06:24:57 PM »
comparing both filters, they seem to have the same structure.
for example, i could modify the second this way:

Match = "</head>$SET(1=</head>)$STOP()|<body$SET(1=</head>)$SET(2=<body)"

this means that can exist a meta-structure to write an if-then-else check:

IF = first match
THEN = first SET command
ELSE = |
IF = second match
THEN = second SET command

this require the use of 2 matching expression separated by an OR function, each using a SET command.
btw, this is still something to have understood well and developed, mine are just the first impressions.

regards,
altosax.

Edited by - altosax on 21 Aug 2002  19:25:35
 

Jor

  • Sr. Member
  • ****
  • Posts: 421
    • ICQ Messenger - 10401286
    • AOL Instant Messenger - jor otf
    • Yahoo Instant Messenger - jor_otf
    • View Profile
    • http://members.outpost10f.com/~jor/
    • Email
If-Then structures in filters
« Reply #2 on: August 21, 2002, 07:10:14 PM »
This is funny

I had realized these filters work that way, and actually have just posted a question about it, asking for assistance, just before reading this topic

GMTA!

http://topic.asp?ARCHIVE=&TOPIC_ID=802



Edited by - Jor on 21 Aug 2002  20:20:25
 

hpguru

  • Sr. Member
  • ****
  • Posts: 257
    • ICQ Messenger -
    • AOL Instant Messenger -
    • Yahoo Instant Messenger -
    • View Profile
    • http://lightning.prohosting.com/~hpguru/
    • Email
If-Then structures in filters
« Reply #3 on: August 21, 2002, 07:43:10 PM »
LOL! So what do you think? Is the idea worth more exploration?

Facing each other,
a thousand miles apart.
Facing each other,
a thousand miles apart.

Jor

  • Sr. Member
  • ****
  • Posts: 421
    • ICQ Messenger - 10401286
    • AOL Instant Messenger - jor otf
    • Yahoo Instant Messenger - jor_otf
    • View Profile
    • http://members.outpost10f.com/~jor/
    • Email
If-Then structures in filters
« Reply #4 on: August 21, 2002, 08:54:40 PM »
It's definately a powerful technique for writing filters -- the IF-THEN method allows for some really complex filters.

Maybe we should try to write some tutorial for the P-FAQ?

 
 

hpguru

  • Sr. Member
  • ****
  • Posts: 257
    • ICQ Messenger -
    • AOL Instant Messenger -
    • Yahoo Instant Messenger -
    • View Profile
    • http://lightning.prohosting.com/~hpguru/
    • Email
If-Then structures in filters
« Reply #5 on: August 21, 2002, 10:36:09 PM »
Using this line of thinking I created the following filter to remove image borders. It's a definite improvement over the one I included in my config.


Name = "Remove Image Borders v2"
Active = TRUE
Multi = TRUE
URL = "$TYPE(htm)"
Bounds = "<imgs*>"
Limit = 300
Match = "((border="0">)1$SET(2=>))"
        "|"
        "((border="0"s)1 2)"
        "|"
        "((border=$AV([#1:10])>)$SET(1=border="0")$SET(2=>))"
        "|"
        "((border=$AV([#1:10])s)$SET(1=border="0" ) 2)"
        "|"
        "(>)$SET(1= border="0")$SET(2=>)"
Replace = "12"




Facing each other,
a thousand miles apart.

Edited by - hpguru on 21 Aug 2002  23:38:36
Facing each other,
a thousand miles apart.

altosax

  • Sr. Member
  • ****
  • Posts: 328
    • ICQ Messenger -
    • AOL Instant Messenger -
    • Yahoo Instant Messenger -
    • View Profile
    • http://
    • Email
If-Then structures in filters
« Reply #6 on: August 22, 2002, 10:29:09 AM »
i think this could work better:

Name = "Remove Image Borders v3"
Active = TRUE
Multi = TRUE
URL = "$TYPE(htm)"
Bounds = "<imgs*>"
Limit = 300
Match = "1border=$AV(*)2
Replace = "1border="0"2"

because the default border, when missing, depends from browser, if you have a browser that use a default border=0, also this could work:

Replace = "12"

i think this kind of filters, where you simply have to remove some stuff, don't need an if-then-else structure.

instead you need an if-then-else check when you have to match different code and replace it with different replacement. for example this is the case of your filter:

Match = "(<!--$SET(1=<small><!--))|"
        "(-->$SET(1=--></small>))"
Replace = "1"

note for jor: because you have proposed first to write a tutorial about this tricky way to write filters, are you writing it? if not i could write it myself. let me know.

regards,
altosax.

 
 

Jor

  • Sr. Member
  • ****
  • Posts: 421
    • ICQ Messenger - 10401286
    • AOL Instant Messenger - jor otf
    • Yahoo Instant Messenger - jor_otf
    • View Profile
    • http://members.outpost10f.com/~jor/
    • Email
If-Then structures in filters
« Reply #7 on: August 22, 2002, 01:07:24 PM »
altosax: I haven't found the time for it yet, so, go ahead

 
 

hpguru

  • Sr. Member
  • ****
  • Posts: 257
    • ICQ Messenger -
    • AOL Instant Messenger -
    • Yahoo Instant Messenger -
    • View Profile
    • http://lightning.prohosting.com/~hpguru/
    • Email
If-Then structures in filters
« Reply #8 on: August 22, 2002, 04:23:39 PM »
Hi Altosax,

I wish it were that simple. The default value for the border attribute is "0" in most browsers but IE will nonetheless draw a border if the image is a link. So I need to match the case that the border attribute is missing. How with accuracy do we match what is not there? Just like in your head tag filter, we match the case(s) that it is there first and having failed that the only other alternative is that it is missing so we add it.

BTW I have already found a much much faster way to get rid of image borders but for the sake of learning I need some help with a problem I posed about this filter in the Cosmetic board.

I think an FAQ about this method is a great idea!

Facing each other,
a thousand miles apart.
Facing each other,
a thousand miles apart.