python-webgen/parser.py

90 lines
2.3 KiB
Python
Raw Permalink Normal View History

2025-03-11 21:03:39 -07:00
# import modules
import markdown as md
# function definitions
# convertFile(): takes in a path to a markdown file and
# outputs an html snippet.
def convertFile(infilePath):
rawfile = open(infilePath)
output = ""
isComment = False
for line in rawfile:
if "+++" in line:
if not isComment:
isComment = True
continue
else:
isComment = False
continue
if isComment:
continue
else:
output = output + line
output = md.markdown(output)
return output
# getMetadata(): returns the metadata as a dictionary
def getMetadata(infilePath):
rawfile = open(infilePath)
isComment = False
metadata = {}
for line in rawfile:
if "+++" in line:
if not isComment:
isComment = True
continue
else:
isComment=False
break
if isComment:
key = line.split(" = ")[0]
value = line.split(' = ')[1][:-1]
metadata[key] = value
if metadata[key] == "true":
metadata[key] = True
if metadata[key] == "false":
metadata[key] = False
if isinstance(metadata[key], str):
metadata[key] = metadata[key][1:-1]
return metadata
# getTitle(): returns the title from the metadata
def getTitle(infilePath):
return getMetadata(infilePath)['title']
def convertDate(timestamp):
dateStr = ""
newTime = timestamp.split('T')[0]
date = newTime.split("-")
day = date[2].lstrip("0")
year = "20" + date[0][-2:]
match date[1]:
case "01":
month = "January"
case "02":
month = "February"
case "03":
month = "March"
case "04":
month = "April"
case "05":
month = "May"
case "06":
month = "June"
case "07":
month = "July"
case "08":
month = "August"
case "09":
month = "September"
case "10":
month = "October"
case "11":
month = "November"
case "12":
month = "December"
case _:
month = ""
return "{} {}, {}".format(day, month, year)