Mercurial > hg > config
annotate python/relativelink.py @ 887:79eeb5390c8a
hgignore
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sat, 24 Apr 2021 14:27:57 -0700 |
parents | 09c10061838c |
children |
rev | line source |
---|---|
142
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 def link(path_info, path=None): |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 segments = path_info.split('/') |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 if segments[0]: |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 segments.insert(0, '') |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 if len(segments) <3: |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 if not path or path == '/': |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 return './' |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 return path |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 nlayers = len(segments[2:]) |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 string = '../' * nlayers |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 if not path or path == '/': |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 return string |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 return string + path |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 if __name__ == '__main__': |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 import sys |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 assert len(sys.argv[1:]) == 2, "need two arguments" |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 path_info, path = sys.argv[1:] |
09c10061838c
add a program to create relative links
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 print link(path_info, path) |