1 #!/usr/bin/env python 2 3 """ 4 Common linking scheme functionality. 5 6 Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 class Linker: 23 24 "Translate Moin links into other forms." 25 26 def __init__(self, pagename, mapping=None, parameters=None): 27 28 """ 29 Initialise the linker with the 'pagename', optional interwiki 'mapping' 30 and 'parameters'. 31 """ 32 33 self.pagename = pagename 34 self.mapping = mapping or {} 35 self.parameters = parameters or {} 36 37 self.root_pagename = self.parameters.get("root_pagename") or "FrontPage" 38 39 def resolve(path, pagename, root_pagename): 40 41 "Resolve 'path' relative to 'pagename'." 42 43 # Omit the root pagename from the resolved path components. 44 45 if pagename == root_pagename: 46 parts = [] 47 else: 48 parts = pagename.rstrip("/").split("/") 49 50 t = path.split("/") 51 52 first = True 53 54 for p in t: 55 56 # Handle replacement of the page with another. 57 58 if p == ".": 59 parts = [] 60 61 # Handle ascent in the page hierarchy. 62 63 elif p == "..": 64 if parts: 65 parts.pop() 66 67 # Any non-navigation element replaces the path at the start. 68 # Otherwise, the path is extended. 69 # Omit the root pagename from the resolved path components if it would 70 # appear at the start. 71 72 elif p: 73 if first: 74 if p == root_pagename: 75 parts = [] 76 else: 77 parts = [p] 78 else: 79 if parts or p != root_pagename: 80 parts.append(p) 81 82 first = False 83 84 return "/".join(parts) 85 86 # vim: tabstop=4 expandtab shiftwidth=4