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