paul@91 | 1 | #!/usr/bin/env python |
paul@91 | 2 | |
paul@91 | 3 | """ |
paul@91 | 4 | Common linking scheme functionality. |
paul@91 | 5 | |
paul@193 | 6 | Copyright (C) 2018, 2019 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@214 | 22 | class Link: |
paul@214 | 23 | |
paul@214 | 24 | "A link abstraction." |
paul@214 | 25 | |
paul@222 | 26 | def __init__(self, target, label, link_target=None): |
paul@214 | 27 | |
paul@222 | 28 | """ |
paul@222 | 29 | Initialise the link with the given 'target' and 'label' and |
paul@222 | 30 | 'link_target' object. |
paul@222 | 31 | """ |
paul@214 | 32 | |
paul@214 | 33 | self.target = target |
paul@214 | 34 | self.label = label |
paul@222 | 35 | self.link_target = link_target |
paul@222 | 36 | |
paul@222 | 37 | def __repr__(self): |
paul@222 | 38 | return "Link(%r, %r, %r)" % (self.target, self.label, self.link_target) |
paul@214 | 39 | |
paul@214 | 40 | def get_target(self): |
paul@214 | 41 | return self.target |
paul@214 | 42 | |
paul@214 | 43 | def get_label(self): |
paul@214 | 44 | return self.label or self.target |
paul@214 | 45 | |
paul@222 | 46 | def get_link_target(self): |
paul@222 | 47 | return self.link_target |
paul@214 | 48 | |
paul@91 | 49 | class Linker: |
paul@91 | 50 | |
paul@91 | 51 | "Translate Moin links into other forms." |
paul@91 | 52 | |
paul@193 | 53 | # Pagename whitespace conversion, configured using the "whitespace" metadata |
paul@193 | 54 | # setting. |
paul@193 | 55 | |
paul@193 | 56 | default_whitespace_map = [ |
paul@193 | 57 | (" ", "_"), |
paul@193 | 58 | ("\n", "_"), |
paul@193 | 59 | ] |
paul@193 | 60 | |
paul@165 | 61 | def __init__(self, metadata): |
paul@165 | 62 | |
paul@165 | 63 | "Initialise the linker with the 'metadata'." |
paul@91 | 64 | |
paul@165 | 65 | self.metadata = metadata |
paul@91 | 66 | |
paul@165 | 67 | # Obtain essential metadata. |
paul@159 | 68 | |
paul@165 | 69 | self.mapping = metadata.get("mapping", {}) |
paul@253 | 70 | self.root_pagename = metadata.get("root_pagename") |
paul@253 | 71 | self.attachments_dir = metadata.get("attachments") |
paul@159 | 72 | |
paul@159 | 73 | def resolve(path, pagename, root_pagename): |
paul@159 | 74 | |
paul@159 | 75 | "Resolve 'path' relative to 'pagename'." |
paul@159 | 76 | |
paul@159 | 77 | # Omit the root pagename from the resolved path components. |
paul@159 | 78 | |
paul@159 | 79 | if pagename == root_pagename: |
paul@159 | 80 | parts = [] |
paul@159 | 81 | else: |
paul@159 | 82 | parts = pagename.rstrip("/").split("/") |
paul@159 | 83 | |
paul@159 | 84 | t = path.split("/") |
paul@159 | 85 | |
paul@159 | 86 | first = True |
paul@159 | 87 | |
paul@159 | 88 | for p in t: |
paul@159 | 89 | |
paul@159 | 90 | # Handle replacement of the page with another. |
paul@159 | 91 | |
paul@159 | 92 | if p == ".": |
paul@159 | 93 | parts = [] |
paul@159 | 94 | |
paul@159 | 95 | # Handle ascent in the page hierarchy. |
paul@159 | 96 | |
paul@159 | 97 | elif p == "..": |
paul@159 | 98 | if parts: |
paul@159 | 99 | parts.pop() |
paul@159 | 100 | |
paul@159 | 101 | # Any non-navigation element replaces the path at the start. |
paul@159 | 102 | # Otherwise, the path is extended. |
paul@159 | 103 | # Omit the root pagename from the resolved path components if it would |
paul@159 | 104 | # appear at the start. |
paul@159 | 105 | |
paul@159 | 106 | elif p: |
paul@159 | 107 | if first: |
paul@159 | 108 | if p == root_pagename: |
paul@159 | 109 | parts = [] |
paul@159 | 110 | else: |
paul@159 | 111 | parts = [p] |
paul@159 | 112 | else: |
paul@159 | 113 | if parts or p != root_pagename: |
paul@159 | 114 | parts.append(p) |
paul@159 | 115 | |
paul@159 | 116 | first = False |
paul@159 | 117 | |
paul@159 | 118 | return "/".join(parts) |
paul@91 | 119 | |
paul@91 | 120 | # vim: tabstop=4 expandtab shiftwidth=4 |