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