Skip to content
Snippets Groups Projects
Commit f97f7e60 authored by Martin Schroschk's avatar Martin Schroschk
Browse files

Make check Working

* The Script read mkdocs.yaml and checks for differences in TOC and
  top level heading in md-files; differences are outputed to stdout
* Return code of the script reflects the found number of mismatches
* Admins can add files to the whitelist; differences in these files are
  ignored
parent d4caa2fd
No related branches found
No related tags found
2 merge requests!811Automated merge from preview to main,!799Created a new check-TOC.py without importing pd, and yaml
......@@ -5,55 +5,61 @@ Provide as an command line argument the path to the mkdocs.yml file.
Author: Michael Bommhardt-Richter
"""
import os
import sys
files = []
firstline = []
nav_section = dict()
from pathlib import Path
# {path/filename.md: [toc_heading, file_heading], ... }
TOCData = dict()
whitelist = ["index.md"] # ["archive"]
TOCFiles=dict()
TOCData=dict()
def list_and_read_files(path):
"List files in a directory recursively and read the first line of each file"
def get_heading_in_file(filename, docs_path):
# TODO join path filename
# Read until first level one heading is found
f = os.path.join(docs_path, "docs", filename)
with open(f, "r") as file:
for line in file:
if line.startswith("#"):
# TODO Make sure it is really a level one heading!
# Will be empty if there is more than one "#".
return line.split("#")[1].strip()
for root, _, filenames in os.walk(path):
for filename in filenames:
if filename.endswith('.md'):
TOCFiles[os.path.join(root.split('/')[-1], filename)]=open(os.path.join(root, filename)).readline().strip().replace('# ','')
return 0
def main():
"Main function"
path = os.getcwd()
with open(sys.argv[1], "r") as file:
c = file.readlines()
nav_section = dict()
with open(sys.argv[1], 'r') as file:
for line in file:
line = line.rstrip()
if line.endswith('.md'):
line=line.split(" - ")[1]
line=line.split(": ")
TOCData[line[1]]=line[0]
set1 = set(TOCFiles.items())
set2 = set(TOCData.items())
output=str(sorted(set1 ^ set2))
output=output.replace("{('","'").replace(")}","").replace("[(","").replace(")]","").split("), (")
print("| Filename | Diff |")
print("|------------------------------------------------------------|------------------------------------------------------------|")
for x in output:
if not "overview.md" in x and not "Overview" in x:
#if not "(Outdated)" in x and not "index.md" in x:
#print(x)
y=x.split("', '")
print("|{0:>60}|{1:>60}".format(y[0]+"'", "'"+y[1]))
if __name__ == '__main__':
docs_path = Path(sys.argv[1]).resolve().parent
print(docs_path)
for line in c:
line = line.rstrip()
# "headline: path/file.md" -> "Headline" = "path/file.md"
if line.endswith(".md"):
line = line.split(" - ")[1]
line = line.split(": ")
key = line[1]
file_heading = get_heading_in_file(line[1], docs_path)
TOCData[line[1]] = [line[0], file_heading]
# Check TOC vs heading in corresponding md-file
cnt = 0
for key, value in TOCData.items():
if key in whitelist:
continue
if value[0] == "Overview":
continue
if value[0] != value[1]:
cnt += 1
print(f"{key:<40}{value[0]:<30} != {value[1]}")
sys.exit(cnt)
if __name__ == "__main__":
if len(sys.argv) > 1:
main()
else:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment