1 #!/usr/bin/env python 2 3 """ 4 Common linking scheme functionality. 5 6 Copyright (C) 2018, 2019 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 Link: 23 24 "A link abstraction." 25 26 def __init__(self, target, label, type): 27 28 "Initialise the link with the given 'target', 'label' and 'type'." 29 30 self.target = target 31 self.label = label 32 self.type = type 33 34 def get_target(self): 35 return self.target 36 37 def get_label(self): 38 return self.label or self.target 39 40 def get_type(self): 41 return self.type 42 43 class Linker: 44 45 "Translate Moin links into other forms." 46 47 # Pagename whitespace conversion, configured using the "whitespace" metadata 48 # setting. 49 50 default_whitespace_map = [ 51 (" ", "_"), 52 ("\n", "_"), 53 ] 54 55 def __init__(self, metadata): 56 57 "Initialise the linker with the 'metadata'." 58 59 self.metadata = metadata 60 61 # Obtain essential metadata. 62 63 self.mapping = metadata.get("mapping", {}) 64 self.root_pagename = metadata.get("root_pagename", "FrontPage") 65 self.attachments_dir = metadata.get("attachments", "attachments") 66 67 def resolve(path, pagename, root_pagename): 68 69 "Resolve 'path' relative to 'pagename'." 70 71 # Omit the root pagename from the resolved path components. 72 73 if pagename == root_pagename: 74 parts = [] 75 else: 76 parts = pagename.rstrip("/").split("/") 77 78 t = path.split("/") 79 80 first = True 81 82 for p in t: 83 84 # Handle replacement of the page with another. 85 86 if p == ".": 87 parts = [] 88 89 # Handle ascent in the page hierarchy. 90 91 elif p == "..": 92 if parts: 93 parts.pop() 94 95 # Any non-navigation element replaces the path at the start. 96 # Otherwise, the path is extended. 97 # Omit the root pagename from the resolved path components if it would 98 # appear at the start. 99 100 elif p: 101 if first: 102 if p == root_pagename: 103 parts = [] 104 else: 105 parts = [p] 106 else: 107 if parts or p != root_pagename: 108 parts.append(p) 109 110 first = False 111 112 return "/".join(parts) 113 114 # vim: tabstop=4 expandtab shiftwidth=4