-
Notifications
You must be signed in to change notification settings - Fork 0
/
mingen.py
93 lines (81 loc) · 2.75 KB
/
mingen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# mingen - static site generator
# created on: Sunday 17 April 2022 03:31:45 PM IST
import os
import sys
import markdown
def enclose(tag, content, attributes=""):
result = "<" + tag + " " + attributes + ">" + content + "</" + tag + ">"
return result
def index(folderPath):
fileList = os.listdir(folderPath)
index = ""
for fileName in fileList:
if fileName[-3:] == ".md":
if fileName == "index.md":
continue
file_ = open(folderPath+"/"+fileName, "r")
title = file_.readline()
file_.close()
index = index + enclose("li", "<a href=\"" + fileName[:-3] + ".html\">" + title[2:] + "</a>")
if index == "":
return index
else:
index = enclose("ul", index)
return index
def nav(filePath):
filePath = filePath.replace(cwd, "")
nav = ""
while filePath != "":
nav = " / " + "<a href=\"" + filePath + "\">" + filePath[filePath.rindex("/")+1:] + "</a>" + nav
filePath = filePath[:filePath.rindex("/")]
nav = "<a href=\"/\">" + siteName + "</a>" + nav
nav = "<div class=navpath><p>" + nav + "</p></div>"
return nav
def md2html(mdFilePath, mdFileName):
mdFile_ = open(mdFilePath+"/"+mdFileName, "r")
firstLine = mdFile_.readline()
mdFile = mdFile_.read()
mdFile_.close()
htmlFile_ = open(mdFilePath+"/"+mdFileName[:-3]+".html", "w+")
title = enclose("title", firstLine[2:-1])
if mdFileName == "index.md":
index_ = index(mdFilePath)
else:
index_ = ""
body = enclose("body", enclose("header", enclose("nav", navLinks + nav(mdFilePath)) + "<hr>" + markdown.markdown(firstLine))
+ enclose("main", markdown.markdown(mdFile) + index_) + "<hr>" + footer)
htmlFile = enclose("html", enclose("head", head + title) + body)
htmlFile_.write(htmlFile)
htmlFile_.close()
def compile(filePath):
fileList = os.listdir(filePath)
for fileName in fileList:
if os.path.isdir(filePath+"/"+fileName):
if fileName == "mingen":
continue
compile(filePath+"/"+fileName)
elif fileName[-3:] == ".md":
md2html(filePath, fileName)
cwd = os.getcwd()
siteName = cwd[cwd.rindex("/")+1:]
try:
navLinks_ = open(cwd+"/items/nav.md", "r")
navLinks = navLinks_.read()
navLinks_.close()
navLinks = "<div class=navlinks>" + markdown.markdown(navLinks) + "</div>"
except:
navLinks = ""
try:
footer_ = open(cwd+"/items/footer.md", "r")
footer = footer_.read()
footer_.close()
footer = "<footer>" + markdown.markdown(footer) + "</footer>"
except:
footer = ""
try:
head_ = open(cwd+"/items/head.html", "r")
head = head_.read()
head_.close()
except:
head = ""
compile(cwd)