-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
185 lines (156 loc) · 6.17 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
Copyright 2024 Nekoweb Wiki
This software is licensed under the 3-clause BSD license.
You should have recieved a copy of such license with this software,
if you did not, a copy can be found at the following
https://opensource.org/license/BSD-3-Clause
Otherwise, a copy of this license can be found in the root directory
of this project.
"""
from git import Repo
from RenderUtils import JinjaRender, RenderMarkdown, WIKI_PAGE_TEMPLATE, TOC, GetTemplate, MDWiki
from os import listdir
from os.path import join as JoinPath, isdir, exists as PathExists, dirname, realpath
from MiscUtils import InitDir, IncludeExclude, IncludeExcludeTest, GenericCopy
from config import OUTPUT_DIR, DIRECTORIES, ADD_FILES, COMMIT_PREFIX, COMMIT_SUFFIX, SITE_URL
from distutils.dir_util import copy_tree as CopyDir
from syntaxcolors import AddSyntaxColors
from datetime import datetime, UTC as UTC_TIMESTAMP
repo = Repo()
Indexed = []
def wikiparse(input_dir: str, output: str, rawinfo: dict = { "out": "w", "articledir": True }):
global Indexed
contents = listdir(input_dir)
for content in contents:
if isdir(content):
wikiparse(JoinPath(input_dir, content), JoinPath(output, content), rawinfo=rawinfo)
continue
isarticle = rawinfo["articledir"]
RenderedMD = RenderMarkdown(JoinPath(input_dir, content))
metadata = RenderedMD.metadata
PageTitle = metadata["title"]
Title = (
metadata["forcetitle"]
if "forcetitle" in metadata
else f"Nekoweb Wiki - {PageTitle}"
)
PageSubtitle = metadata["subtitle"] if "subtitle" in metadata else None
Description = metadata["desc"] if "desc" in metadata else PageSubtitle
TableOfContents = TOC(
RenderedMD.toc_html,
forcenone=(
metadata["notoc"] if "notoc" in metadata else False
)
)
RenderedOut = content.replace(".md", ".html")
webout = output.replace("\\", "/").replace("build/", "/", 1) + "/" + RenderedOut
weboutforeditor = webout[2:] if webout.startswith("//") else webout[1:]
WikiRender, options = MDWiki(
RenderedMD,
{
"toc": TableOfContents,
}
)
JinjaRender(
WIKI_PAGE_TEMPLATE,
JoinPath(output, RenderedOut),
LANG="en",
PAGE_TITLE=Title,
PAGE_TYPE=("article" if isarticle else "website"),
DisplayTitle=PageTitle,
PAGE_DESC=Description,
PAGE_SUBTITLE=PageSubtitle,
TableOfContents=TableOfContents if not options["manualtoc"] else None,
Content=WikiRender,
Source=JoinPath(input_dir, content).replace("\\", "/"),
webout=weboutforeditor
)
Indexed.append((webout,PageTitle,input_dir,Description))
def StaticDirectory(directory: str, output: str):
build_include, build_exclude = IncludeExclude(directory)
for file in listdir(directory):
CurrentPath = JoinPath(directory, file)
FAILURE = IncludeExcludeTest(build_include, build_exclude, CurrentPath)
if FAILURE:
continue
GenericCopy(CurrentPath, output)
def WriteFeed(CommitBase, ReleaseMessage = "#action/build-deploy"):
commits = list(CommitBase)
Releases = []
RSSFeed = ""
for commit in commits:
if len(Releases) == 10:
break
if ReleaseMessage not in commit.message:
continue
ChangelogId = datetime.fromtimestamp(commit.authored_date, UTC_TIMESTAMP).strftime("%Y-%b-%d").lower()
if ChangelogId in Releases:
continue
Releases.append(ChangelogId)
author = commit.author.name
date = datetime.fromtimestamp(commit.authored_date, UTC_TIMESTAMP).strftime("%a, %d %b %Y %H:%M:%S GMT")
commitlink = SITE_URL + "changelog.html#" + ChangelogId
RSSFeed += \
"<item>" +\
f"<title>Update to the Nekoweb Wiki</title>" +\
f"<link>{commitlink}</link>" +\
f"<description>The Nekoweb Wiki has now been updated.</description>" +\
f"<pubDate>{date}</pubDate>" +\
f"<guid>{ChangelogId}</guid>" +\
"</item>"
RSSFeed = \
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +\
"<rss version=\"2.0\">" +\
"<channel>" +\
"<title>Nekoweb Wiki</title>" +\
f"<link>{SITE_URL}</link>" +\
"<lang>en-us</lang>" +\
RSSFeed +\
"</channel>" +\
"</rss>"
return RSSFeed
def main():
global Indexed
InitDir(OUTPUT_DIR)
for source, dest in ADD_FILES.items():
GenericCopy(source, JoinPath(OUTPUT_DIR, dest))
for directory, info in DIRECTORIES.items():
output = JoinPath(OUTPUT_DIR, info["out"]).replace("\\", "/")
if info["static"]:
StaticDirectory(directory, output)
continue
InitDir(output, overwrite=False)
wikiparse(directory, output, rawinfo=info)
JinjaRender(
GetTemplate("pageindex.html"),
JoinPath("build", "pages.html"),
PAGE_TITLE = "Nekoweb Wiki - Page Index",
PAGE_DESC = "A list of pages on this wiki.",
PAGE_TYPE = "website",
LANG="en",
pages = Indexed,
)
JinjaRender(
GetTemplate("guestbook.html"),
JoinPath("build", "guestbook.html"),
PAGE_TITLE = "Nekoweb Wiki - Guestbook",
PAGE_DESC = "Sign the wiki guestbook!",
PAGE_TYPE = "website",
LANG="en",
)
JinjaRender(
GetTemplate("editor.html"),
JoinPath("build", "editor.html"),
PAGE_TITLE = "Nekoweb Wiki - Edit",
PAGE_DESC = "Edit wikipages",
PAGE_TYPE = "website",
LANG="en",
)
AddSyntaxColors()
RSSFeed = WriteFeed(
CommitBase = repo.iter_commits("HEAD", max_count=50)
)
with open(JoinPath(OUTPUT_DIR, "feed.xml"), "w", encoding="utf-8") as file:
file.write(RSSFeed)
if __name__ == "__main__":
main()