1 #!/usr/bin/env python 2 3 """ 4 HTML linking scheme. 5 6 Copyright (C) 2018, 2019, 2022 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 from moinformat.links.common import Link, Linker, resolve 23 from urllib import quote, quote_plus 24 25 class HTMLLinker(Linker): 26 27 "Translate Moin links into HTML links." 28 29 name = "html" 30 31 def get_top_level(self): 32 33 "Return a relative link to the top level." 34 35 # The root page is at the top level already. 36 37 pagename = self.metadata.get("pagename", "") 38 39 if pagename == self.root_pagename: 40 return "" 41 42 # Siblings of the root page are actually one level below. 43 44 levels = pagename.count("/") + 1 45 return "/".join([".."] * levels) 46 47 def normalise(self, path): 48 49 "Return a normalised form of 'path'." 50 51 return not path.endswith("/") and "%s/" % path or path 52 53 def translate(self, target): 54 55 """ 56 Translate the 'target', returning a link object containing the rewritten 57 target and a suitable default label. 58 """ 59 60 identifier = target.get_identifier() 61 text = target.get_text() 62 type = target.get_type() 63 64 # Fragments. 65 66 if type == "fragment": 67 return Link(self.quote(text), identifier, target) 68 69 # Sub-pages. Remove the leading slash for the label. 70 71 if type == "sub-page": 72 return Link(self.translate_pagename(text), identifier, target) 73 74 # Sibling (of ancestor) pages. 75 76 if type == "sibling-page": 77 return Link(self.translate_pagename(text), identifier, target) 78 79 # Plain URL. 80 81 if type == "url": 82 return Link(text, identifier, target) 83 84 # Top-level pages. 85 86 if type == "page": 87 return Link(self.translate_pagename(text), identifier, target) 88 89 # Attachment or interwiki link. 90 91 return self.translate_qualified_link(target) 92 93 def translate_pagename(self, text): 94 95 "Translate the pagename in 'text'." 96 97 # Obtain the target pagename and the fragment. 98 # Split the pagename into path components. 99 100 t = text.split("#", 1) 101 102 # Determine the actual pagename referenced. 103 # Replace the root pagename if it appears. 104 105 pagename = self.metadata.get("pagename", "") 106 resolved = resolve(t[0], pagename, self.root_pagename) 107 108 # Rewrite the target using a relative link to the top level and then the 109 # resolved pagename. 110 111 top_level = self.get_top_level() 112 113 # Support an explicit "DocumentIndex" filename for file browsing. 114 115 document_index = self.metadata.get("document_index") 116 117 t[0] = "%s%s%s" % (top_level and "%s/" % top_level or "", resolved, 118 document_index and "/%s" % document_index or "") 119 120 return self.quote("#".join(t)) 121 122 def translate_qualified_link(self, target): 123 124 """ 125 Translate a possible qualified link 'target', returning a link object 126 retaining a rewritten target and a suitable default label. 127 128 Return None if the link is not suitable. 129 """ 130 131 identifier = target.get_identifier() 132 pagename = target.get_pagename() 133 text = target.get_text() 134 type = target.get_type() 135 136 # Attachment links. 137 138 if type == "attachment": 139 return Link(self.translate_attachment(identifier, pagename), 140 identifier, target) 141 142 # Interwiki links. 143 144 url = self.mapping.get(type) 145 if url: 146 return Link(self.translate_interwiki(url, identifier), 147 identifier or type, target) 148 149 return None 150 151 # Specific link translators. 152 153 def translate_attachment(self, target, pagename): 154 155 """ 156 Return a translation of the given attachment 'target' associated with 157 the given 'pagename'. 158 """ 159 160 common_attachments = self.metadata.get("common_attachments") 161 top_level = self.get_top_level() 162 163 return self.quote("%s%s/%s%s" % (top_level and "%s/" % top_level or "", 164 self.attachments_dir, 165 not common_attachments and "%s/" % pagename or "", 166 target)) 167 168 def translate_interwiki(self, url, target): 169 170 "Return a translation of the given interwiki 'target'." 171 172 return "%s%s" % (self.normalise(url), self.quote(target)) 173 174 # Path encoding. 175 176 def quote(self, s): 177 178 """ 179 Quote URL path 's', preserving path separators and fragment indicators, 180 encoding fragment identifiers. 181 """ 182 183 s = self.replace_whitespace(s) 184 parts = s.split("#", 1) 185 186 if len(parts) > 1: 187 parts[1] = self.make_id(parts[1]) 188 189 return "#".join(map(quote, parts)) 190 191 # Whitespace conversion in pagenames. 192 193 def replace_whitespace(self, pagename): 194 195 "Map whitespace in 'pagename' to appropriate characters." 196 197 wsmap = self.metadata.get("whitespace", self.default_whitespace_map) 198 199 for old, new in wsmap: 200 pagename = pagename.replace(old, new) 201 202 return pagename 203 204 # Identifier encoding. 205 206 def make_id(self, s): 207 208 "Make a suitable identifier for HTML element identification." 209 210 # NOTE: This reproduces the Moin algorithm for compatibility. 211 # NOTE: There may well be improvements possible, possibly by replacing plus 212 # NOTE: with something less cumbersome, even though plus may be unusual in 213 # NOTE: things like headings, anyway. 214 215 # The desired output is the following pattern: 216 217 # [A-Za-z][-_:.A-Za-z0-9]* 218 219 # The Python UTF-7 encoder preserves symbols and it encodes + as +- with an 220 # output range as follows (in addition to A-Za-z0-9): 221 222 # -_:.%+ !"#$&\'()*,/;<=>?@[]^`{|} 223 224 # The quote_plus function converts space to plus, preserves -_:. and encodes 225 # all other symbols (including original occurrences of plus and percent) and 226 # non-alphanumeric (ASCII) characters using percent encoding. 227 228 # With colons preserved, the resulting output is in the following range 229 # (in addition to A-Za-z0-9): 230 231 # -_:.%+ 232 233 # Percent will only occur as an encoding prefix. Plus will only occur as a 234 # replacement for space. 235 236 # Combining quote_plus and UTF-7 gives the following range (in addition to 237 # A-Za-z0-9): 238 239 # -_:.%+ 240 241 # Examples: 242 243 # UTF-7 quote_plus replace percent and plus 244 # : -> : -> : -> : 245 # - -> - -> - -> - 246 # . -> . -> . -> . 247 # % -> % -> %25 -> .25 248 # + -> +- -> %2B- -> .2B- 249 # _ -> _ -> _ -> _ 250 # space -> space -> + -> _ 251 252 # See: RFC2152 - UTF-7 A Mail-Safe Transformation Format of Unicode 253 254 quoted = quote_plus(s.encode("utf-7"), ":").replace("%", ".").replace("+", "_") 255 256 # Ensure that the identifier starts with an alphabetical character. 257 258 if not quoted[0].isalpha(): 259 return "A%s" % quoted 260 else: 261 return quoted 262 263 linker = HTMLLinker 264 265 # vim: tabstop=4 expandtab shiftwidth=4