Created
February 25, 2020 08:24
-
-
Save DennySORA/9892e6380a2a251339fd504bf1c4fe67 to your computer and use it in GitHub Desktop.
Simple markdown parsing to list struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def CassificationToMD(textList, level=-1): | |
data = list() | |
count = 0 | |
noIndex = False | |
while True: | |
if len(textList[count:]) == 0: | |
break | |
if textList[count].startswith("#") and not noIndex: | |
newLevel = textList[count].count("#") | |
elif textList[count].count("```") == 2: | |
newLevel = 0 | |
elif textList[count].startswith("```") and not noIndex: | |
noIndex = True | |
newLevel = 0 | |
elif textList[count].startswith("```"): | |
noIndex = False | |
newLevel = 0 | |
else: | |
newLevel = 0 | |
if level >= newLevel and newLevel != 0: | |
return data, count-1 | |
elif level < newLevel: | |
returnData, returnCount = CassificationToMD( | |
textList[count + 1:], newLevel) | |
# Add return data to data box. | |
data.append([textList[count], returnData]) | |
# Update count point. | |
count += returnCount+1 | |
else: | |
# if not title then add to data box. | |
data.append(textList[count]) | |
count += 1 | |
return data, count | |
def MakeToData(*dataList): | |
text = str() | |
for i in dataList: | |
if type(i) == type(list()): | |
text += MakeToData(*i) + "\n" | |
else: | |
text += i + "\n" | |
return text | |
def main(): | |
reults = None | |
with open("text.md", 'r', encoding='utf-8') as data: | |
text = data.read().split("\n") | |
reults, count = CassificationToMD(text) | |
print("All Count is :", count) | |
reults.sort() | |
with open("Output.md", 'w', encoding='utf-8') as data: | |
data.write(MakeToData(*reults)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment