Skip to content
Snippets Groups Projects
Verified Commit d877bed6 authored by Laurent Heirendt's avatar Laurent Heirendt :airplane:
Browse files

add other functions to sort sections and generate index

parent 4feae0a1
No related branches found
No related tags found
No related merge requests found
import os, re
from os import path
from os.path import exists
from threading import local
import yaml
from natsort import natsorted
def line_prepender(filename, line):
with open(filename, 'r+') as f:
......@@ -286,94 +288,184 @@ def get_title(localroot, root, file):
return title, qms_yml, yml_file
def core(file, fileName, localroot, root, folder, localIndexArr, orderArr, indexS, whiteList, folderFlag, maxOrder):
# ignore subsections (.md files that start with _)
if file[0] != "_":
print(" > Generating header for: " + fileName)
# save order and legacy section
order = save_tag(localroot, root, file, "card_order")
legacy_from = save_legacy_from(localroot, root, file)
description = save_tag(localroot, root, file, "description")
# extract the title from the QMS metadata
title, qms_yml, yml_file = get_title(localroot, root, file)
if qms_yml:
name = save_tag(localroot, root, yml_file, "name")
prepare_qms(localroot, root, file)
# remove the previous header
n = remove_header(localroot, root, file)
# generate a permalink
permalink = "/" + root + "/"
# generate the shortcut
shortcut = re.sub(folder, '', root)
# remove the first /
shortcut = shortcut[1:]
# replace the / with a :
shortcut = re.sub('/', ':', shortcut)
if len(order) > 0:
# find the maximum of existing orders
if folderFlag:
if len(orderArr[indexS]) > 0:
maxOrder = max(orderArr[indexS])
else:
maxOrder = 0
# after determining the max order, set the folder flag to False to avoid another entry into the same block of code
folderFlag = False
tmp = orderArr[indexS].copy()
tmp.append(maxOrder + int(order))
orderArr[indexS] = tmp
def core(file, localroot, root, folder, localIndexArr, orderArr, indexS, whiteList, folderFlag, maxOrder):
if file.endswith(".md"):
fileName = os.path.join(root, file)
# ignore subsections (.md files that start with _)
if file[0] != "_":
print(" > Generating header for: " + fileName)
# save order and legacy section
order = save_tag(localroot, root, file, "card_order")
legacy_from = save_legacy_from(localroot, root, file)
description = save_tag(localroot, root, file, "description")
# extract the title from the QMS metadata
title, qms_yml, yml_file = get_title(localroot, root, file)
if qms_yml:
name = save_tag(localroot, root, yml_file, "name")
prepare_qms(localroot, root, file)
# remove the previous header
n = remove_header(localroot, root, file)
# generate a permalink
permalink = "/" + root + "/"
# generate the shortcut
shortcut = re.sub(folder, '', root)
# remove the first /
shortcut = shortcut[1:]
# replace the / with a :
shortcut = re.sub('/', ':', shortcut)
if len(order) > 0:
# find the maximum of existing orders
if folderFlag:
if len(orderArr[indexS]) > 0:
maxOrder = max(orderArr[indexS])
else:
maxOrder = 0
# after determining the max order, set the folder flag to False to avoid another entry into the same block of code
folderFlag = False
tmp = orderArr[indexS].copy()
tmp.append(maxOrder + int(order))
orderArr[indexS] = tmp
else:
orderArr[indexS] = []
# generate the header for each card
header = generate_header(folder, permalink, shortcut, order, legacy_from, title, description, qms_yml)
# add autogenerated links to whitelist
whiteList += generate_whitelist_entry(folder, permalink, shortcut)
# add the header properly speaking
line_prepender(fileName, header)
# open file and get the title after the header
if "qms" in root:
header_offset = 3
else:
header_offset = 1
if not qms_yml:
count = 0
title = ""
bp = n + header_offset
with open(fileName, 'r') as f:
for line in f:
count += 1
if count == bp:
if len(line) > 2:
title = line
break
else:
bp += 1
# remove first and last chars
title = title.rstrip("\n\r")
title = title[2:]
# open file and get the title after the header
#list_titles.append({ 'name': name, 'link': build_link(name + ' -- ' + title, permalink)})
localIndexArr[indexS].append(build_link(title, root))
# output
print(" + New header added.")
print("-----------------------")
return folderFlag, localIndexArr, whiteList
def get_sections(cardDirs):
sections = []
# retrieve ignore list
ignore = get_ignore()
# determine first the directories
for direct in cardDirs:
if path.isdir(direct) and has_subdirs(direct):
dirs = os.listdir(direct)
dirs = natsorted(dirs)
for d in dirs:
if d[0] != "." and d not in ignore and has_subdirs(direct + "/" + d):
sections.append(d)
sections = list(set(sections))
sections = natsorted(sections)
return sections, ignore
def get_indexArr(sections):
localIndexArr = {k: [] for k in range(len(sections))}
orderArr = {k: [] for k in range(len(sections))}
return localIndexArr, orderArr
def sort_sections(sections, localIndexArr, orderArr):
for d in sections:
indexS = sections.index(d)
# join all subcategories to the index
# if all subcategories have a predefined order
if len(orderArr[indexS]) == len(localIndexArr[indexS])-1 and len(orderArr[indexS]) > 0:
print("")
X = localIndexArr[indexS][1:]
Y = orderArr[indexS]
localIndexArr[indexS] = [x for _, x in sorted(zip(Y, X))]
# natural sorting otherwise
else:
orderArr[indexS] = []
localIndexArr[indexS] = natsorted(localIndexArr[indexS])
print(localIndexArr)
# generate the header for each card
header = generate_header(folder, permalink, shortcut, order, legacy_from, title, description, qms_yml)
return localIndexArr
# add autogenerated links to whitelist
whiteList += generate_whitelist_entry(folder, permalink, shortcut)
def generate_index(index, sections, localIndexArr):
# determine the index
k = 0
for s in sections:
index += build_section_start(s.replace("-", " ").capitalize(), s)
index += ''.join(localIndexArr[k])
index += build_section_end()
k += 1
# add the header properly speaking
line_prepender(fileName, header)
# close the container
index += "\n</div>"
# open file and get the title after the header
if "qms" in root:
header_offset = 3
else:
header_offset = 1
if not qms_yml:
count = 0
title = ""
bp = n + header_offset
with open(fileName, 'r') as f:
for line in f:
count += 1
if count == bp:
if len(line) > 2:
title = line
break
else:
bp += 1
# remove first and last chars
title = title.rstrip("\n\r")
title = title[2:]
# open file and get the title after the header
#list_titles.append({ 'name': name, 'link': build_link(name + ' -- ' + title, permalink)})
localIndexArr[indexS].append(build_link(title, root))
# output
print(" + New header added.")
print("-----------------------")
return folderFlag, localIndexArr, whiteList
\ No newline at end of file
## add link to return to main index
index += """<br><center><a href="{{ '/' | relative_url }}">go back</a></center>"""
index += """<br><center><a href="{{ '/cards' | relative_url }}">Overview of all HowTo cards</a></center>"""
return index
def save_index(index, indexFile):
# output the index
#print(index)
# Read in the file
#indexFile = "cards.md"
filedata = ""
with open(indexFile, 'r') as file :
for line in file:
filedata += line
# stop reading once the index place holder has been reached
if re.search("<!-- index -->", line):
filedata += "[[ index ]]"
break
# Replace the target string
filedata = filedata.replace('[[ index ]]', index)
# Write the file out again
with open(indexFile, 'w') as file:
file.write(filedata)
print("\n > New index generated and saved in " + indexFile)
\ No newline at end of file
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