paul@91 | 1 | #!/usr/bin/env python |
paul@91 | 2 | |
paul@91 | 3 | """ |
paul@91 | 4 | HTML linking scheme. |
paul@91 | 5 | |
paul@91 | 6 | Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@91 | 7 | |
paul@91 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@91 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@91 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@91 | 11 | version. |
paul@91 | 12 | |
paul@91 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@91 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@91 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@91 | 16 | details. |
paul@91 | 17 | |
paul@91 | 18 | You should have received a copy of the GNU General Public License along with |
paul@91 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@91 | 20 | """ |
paul@91 | 21 | |
paul@91 | 22 | from moinformat.links.common import Linker |
paul@114 | 23 | from urllib import quote as _quote |
paul@91 | 24 | from urlparse import urlparse |
paul@91 | 25 | |
paul@114 | 26 | def quote(s): |
paul@114 | 27 | |
paul@114 | 28 | "Quote URL path 's', preserving path separators and fragment indicators." |
paul@114 | 29 | |
paul@114 | 30 | return "#".join(map(_quote, s.split("#", 1))) |
paul@114 | 31 | |
paul@91 | 32 | class HTMLLinker(Linker): |
paul@91 | 33 | |
paul@91 | 34 | "Translate Moin links into HTML links." |
paul@91 | 35 | |
paul@91 | 36 | name = "html" |
paul@91 | 37 | |
paul@91 | 38 | def get_top_level(self): |
paul@91 | 39 | |
paul@91 | 40 | "Return a relative link to the top level." |
paul@91 | 41 | |
paul@91 | 42 | levels = self.pagename.count("/") |
paul@91 | 43 | return "/".join([".."] * levels) |
paul@91 | 44 | |
paul@93 | 45 | def is_url(self, target): |
paul@91 | 46 | |
paul@93 | 47 | "Return whether the 'target' references a URL." |
paul@91 | 48 | |
paul@93 | 49 | scheme, host, path, params, query, fragment = urlparse(target) |
paul@93 | 50 | return scheme and target or None |
paul@91 | 51 | |
paul@91 | 52 | def normalise(self, path): |
paul@91 | 53 | |
paul@91 | 54 | "Return a normalised form of 'path'." |
paul@91 | 55 | |
paul@91 | 56 | return not path.endswith("/") and "%s/" % path or path |
paul@91 | 57 | |
paul@93 | 58 | def translate(self, target): |
paul@91 | 59 | |
paul@118 | 60 | """ |
paul@118 | 61 | Translate the 'target', returning a tuple containing the rewritten |
paul@118 | 62 | target string and a suitable default label. |
paul@118 | 63 | """ |
paul@91 | 64 | |
paul@93 | 65 | target = target.rstrip("/") |
paul@91 | 66 | |
paul@91 | 67 | # Sub-pages. |
paul@91 | 68 | |
paul@91 | 69 | if target.startswith("/"): |
paul@118 | 70 | return self.translate_subpage(target), None |
paul@91 | 71 | |
paul@91 | 72 | # Sibling (of ancestor) pages. |
paul@91 | 73 | |
paul@93 | 74 | if target.startswith("../"): |
paul@118 | 75 | return self.translate_relative(target), None |
paul@91 | 76 | |
paul@91 | 77 | # Attachment or interwiki link. |
paul@91 | 78 | |
paul@93 | 79 | rewritten = self.translate_qualified_link(target) |
paul@93 | 80 | if rewritten: |
paul@118 | 81 | return rewritten # includes label |
paul@91 | 82 | |
paul@91 | 83 | # Plain URL. |
paul@91 | 84 | |
paul@93 | 85 | rewritten = self.is_url(target) |
paul@93 | 86 | if rewritten: |
paul@118 | 87 | return rewritten, None |
paul@91 | 88 | |
paul@91 | 89 | # Top-level pages. |
paul@91 | 90 | |
paul@93 | 91 | top_level = self.get_top_level() |
paul@118 | 92 | return quote("%s%s" % (top_level and "%s/" % top_level or "", target)), None |
paul@91 | 93 | |
paul@93 | 94 | def translate_qualified_link(self, target): |
paul@91 | 95 | |
paul@91 | 96 | """ |
paul@118 | 97 | Translate a possible qualified link 'target', returning a tuple |
paul@118 | 98 | containing a rewritten target and a suitable default label. |
paul@118 | 99 | |
paul@118 | 100 | Return None if the link is not suitable. |
paul@91 | 101 | """ |
paul@91 | 102 | |
paul@91 | 103 | t = target.split(":", 1) |
paul@91 | 104 | if len(t) != 2: |
paul@93 | 105 | return None |
paul@91 | 106 | |
paul@91 | 107 | prefix, target = t |
paul@91 | 108 | |
paul@91 | 109 | # Attachment links. |
paul@91 | 110 | |
paul@91 | 111 | if prefix == "attachment": |
paul@118 | 112 | return self.translate_attachment(target), target |
paul@91 | 113 | |
paul@91 | 114 | # Interwiki links. |
paul@91 | 115 | |
paul@91 | 116 | url = self.mapping.get(prefix) |
paul@91 | 117 | if url: |
paul@118 | 118 | return self.translate_interwiki(url, target), target |
paul@91 | 119 | |
paul@93 | 120 | return None |
paul@91 | 121 | |
paul@91 | 122 | # Specific link translators. |
paul@91 | 123 | |
paul@93 | 124 | def translate_attachment(self, target): |
paul@91 | 125 | |
paul@93 | 126 | "Return a translation of the given attachment 'target'." |
paul@91 | 127 | |
paul@112 | 128 | return quote("%sattachments/%s/%s" % ( |
paul@112 | 129 | self.get_top_level(), self.pagename, target)) |
paul@91 | 130 | |
paul@93 | 131 | def translate_interwiki(self, url, target): |
paul@91 | 132 | |
paul@93 | 133 | "Return a translation of the given interwiki 'target'." |
paul@91 | 134 | |
paul@112 | 135 | return "%s%s" % (self.normalise(url), quote(target)) |
paul@91 | 136 | |
paul@93 | 137 | def translate_relative(self, target): |
paul@91 | 138 | |
paul@93 | 139 | "Return a translation of the given relative 'target'." |
paul@91 | 140 | |
paul@112 | 141 | return quote(target[len("../"):]) |
paul@91 | 142 | |
paul@93 | 143 | def translate_subpage(self, target): |
paul@91 | 144 | |
paul@93 | 145 | "Return a translation of the given subpage 'target'." |
paul@91 | 146 | |
paul@112 | 147 | return quote(".%s" % target) |
paul@91 | 148 | |
paul@91 | 149 | linker = HTMLLinker |
paul@91 | 150 | |
paul@91 | 151 | # vim: tabstop=4 expandtab shiftwidth=4 |