From c294d541f9afbd15ec2f9ebf5a13f5c35c463aa6 Mon Sep 17 00:00:00 2001
From: laurentheirendt <laurent.heirendt@uni.lu>
Date: Wed, 14 Sep 2022 08:28:16 +0200
Subject: [PATCH] fix links for qms

---
 .ci/generateIndex.py | 74 +++++++++++++++++++++++++++++---------------
 1 file changed, 49 insertions(+), 25 deletions(-)

diff --git a/.ci/generateIndex.py b/.ci/generateIndex.py
index b0910730..2e31de62 100644
--- a/.ci/generateIndex.py
+++ b/.ci/generateIndex.py
@@ -20,16 +20,13 @@ def build_link(title, href):
 
     return f'\t\t\t<li><a href="{href}">{title}</a></li>\n'
 
-def root_handbook(href):
+def root_href(href):
     # remove sym link reference in href
     if ('handbook-additional' in href):
         href = href.replace('handbook-additional', 'handbook').strip()
     elif ('handbook-annexes' in href):
         href = href.replace('handbook-annexes', 'handbook').strip()
 
-    return href
-
-def root_lab(href):
     # remove sym link reference in href
     if ('lab-software' in href):
         href = href.replace('lab-software', 'lab').strip()
@@ -38,6 +35,12 @@ def root_lab(href):
     elif ('lab-hsa' in href):
         href = href.replace('lab-hsa', 'lab').strip()
 
+    # remove sym link reference in href
+    if ('qms-policies' in href):
+        href = href.replace('qms-policies', 'qms').strip()
+    elif ('qms-sops' in href):
+        href = href.replace('qms-sops', 'qms').strip()
+
     return href
 
 def build_section_start(title, shortcut):
@@ -50,6 +53,8 @@ def build_section_start(title, shortcut):
     title = title.replace("Lab software", "Lab: Software")
     title = title.replace("Lab equipment", "Lab: Equipment")
     title = title.replace("Lab hsa", "Lab: Health & Safety, Access")
+    title = title.replace("Qms sops", "QMS: Standard Operating Procedures (SOPS)")
+    title = title.replace("Qms policies", "QMS: Policies")
 
     return f'\n\t<div class="index-box noborderbox" id="{shortcut}-card">\n\t\t<h3>{title}</h3>\n\t\t<ul>\n'
 
@@ -84,7 +89,8 @@ def get_ignore():
             "assets",
             "help.md",
             "index.md",
-            "pagination.md"
+            "pagination.md",
+            ".gitkeep",
             ]
 
 def save_legacy_from(localroot, root, filename):
@@ -149,6 +155,34 @@ def remove_header(localroot, root, filename):
 
     return n
 
+def folders_in(path_to_parent):
+  for fname in os.listdir(path_to_parent):
+    if os.path.isdir(os.path.join(path_to_parent, fname)):
+      yield os.path.join(path_to_parent, fname)
+
+def has_folders(path_to_parent):
+  folders = list(folders_in(path_to_parent))
+  return len(folders) != 0
+
+def element_header(prefix, permalink, element):
+
+    # handbook section
+    if "handbook-annexes" in permalink or "handbook-additional" in permalink:
+        element += prefix + "/cards/" + root_href(shortcut) + "\n"
+        element += prefix + "/" + folder + "/cards/" + root_href(shortcut) + "\n"
+
+    # lab section
+    if "lab-software" in permalink or "lab-equipment" in permalink or "lab-hsa" in permalink:
+        element += prefix + "/cards/" + root_href(shortcut) + "\n"
+        element += prefix + "/" + folder + "/cards/" + root_href(shortcut) + "\n"
+
+    # QMS section
+    if "qms-policies" in permalink or "qms-sops" in permalink:
+        element += prefix + "/cards/" + root_href(shortcut) + "\n"
+        element += prefix + "/" + folder + "/cards/" + root_href(shortcut) + "\n"
+
+    return element
+
 def generate_header(folder, permalink, shortcut, order, legacy_from, title, description):
     header = "---\n"
 
@@ -157,18 +191,13 @@ def generate_header(folder, permalink, shortcut, order, legacy_from, title, desc
 
     header += "layout: page\n"
     header += "permalink: " + permalink + "\n"
-    header += "shortcut: " + root_handbook(shortcut) + "\n"
+    header += "shortcut: " + root_href(shortcut) + "\n"
     header += "redirect_from:\n"
     header += "  - /cards/" + shortcut + "\n"
     header += "  - /" + folder + "/cards/" + shortcut + "\n"
-    # special case of handbook
-    if "handbook-annexes" in permalink or "handbook-additional" in permalink:
-        header += "  - /cards/" + root_handbook(shortcut) + "\n"
-        header += "  - /" + folder + "/cards/" + root_handbook(shortcut) + "\n"
-    # separate lab section
-    if "lab-software" in permalink or "lab-equipment" in permalink or "lab-hsa" in permalink:
-        header += "  - /cards/" + root_lab(shortcut) + "\n"
-        header += "  - /" + folder + "/cards/" + root_lab(shortcut) + "\n"
+
+    # generate specific redirects
+    header = element_header("  - ", shortcut, header)
 
     # include the legacy section in the redirect_from section
     if len(legacy_from) > 0:
@@ -195,13 +224,8 @@ def generate_whitelist_entry(folder, permalink, shortcut):
     wl_entry += "/?" + shortcut + "\n"
     wl_entry += "/cards/" + shortcut + "\n"
     wl_entry += "/" + folder + "/cards/" + shortcut + "\n"
-    if "handbook-annexes" in permalink or "handbook-additional" in permalink:
-        wl_entry += "/cards/" + root_handbook(shortcut) + "\n"
-        wl_entry += "/" + folder + "/cards/" + root_handbook(shortcut) + "\n"
-    # separate lab section
-    if "lab-software" in permalink or "lab-equipment" in permalink or "lab-hsa" in permalink:
-        wl_entry += "/cards/" + root_lab(shortcut) + "\n"
-        wl_entry += "/" + folder + "/cards/" + root_lab(shortcut) + "\n"
+
+    wl_entry = element_header("", permalink, wl_entry)
 
     return wl_entry
 
@@ -212,17 +236,17 @@ localroot = os.getcwd()
 ignore = get_ignore()
 
 # generate the index properly speaking
-cardDirs = ["internal", "external", "policies"]
+cardDirs = ["internal", "external"]
 sections = []
 
 # determine first the directories
 for direct in cardDirs:
-    if path.isdir(direct):
+    if path.isdir(direct) and has_folders(direct):
         dirs = os.listdir(direct)
         dirs = natsorted(dirs)
 
         for d in dirs:
-            if d[0] != "." and d not in ignore:
+            if d[0] != "." and d not in ignore and has_folders(direct + "/" + d):
                 sections.append(d)
 
 sections = list(set(sections))
@@ -246,7 +270,7 @@ for folder in cardDirs:
         dirs = natsorted(dirs)
 
         for d in dirs:
-            if d[0] != "." and d not in ignore:
+            if d[0] != "." and d not in ignore and has_folders(folder + "/" + d):
                 # set the header of the section
                 #index += "\n### " + d.replace("-", " ").capitalize() + "\n"
 
-- 
GitLab