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