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