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 """ 61 Translate the 'target', returning a tuple containing the rewritten 62 target string and a suitable default label. 63 """ 64 65 target = target.rstrip("/") 66 67 # Sub-pages. 68 69 if target.startswith("/"): 70 return self.translate_subpage(target), None 71 72 # Sibling (of ancestor) pages. 73 74 if target.startswith("../"): 75 return self.translate_relative(target), None 76 77 # Attachment or interwiki link. 78 79 rewritten = self.translate_qualified_link(target) 80 if rewritten: 81 return rewritten # includes label 82 83 # Plain URL. 84 85 rewritten = self.is_url(target) 86 if rewritten: 87 return rewritten, None 88 89 # Top-level pages. 90 91 top_level = self.get_top_level() 92 return quote("%s%s" % (top_level and "%s/" % top_level or "", target)), None 93 94 def translate_qualified_link(self, target): 95 96 """ 97 Translate a possible qualified link 'target', returning a tuple 98 containing a rewritten target and a suitable default label. 99 100 Return None if the link is not suitable. 101 """ 102 103 t = target.split(":", 1) 104 if len(t) != 2: 105 return None 106 107 prefix, target = t 108 109 # Attachment links. 110 111 if prefix == "attachment": 112 return self.translate_attachment(target), target 113 114 # Interwiki links. 115 116 url = self.mapping.get(prefix) 117 if url: 118 return self.translate_interwiki(url, target), target 119 120 return None 121 122 # Specific link translators. 123 124 def translate_attachment(self, target): 125 126 "Return a translation of the given attachment 'target'." 127 128 return quote("%sattachments/%s/%s" % ( 129 self.get_top_level(), self.pagename, target)) 130 131 def translate_interwiki(self, url, target): 132 133 "Return a translation of the given interwiki 'target'." 134 135 return "%s%s" % (self.normalise(url), quote(target)) 136 137 def translate_relative(self, target): 138 139 "Return a translation of the given relative 'target'." 140 141 return quote(target[len("../"):]) 142 143 def translate_subpage(self, target): 144 145 "Return a translation of the given subpage 'target'." 146 147 return quote(".%s" % target) 148 149 linker = HTMLLinker 150 151 # vim: tabstop=4 expandtab shiftwidth=4