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