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