Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

identify path in url

Posted on 2006-06-27
17
Medium Priority
?
383 Views
Last Modified: 2010-04-16
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!
0
Comment
Question by:mmc98dl1
  • 7
  • 3
  • 3
  • +2
17 Comments
 
LVL 17

Expert Comment

by:ramrom
ID: 16997337
I can't. I don't understand the question!
0
 
LVL 12

Author Comment

by:mmc98dl1
ID: 16997369
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
 
LVL 17

Expert Comment

by:ramrom
ID: 16997609
Is this in a CGI script?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 12

Author Comment

by:mmc98dl1
ID: 16997659
I am using mod_python
0
 
LVL 12

Author Comment

by:mmc98dl1
ID: 16997661
python 2.3 on apache
0
 
LVL 29

Expert Comment

by:pepr
ID: 17007734
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
 
LVL 9

Expert Comment

by:rjkimble
ID: 17066759
You may want to consider using the string startswith() function.
0
 
LVL 29

Expert Comment

by:pepr
ID: 17079253
"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
 
LVL 12

Author Comment

by:mmc98dl1
ID: 17079310
I dont see how this works on a dynamic path - whats the variable to get the address from the browser?
0
 
LVL 9

Expert Comment

by:rjkimble
ID: 17081225
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
 
LVL 12

Author Comment

by:mmc98dl1
ID: 17086668
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
 
LVL 29

Expert Comment

by:pepr
ID: 17088171
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
 
LVL 12

Author Comment

by:mmc98dl1
ID: 17088201
thanks pepr - can you tell me how this will dynamically pick up the url the user has in their browser window?
0
 
LVL 17

Expert Comment

by:ramrom
ID: 17093377
That is a mod_python question - how does mod_python make the request available?
0
 
LVL 15

Accepted Solution

by:
mish33 earned 1000 total points
ID: 17426043
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
 
LVL 12

Author Comment

by:mmc98dl1
ID: 17426599
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
 
LVL 15

Expert Comment

by:mish33
ID: 17426965
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question