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