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