[scripts] add script to sort yaml files by (toplevel) key

This commit is contained in:
Gaspard Jankowiak 2023-07-31 15:56:06 +02:00
parent 6b9c0094e3
commit ce6bb2d0f3
2 changed files with 39 additions and 1 deletions

View file

@ -3,7 +3,7 @@ destination: ./_site
markdown: kramdown markdown: kramdown
exclude: ["static", "s"] exclude: ["static", "s", "scripts"]
safe: true safe: true
highlighter: rouge highlighter: rouge

38
scripts/sort_yaml.py Normal file
View file

@ -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)