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 as _quote 24 from urlparse import urlparse 25 26 def quote(s): 27 28 "Quote URL path 's', preserving path separators and fragment indicators." 29 30 return "#".join(map(_quote, s.split("#", 1))) 31 32 class HTMLLinker(Linker): 33 34 "Translate Moin links into HTML links." 35 36 name = "html" 37 38 def get_top_level(self): 39 40 "Return a relative link to the top level." 41 42 levels = self.pagename.count("/") 43 return "/".join([".."] * levels) 44 45 def is_url(self, target): 46 47 "Return whether the 'target' references a URL." 48 49 scheme, host, path, params, query, fragment = urlparse(target) 50 return scheme and target or None 51 52 def normalise(self, path): 53 54 "Return a normalised form of 'path'." 55 56 return not path.endswith("/") and "%s/" % path or path 57 58 def translate(self, target): 59 60 "Translate the 'target' and return the rewritten string." 61 62 target = target.rstrip("/") 63 64 # Sub-pages. 65 66 if target.startswith("/"): 67 return self.translate_subpage(target) 68 69 # Sibling (of ancestor) pages. 70 71 if target.startswith("../"): 72 return self.translate_relative(target) 73 74 # Attachment or interwiki link. 75 76 rewritten = self.translate_qualified_link(target) 77 if rewritten: 78 return rewritten 79 80 # Plain URL. 81 82 rewritten = self.is_url(target) 83 if rewritten: 84 return rewritten 85 86 # Top-level pages. 87 88 top_level = self.get_top_level() 89 return quote("%s%s" % (top_level and "%s/" % top_level or "", target)) 90 91 def translate_qualified_link(self, target): 92 93 """ 94 Translate a possible qualified link 'target', returning a rewritten 95 target or None if the link is not suitable. 96 """ 97 98 t = target.split(":", 1) 99 if len(t) != 2: 100 return None 101 102 prefix, target = t 103 104 # Attachment links. 105 106 if prefix == "attachment": 107 return self.translate_attachment(target) 108 109 # Interwiki links. 110 111 url = self.mapping.get(prefix) 112 if url: 113 return self.translate_interwiki(url, target) 114 115 return None 116 117 # Specific link translators. 118 119 def translate_attachment(self, target): 120 121 "Return a translation of the given attachment 'target'." 122 123 return quote("%sattachments/%s/%s" % ( 124 self.get_top_level(), self.pagename, target)) 125 126 def translate_interwiki(self, url, target): 127 128 "Return a translation of the given interwiki 'target'." 129 130 return "%s%s" % (self.normalise(url), quote(target)) 131 132 def translate_relative(self, target): 133 134 "Return a translation of the given relative 'target'." 135 136 return quote(target[len("../"):]) 137 138 def translate_subpage(self, target): 139 140 "Return a translation of the given subpage 'target'." 141 142 return quote(".%s" % target) 143 144 linker = HTMLLinker 145 146 # vim: tabstop=4 expandtab shiftwidth=4