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