]> de.git.xonotic.org Git - xonotic/xonotic.wiki.git/blobdiff - assets/check-and-fix.py
Mapping: Screenshots and minor clarification
[xonotic/xonotic.wiki.git] / assets / check-and-fix.py
index 2528d04ef3904daf2b5d3a1f0c70500268fd69b1..f69d206703ee1b0a6149c9491e240abf35de96e3 100755 (executable)
@@ -4,10 +4,8 @@
 
 # Well, this wasn't supposed to be so long and complicated.
 # Anyway, it makes sure the wiki works on both Gitlab and Github by moving
 
 # Well, this wasn't supposed to be so long and complicated.
 # Anyway, it makes sure the wiki works on both Gitlab and Github by moving
-# stuff around and fixing links. Then it reports all remaining broken links
-# and unused files. Since the wiki is in git, you can use `git status`
-# and `git diff` to see the changes. You can also use the `--dry-run` flag
-# to print all changes the script would make without actually making them.
+# stuff around and fixing links. Then it reports all broken links
+# and unused files that can't be fixed automatically. By default it only prints changes it would make to stdout, if you wish to apply them, use `--fix`.
 
 # See Editing.md for more information.
 
 
 # See Editing.md for more information.
 
@@ -33,7 +31,7 @@ def compile_regex(rgx: str):
     # regex (unlike re) supports non-constant length look-behinds
     return regex.compile(
         "".join(
     # regex (unlike re) supports non-constant length look-behinds
     return regex.compile(
         "".join(
-            [line.strip() for line in rgx]))
+            [line.lstrip() for line in rgx.split('\n')]))
 
 
 # examples:
 
 
 # examples:
@@ -68,7 +66,7 @@ LINK_REGEX = compile_regex("""
 """)
 
 
 """)
 
 
-dry_run = False
+apply_fixes = False
 
 
 def strip_header_link(link: str) -> str:
 
 
 def strip_header_link(link: str) -> str:
@@ -126,10 +124,10 @@ def fix_dir_structure():
 
         if os.path.exists(fixed):
             print("warning: collision: {}".format(path))
 
         if os.path.exists(fixed):
             print("warning: collision: {}".format(path))
-        elif dry_run:
-            print("would rename {} to {}".format(path, fixed))
-        else:
+        elif apply_fixes:
             os.rename(path, fixed)
             os.rename(path, fixed)
+        else:
+            print("would rename {} to {}".format(path, fixed))
 
 
 def is_between_files(link: str) -> bool:
 
 
 def is_between_files(link: str) -> bool:
@@ -180,15 +178,14 @@ def fix_links():
             changes = []
             replacer = functools.partial(replace_link, changes)
             contents_new = LINK_REGEX.sub(replacer, contents)
             changes = []
             replacer = functools.partial(replace_link, changes)
             contents_new = LINK_REGEX.sub(replacer, contents)
-            if dry_run and any(changes):
-                print("would convert these links in {}:".format(path))
-                for change in changes:
-                    print(change)
-
-            if not dry_run and contents != contents_new:
+            if apply_fixes and contents != contents_new:
                 f.seek(0)
                 f.write(contents_new)
                 f.truncate()
                 f.seek(0)
                 f.write(contents_new)
                 f.truncate()
+            elif not apply_fixes and any(changes):
+                print("would convert these links in {}:".format(path))
+                for change in changes:
+                    print(change)
 
 
 def link_to_path(current_file: str, link: str) -> str:
 
 
 def link_to_path(current_file: str, link: str) -> str:
@@ -200,7 +197,7 @@ def link_to_path(current_file: str, link: str) -> str:
     # when not using subdirs, nothing or "." works for all 3
 
     if link.startswith("..") or link.startswith("/"):
     # when not using subdirs, nothing or "." works for all 3
 
     if link.startswith("..") or link.startswith("/"):
-        print("file: {} bad link: {}", link)
+        print("file: {} bad link: {}".format(current_file, link))
 
     # path relative to wiki root, not curent file
     current_dir = dirname(current_file)
 
     # path relative to wiki root, not curent file
     current_dir = dirname(current_file)
@@ -277,7 +274,7 @@ def find_unlinked(all_paths: List[str]):
 
     walk_links(canonical_to_real, is_linked, "Home.md")
 
 
     walk_links(canonical_to_real, is_linked, "Home.md")
 
-    for path, linked in is_linked.items():
+    for path, linked in sorted(is_linked.items()):
         if not linked:
             print("not reachable from Home: {}".format(path))
 
         if not linked:
             print("not reachable from Home: {}".format(path))
 
@@ -289,9 +286,9 @@ def check_links():
 
 
 def main():
 
 
 def main():
-    global dry_run
-    if len(sys.argv) > 1 and sys.argv[1] == "--dry-run":
-        dry_run = True
+    global apply_fixes
+    if len(sys.argv) > 1 and sys.argv[1] == "--fix":
+        apply_fixes = True
 
     # convert file paths - put everything into root
     fix_dir_structure()
 
     # convert file paths - put everything into root
     fix_dir_structure()