Tim 'mithro' Ansell | 8b323f8 | 2020-05-14 13:18:07 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2020 SkyWater PDK Authors |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | # SPDX-License-Identifier: Apache-2.0 |
| 17 | |
| 18 | |
| 19 | import os.path |
| 20 | import sys |
| 21 | import tempfile |
| 22 | from collections import defaultdict |
| 23 | |
| 24 | |
| 25 | def count_chars(l): |
| 26 | o = defaultdict(lambda: 0) |
| 27 | for i in l: |
| 28 | o[i] += 1 |
| 29 | if '\n' in o: |
| 30 | del o['\n'] |
| 31 | return dict(o) |
| 32 | |
| 33 | |
| 34 | def main(argv): |
| 35 | assert len(argv) == 1, argv |
| 36 | |
| 37 | fname = argv[0] |
| 38 | assert fname.endswith('.rst'), fname |
| 39 | assert os.path.exists(fname), fname |
| 40 | |
| 41 | output = ['',] |
| 42 | with open(fname) as f: |
| 43 | for l in f: |
| 44 | output.append(l) |
| 45 | c = count_chars(l) |
| 46 | if len(c) != 1: |
| 47 | continue |
| 48 | |
| 49 | header = list(c.keys())[0] |
| 50 | if header not in ['-', '=', '+', '~']: |
| 51 | print("Possible header?", repr(l)) |
| 52 | continue |
| 53 | |
| 54 | lastline = output[-2] |
| 55 | |
| 56 | if len(lastline) <= 4: |
| 57 | continue |
| 58 | |
| 59 | oheader = (header * (len(lastline)-1))+'\n' |
| 60 | output[-1] = oheader |
| 61 | |
| 62 | with open(fname, 'w') as f: |
| 63 | f.write("".join(output[1:])) |
| 64 | |
| 65 | return 0 |
| 66 | |
| 67 | |
| 68 | if __name__ == "__main__": |
| 69 | sys.exit(main(sys.argv[1:])) |