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