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