Solved

identify path in url

Posted on 2006-06-27
17
373 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
 
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 28

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 28

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 28

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 250 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now