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