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