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 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 text = target.get_text() 133 type = target.get_type() 134 135 # Attachment links. 136 137 if type == "attachment": 138 return Link(self.translate_attachment(identifier), 139 identifier, target) 140 141 # Interwiki links. 142 143 url = self.mapping.get(type) 144 if url: 145 return Link(self.translate_interwiki(url, identifier), 146 identifier or type, 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("./%s/%s" % (self.attachments_dir, 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 s = self.replace_whitespace(s) 174 parts = s.split("#", 1) 175 176 if len(parts) > 1: 177 parts[1] = self.make_id(parts[1]) 178 179 return "#".join(map(quote, parts)) 180 181 # Whitespace conversion in pagenames. 182 183 def replace_whitespace(self, pagename): 184 185 "Map whitespace in 'pagename' to appropriate characters." 186 187 wsmap = self.metadata.get("whitespace", self.default_whitespace_map) 188 189 for old, new in wsmap: 190 pagename = pagename.replace(old, new) 191 192 return pagename 193 194 # Identifier encoding. 195 196 def make_id(self, s): 197 198 "Make a suitable identifier for HTML element identification." 199 200 # NOTE: This reproduces the Moin algorithm for compatibility. 201 # NOTE: There may well be improvements possible, possibly by replacing plus 202 # NOTE: with something less cumbersome, even though plus may be unusual in 203 # NOTE: things like headings, anyway. 204 205 # The desired output is the following pattern: 206 207 # [A-Za-z][-_:.A-Za-z0-9]* 208 209 # The Python UTF-7 encoder preserves symbols and it encodes + as +- with an 210 # output range as follows (in addition to A-Za-z0-9): 211 212 # -_:.%+ !"#$&\'()*,/;<=>?@[]^`{|} 213 214 # The quote_plus function converts space to plus, preserves -_:. and encodes 215 # all other symbols (including original occurrences of plus and percent) and 216 # non-alphanumeric (ASCII) characters using percent encoding. 217 218 # With colons preserved, the resulting output is in the following range 219 # (in addition to A-Za-z0-9): 220 221 # -_:.%+ 222 223 # Percent will only occur as an encoding prefix. Plus will only occur as a 224 # replacement for space. 225 226 # Combining quote_plus and UTF-7 gives the following range (in addition to 227 # A-Za-z0-9): 228 229 # -_:.%+ 230 231 # Examples: 232 233 # UTF-7 quote_plus replace percent and plus 234 # : -> : -> : -> : 235 # - -> - -> - -> - 236 # . -> . -> . -> . 237 # % -> % -> %25 -> .25 238 # + -> +- -> %2B- -> .2B- 239 # _ -> _ -> _ -> _ 240 # space -> space -> + -> _ 241 242 # See: RFC2152 - UTF-7 A Mail-Safe Transformation Format of Unicode 243 244 quoted = quote_plus(s.encode("utf-7"), ":").replace("%", ".").replace("+", "_") 245 246 # Ensure that the identifier starts with an alphabetical character. 247 248 if not quoted[0].isalpha(): 249 return "A%s" % quoted 250 else: 251 return quoted 252 253 linker = HTMLLinker 254 255 # vim: tabstop=4 expandtab shiftwidth=4