1.1 --- a/moinformat/links/common.py Mon Aug 13 15:53:53 2018 +0200
1.2 +++ b/moinformat/links/common.py Mon Aug 13 17:10:07 2018 +0200
1.3 @@ -23,14 +23,64 @@
1.4
1.5 "Translate Moin links into other forms."
1.6
1.7 - def __init__(self, pagename, mapping=None):
1.8 + def __init__(self, pagename, mapping=None, parameters=None):
1.9
1.10 """
1.11 - Initialise the linker with the 'pagename' and optional interwiki
1.12 - 'mapping'.
1.13 + Initialise the linker with the 'pagename', optional interwiki 'mapping'
1.14 + and 'parameters'.
1.15 """
1.16
1.17 self.pagename = pagename
1.18 self.mapping = mapping or {}
1.19 + self.parameters = parameters or {}
1.20 +
1.21 + self.root_pagename = self.parameters.get("root_pagename") or "FrontPage"
1.22 +
1.23 +def resolve(path, pagename, root_pagename):
1.24 +
1.25 + "Resolve 'path' relative to 'pagename'."
1.26 +
1.27 + # Omit the root pagename from the resolved path components.
1.28 +
1.29 + if pagename == root_pagename:
1.30 + parts = []
1.31 + else:
1.32 + parts = pagename.rstrip("/").split("/")
1.33 +
1.34 + t = path.split("/")
1.35 +
1.36 + first = True
1.37 +
1.38 + for p in t:
1.39 +
1.40 + # Handle replacement of the page with another.
1.41 +
1.42 + if p == ".":
1.43 + parts = []
1.44 +
1.45 + # Handle ascent in the page hierarchy.
1.46 +
1.47 + elif p == "..":
1.48 + if parts:
1.49 + parts.pop()
1.50 +
1.51 + # Any non-navigation element replaces the path at the start.
1.52 + # Otherwise, the path is extended.
1.53 + # Omit the root pagename from the resolved path components if it would
1.54 + # appear at the start.
1.55 +
1.56 + elif p:
1.57 + if first:
1.58 + if p == root_pagename:
1.59 + parts = []
1.60 + else:
1.61 + parts = [p]
1.62 + else:
1.63 + if parts or p != root_pagename:
1.64 + parts.append(p)
1.65 +
1.66 + first = False
1.67 +
1.68 + return "/".join(parts)
1.69
1.70 # vim: tabstop=4 expandtab shiftwidth=4