From d4008f369389aa584f42ede6e40e82c11a131a94 Mon Sep 17 00:00:00 2001
From: laurentheirendt <laurent.heirendt@uni.lu>
Date: Fri, 17 Dec 2021 09:29:00 +0100
Subject: [PATCH] add draft to base index generation on a given order

---
 .ci/generateIndex.py | 42 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/.ci/generateIndex.py b/.ci/generateIndex.py
index e07ade6e..c98c638c 100644
--- a/.ci/generateIndex.py
+++ b/.ci/generateIndex.py
@@ -42,6 +42,23 @@ def build_section_start(title, shortcut):
 def build_section_end():
     return "\t\t</ul>\n\t</div>"
 
+def save_order(localroot, root, filename):
+    order = ""
+    os.chdir(root)
+
+    with open(filename, 'r') as f:
+        for line in f:
+            # check for the start of the section
+            #print(line)
+            if line[0:6] == "order:":
+                order = line[7:]
+                break
+
+    # change back to the local root
+    os.chdir(localroot)
+
+    return order.rstrip()
+
 def save_legacy_from(localroot, root, filename):
     legacy_from = []
     os.chdir(root)
@@ -56,12 +73,13 @@ def save_legacy_from(localroot, root, filename):
                 legacy_from_flag = True
 
             # append lines from the legacy section
-            if legacy_from_flag and line[0:12]:
+            if legacy_from_flag:
                 legacy_from.append(line)
 
             # check for the end of the header
             if legacy_from_flag and line[0:3] == "---":
                legacy_from_flag = False
+               break
 
     # change back to the local root
     os.chdir(localroot)
@@ -75,6 +93,7 @@ def remove_header(localroot, root, filename):
 
     # count the number of lines
     count = 0
+    n = 0
     headerCheck = False
     with open(filename, 'r') as f:
         for line in f:
@@ -149,6 +168,7 @@ for folder in cardDirs:
                 if len(localIndexArr[indexS]) == 0:
                     localIndexArr[indexS] = ["\n"]
 
+                orderArr = []
                 # walk through the folders with all the cards
                 for root, dirs, files in os.walk(folder+"/"+d):
                     for file in files:
@@ -159,7 +179,8 @@ for folder in cardDirs:
                             if file[0] != "_":
                                 print(" > Generating header for: " + fileName)
 
-                                # save legacy section
+                                # save order and legacy section
+                                order = save_order(localroot, root, file)
                                 legacy_from = save_legacy_from(localroot, root, file)
 
                                 # remove the previous header
@@ -179,6 +200,9 @@ for folder in cardDirs:
 
                                 # define the header for each card
                                 header = "---\n"
+                                if len(order) > 0:
+                                    header += "order: " + str(order) + "\n"
+                                    orderArr.append(int(order))
                                 header += "layout: page\n"
                                 header += "permalink: " + permalink + "\n"
                                 header += "shortcut: " + root_handbook(shortcut) + "\n"
@@ -235,8 +259,18 @@ for folder in cardDirs:
                                 print(" + New header added.")
                                 print("-----------------------")
 
-                # join all subcategories to the index
-                localIndexArr[indexS] = natsorted(localIndexArr[indexS])
+                print(localIndexArr[indexS])
+                print(orderArr)
+
+                if len(orderArr) > 0:
+                    Y = orderArr
+                    X  = localIndexArr[indexS][1:]
+                    print("here")
+                    localIndexArr[indexS] = [x for _, x in sorted(zip(Y, X))]
+                    print(localIndexArr[indexS])
+                else:
+                    # join all subcategories to the index
+                    localIndexArr[indexS] = natsorted(localIndexArr[indexS])
 
 print(localIndexArr)
 
-- 
GitLab