Verified Commit 0a77f845 authored by Laurent Heirendt's avatar Laurent Heirendt
Browse files

check existence of folder

parent 7474bbca
import os, re
folder = "internal"
from os import path
def line_prepender(filename, line):
with open(filename, 'r+') as f:
......@@ -57,11 +56,12 @@ sections = []
# determine first the directories
for direct in cardDirs:
dirs = os.listdir(direct)
if path.isdir(direct):
dirs = os.listdir(direct)
for d in dirs:
if d[0] != ".":
sections.append(d)
for d in dirs:
if d[0] != ".":
sections.append(d)
sections = list(set(sections))
sections.sort()
......@@ -70,89 +70,89 @@ index = ""
localIndexArr = [[]] * len(sections)
for folder in cardDirs:
# check here if folder exists
dirs = os.listdir(folder)
dirs = sorted(dirs)
for d in dirs:
if d[0] != ".":
# set the header of the section
#index += "\n### " + d.replace("-", " ").capitalize() + "\n"
# get the index of the section
indexS = sections.index(d)
#print(indexS)
if len(localIndexArr[indexS]) == 0:
localIndexArr[indexS] = ["\n"]
# walk through the folders with all the cards
for root, dirs, files in os.walk(folder+"/"+d):
for file in files:
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)
# remove the previous header
remove_header(localroot, root, file, 8)
# 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)
# define the header for each card
header = "---\n"
header += "layout: page\n"
header += "permalink: " + permalink + "\n"
header += "shortcut: " + shortcut + "\n"
header += "redirect_from:\n"
header += " - /cards/" + shortcut + "\n"
header += " - /internal/cards/" + shortcut + "\n"
header += "---"
# add the header properly speaking
line_prepender(fileName, header)
# open file and get the title after the header
count = 0
title = ""
bp = 9
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:]
#index += " * [" + title + "](./" + root + "/" + "\n"
localIndexArr[indexS].append("* [" + title + "](./" + root + "/" + ")\n")
# output
print(" + New header added.")
print("-----------------------")
# join all subcategories to the index
localIndexArr[indexS].sort()
#index += ''.join(localIndexArr)
# check if folder exists
if path.isdir(folder):
dirs = os.listdir(folder)
dirs = sorted(dirs)
for d in dirs:
if d[0] != ".":
# set the header of the section
#index += "\n### " + d.replace("-", " ").capitalize() + "\n"
# get the index of the section
indexS = sections.index(d)
#print(indexS)
if len(localIndexArr[indexS]) == 0:
localIndexArr[indexS] = ["\n"]
# walk through the folders with all the cards
for root, dirs, files in os.walk(folder+"/"+d):
for file in files:
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)
# remove the previous header
remove_header(localroot, root, file, 8)
# 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)
# define the header for each card
header = "---\n"
header += "layout: page\n"
header += "permalink: " + permalink + "\n"
header += "shortcut: " + shortcut + "\n"
header += "redirect_from:\n"
header += " - /cards/" + shortcut + "\n"
header += " - /internal/cards/" + shortcut + "\n"
header += "---"
# add the header properly speaking
line_prepender(fileName, header)
# open file and get the title after the header
count = 0
title = ""
bp = 9
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:]
#index += " * [" + title + "](./" + root + "/" + "\n"
localIndexArr[indexS].append("* [" + title + "](./" + root + "/" + ")\n")
# output
print(" + New header added.")
print("-----------------------")
# join all subcategories to the index
localIndexArr[indexS].sort()
print(localIndexArr)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment