identify path in url

I am looking to make this code pick up the URL of the page and if it contains the word in the link then apply a class to the a href:

<div  id="topbar">
  <ul>
    <li><a href="/"><span>Projects</span></a></li>
<li><a class="active" href="/mars"><span>MARS</span></a></li>
<li><a href="/mplus"><span>MPlus</span></a></li>
<li><a href="/mcgrath"><span>Mcgrath</span></a></li>
<li><a href="/intranet"><span>Intranet</span></a></li>
<li><a href="/automate"><span>Automate</span></a></li>
<li><a href="/tret"><span>TRET</span></a></li>
<li><a href="/oxygen"><span>Oxygen</span></a></li>
  </ul>
</div>

You can see for example that I took this code from the /mars directory therefore it has a class attached. The class simply highlights the link to identify where you are.

It is important that its not an absolute match, as there are many levels below /mars and many pages too.

Can anyone shed some light on how to do this?

Thanks!
LVL 12
mmc98dl1Asked:
Who is Participating?
 
mish33Commented:
Create selectedstyle.py:

from mod_python import apache
import re
def outputfilter(filter):
    for line in filter.readline():
        if 'http://' in line:
           for uri in re.findall(' href="([^"]+)", line):
             if apache.request.unparsed_uri == uri:
               href = ' href="'+uri
               line = line.replace(href, ' style="selected"'+href)
               break
        filter.write(line)
    filter.close()

Install it:
  PythonOutputFilter selectedstyle SELECTED
  AddOutputFilter SELECTED .html
0
 
ramromconsultant Commented:
I can't. I don't understand the question!
0
 
mmc98dl1Author Commented:
ok so I need to identify when the user is on a page that resides under

url.com/mars
url.com/mplus
url.com/intranet etc etc

if they are under url.com/mars insert class="active" into the appropriate place in the HTML.

better?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ramromconsultant Commented:
Is this in a CGI script?
0
 
mmc98dl1Author Commented:
I am using mod_python
0
 
mmc98dl1Author Commented:
python 2.3 on apache
0
 
peprCommented:
If I understand you correctly, here is the fragment that does the substitution using the regular expression. Try it first as the script  stored in the file file a.py
-----------------------------------------------------
import re

source = '''\
<div  id="topbar">
  <ul>
    <li><a href="/"><span>Projects</span></a></li>
<li><a href="something/mars/somethingelse"><span>MARS</span></a></li>
<li><a href="/mplus"><span>MPlus</span></a></li>
<li><a href="/mcgrath"><span>Mcgrath</span></a></li>
<li><a href="/intranet"><span>Intranet</span></a></li>
<li><a href="/automate"><span>Automate</span></a></li>
<li><a href="/tret"><span>TRET</span></a></li>
<li><a href="/oxygen"><span>Oxygen</span></a></li>
  </ul>
</div>'''

pat = 'mars'
highlight = 'class="active"'

pattern = r'<a\s+(href=".*?\b' + pat + r'\b.*?")'
repl = '<a ' + highlight + r' \1'

result = re.sub(pattern, repl, source)

print result
-----------------------------------------------------

The result looks like
-----------------------------------------------------
C:\tmp\aa>python a.py
<div  id="topbar">
  <ul>
    <li><a href="/"><span>Projects</span></a></li>
<li><a class="active" href="something/mars/somethingelse"><span>MARS</span></a></li>
<li><a href="/mplus"><span>MPlus</span></a></li>
<li><a href="/mcgrath"><span>Mcgrath</span></a></li>
<li><a href="/intranet"><span>Intranet</span></a></li>
<li><a href="/automate"><span>Automate</span></a></li>
<li><a href="/tret"><span>TRET</span></a></li>
<li><a href="/oxygen"><span>Oxygen</span></a></li>
  </ul>
</div>
-----------------------------------------------------

Notice that the pat containts your identification, without slash. Notice that the fragment was modified to demonstrate that the identification is the word inside the url. The pattern must be put together every time when you change the pat.
You can define a function for building the pattern from the identification if used on more places. You can also compile the regular expression if required.
0
 
rjkimbleCommented:
You may want to consider using the string startswith() function.
0
 
peprCommented:
"You may want to consider using the string startswith() function." -- in my opinion, it is hardly possible in this case. Or the solution would not look natural.
0
 
mmc98dl1Author Commented:
I dont see how this works on a dynamic path - whats the variable to get the address from the browser?
0
 
rjkimbleCommented:
From the information given, I don't know enough about how the pages are generated. I assume that there's some code generating the hrefs in the list. Assuming that the page also has access to some variable holding the URL, it's relatively easy to strip out the path portion (everything after http://url.com or whatever). So assuming we have access to variables containing the href and path, say href and path, then it's easy to test to see whether or not the current href meets his criteria using startswith():

for href in list_of_hrefs:
    if path.startswith(href):
        # add the active class
    ....continue on with the rest of the script

That's all I was suggesting. Of course, if my assumptions are wrong, then this is not a solution.

After perusing the mod_python documentation, it appears to me that the path is contained in the Request object's uri member, so that value should be readily available.
0
 
mmc98dl1Author Commented:
ok so the code I supplied is static HTML, so for each line in this code I need a piece of python that will

1) Compare the current page uri to the href in the current line (lets just say this is a variable)
2) if the compare is true enter "style='selected'"

In psuedo code:

<li><a href="/" #if request.uri contains "/"{print "style='selected'"}#><span>Projects</span></a></li>
<li><a href="/" #if request.uri contains "/mars/"{print "style='selected'"}#><span>Mars</span></a></li>
<li><a href="/" #if request.uri contains "/mplus/"{print "style='selected'"}#><span>Mplus</span></a></li>
<li><a href="/" #if request.uri contains "/mcgrath/"{print "style='selected'"}#><span>Mcgrath</span></a></li>

etc.
This is how I would go about it in php or coldfusion, but I havent a clue about python.
Is this clearer?
0
 
peprCommented:
import re

# Create the list of lines to simulate the input consumed by lines.
lines = '''\
<li><a href="http://url.com/"><span>Projects</span></a></li>
<li><a href="http://url.com/something/mars/somethingelse"><span>MARS</span></a></li>
<li><a href="http://url.com/mplus/"><span>MPlus</span></a></li>
<li><a href="http://url.com/mcgrath/"><span>Mcgrath</span></a></li>
<li><a href="http://url.com/intranet/"><span>Intranet</span></a></li>
<li><a href="http://url.com/automate/"><span>Automate</span></a></li>
<li><a href="http://url.com/tret/"><span>TRET</span></a></li>
<li><a href="http://url.com/oxygen/"><span>Oxygen</span></a></li>
'''.split('\n')  # !!! notice here -- splited to lines without line endings


# The following is your function that decorates your lines.

def decoratedLine(line, pat):
    pattern = r'<a\s+(href=".*?' + pat + r'.*?")'
    repl = '<a ' + "style='selected'" + r' \1'
    return re.sub(pattern, repl, line)


# Simulate the marking only the requested lines.
for line in lines:
    print decoratedLine(line, '/mars/')

print '-' * 70
for line in lines:
    print decoratedLine(line, '/oxygen/')

# However, use better pattern than "/" for the Projects.
0
 
mmc98dl1Author Commented:
thanks pepr - can you tell me how this will dynamically pick up the url the user has in their browser window?
0
 
ramromconsultant Commented:
That is a mod_python question - how does mod_python make the request available?
0
 
mmc98dl1Author Commented:
mish33,

This looks like it may be what I am after. How do I go about installing this, I dont quite understand your instructions?

The HTML code I have is in a file called site_header.cs This is included by the application I am customising. The app for reference is trac - http://trac.edgewall.com

0
 
mish33Commented:
See http://www.modpython.org/live/current/doc-html/pyapi-filter.html

You need to put it in apache config file (mod_python section):
  PythonOutputFilter selectedstyle SELECTED
  AddOutputFilter SELECTED .cs
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.