From ce6bb2d0f368c37da3224fa225d65ca54374b384 Mon Sep 17 00:00:00 2001 From: Gaspard Jankowiak Date: Mon, 31 Jul 2023 15:56:06 +0200 Subject: [PATCH] [scripts] add script to sort yaml files by (toplevel) key --- _config.yml | 2 +- scripts/sort_yaml.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 scripts/sort_yaml.py diff --git a/_config.yml b/_config.yml index f79e704..9752f3e 100644 --- a/_config.yml +++ b/_config.yml @@ -3,7 +3,7 @@ destination: ./_site markdown: kramdown -exclude: ["static", "s"] +exclude: ["static", "s", "scripts"] safe: true highlighter: rouge diff --git a/scripts/sort_yaml.py b/scripts/sort_yaml.py new file mode 100644 index 0000000..72d1bc6 --- /dev/null +++ b/scripts/sort_yaml.py @@ -0,0 +1,38 @@ +import sys +import re + +def sort_yaml(filename): + + keys = [] + blocks = {} + + with open(filename, "r") as f: + current_block = "" + current_key = None + + for line in f.readlines(): + if line == "\n" and current_key is not None: + if current_key in keys: + raise RuntimeError("Duplicate key '" + current_key + "'!") + keys.append(current_key) + blocks[current_key] = current_block + current_block = "" + current_key = None + continue + else: + m = re.match("^([a-z]+):\n$", line) + if m: + if current_key is None: + current_key = m.group(1) + else: + raise RuntimeError("New block not separated from the previous block by a newline!") + current_block += line + + keys.sort() + + for k in keys: + print(blocks[k]) + +if __name__ == "__main__": + for filename in sys.argv[1:]: + sort_yaml(filename)