paul@333 | 1 | #!/usr/bin/env python |
paul@333 | 2 | |
paul@333 | 3 | """ |
paul@333 | 4 | Prettyprinted document tree parser. |
paul@333 | 5 | |
paul@333 | 6 | Copyright (C) 2017, 2018, 2019, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@333 | 7 | |
paul@333 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@333 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@333 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@333 | 11 | version. |
paul@333 | 12 | |
paul@333 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@333 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@333 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@333 | 16 | details. |
paul@333 | 17 | |
paul@333 | 18 | You should have received a copy of the GNU General Public License along with |
paul@333 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@333 | 20 | """ |
paul@333 | 21 | |
paul@333 | 22 | from moinformat.tree.pretty import Node |
paul@333 | 23 | |
paul@333 | 24 | class PrettyParser: |
paul@333 | 25 | |
paul@333 | 26 | "A prettyprinted document tree parser." |
paul@333 | 27 | |
paul@333 | 28 | formats = ["pretty"] |
paul@333 | 29 | |
paul@333 | 30 | def __init__(self, metadata): |
paul@333 | 31 | self.metadata = metadata |
paul@333 | 32 | |
paul@333 | 33 | def parse(self, s): |
paul@333 | 34 | |
paul@333 | 35 | "Parse the tree structure representation in 's'." |
paul@333 | 36 | |
paul@333 | 37 | indent = 0 |
paul@333 | 38 | branches = [] |
paul@333 | 39 | |
paul@333 | 40 | for line in s.split("\n"): |
paul@333 | 41 | line = line.rstrip() |
paul@333 | 42 | if not line: |
paul@333 | 43 | continue |
paul@333 | 44 | |
paul@333 | 45 | new_indent = line.rfind(" ") + 1 |
paul@333 | 46 | node = Node(line[new_indent:]) |
paul@333 | 47 | |
paul@333 | 48 | # Establish a branch to add nodes to. |
paul@333 | 49 | |
paul@333 | 50 | if not branches: |
paul@333 | 51 | branches.append(node) |
paul@333 | 52 | else: |
paul@333 | 53 | # Note the current node as outermost branch. |
paul@333 | 54 | |
paul@333 | 55 | if new_indent > indent: |
paul@333 | 56 | branches.append(node) |
paul@333 | 57 | else: |
paul@333 | 58 | # Reduced indent involves obtaining an inner branch again. |
paul@333 | 59 | |
paul@333 | 60 | while indent > new_indent: |
paul@333 | 61 | del branches[-1] |
paul@333 | 62 | indent -= 2 |
paul@333 | 63 | |
paul@333 | 64 | # Note the current node as outermost branch. |
paul@333 | 65 | |
paul@333 | 66 | branches[-1] = node |
paul@333 | 67 | |
paul@333 | 68 | # Append the current node to the parent branch. |
paul@333 | 69 | |
paul@333 | 70 | branches[-2].append(node) |
paul@333 | 71 | |
paul@333 | 72 | indent = new_indent |
paul@333 | 73 | |
paul@333 | 74 | return branches[0] |
paul@333 | 75 | |
paul@333 | 76 | parser = PrettyParser |
paul@333 | 77 | |
paul@333 | 78 | # vim: tabstop=4 expandtab shiftwidth=4 |