-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneodym.py
More file actions
executable file
·241 lines (176 loc) · 6.29 KB
/
Copy pathneodym.py
File metadata and controls
executable file
·241 lines (176 loc) · 6.29 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#
# Neodym
#
# Copyright (C) by Andreas Zoglauer and contributors
# Please see the license file for more details.
#
# -----------------------------------------------------------------------------------
# Import external files
import os
import sys
import shutil
from collections import OrderedDict
# Import neodym files
from configuration import ZConfiguration
from reader import ZReader
from article import ZArticle
from blog import ZBlog
from pictureshow import ZPictureShow
from bibliography import ZBibliography
# -----------------------------------------------------------------------------------
def CopyFiles(Article):
for FileName in Article.mFiles:
CopyFile(FileName)
for Name in Article.mCSSFileNames:
CopyFile(Name)
for Name in Article.mJavaScriptFileNames:
CopyFile(Name)
# -----------------------------------------------------------------------------------
def CopyFile(FileName):
NewFileName = Config.mTargetDirectory + os.sep + FileName
OldFileName = "content" + os.sep + FileName
# Check if the fike exists:
if os.path.isfile(OldFileName) == False:
OldFileName = Config.mTemplateDirectory + os.sep + FileName
if os.path.isfile(OldFileName) == False:
print("ERROR: Unable to find file: " + FileName)
Dirs = []
path = os.path.dirname(NewFileName)
while True:
path, folder = os.path.split(path)
if folder != "":
Dirs.append(folder)
else:
break
Dirs.reverse()
if len(Dirs) > 0 and Config.mTargetDirectory[0] == "/":
Dirs[0] = "/" + Dirs[0]
FullPath = ""
for Dir in Dirs:
if FullPath != "":
FullPath += os.sep + Dir
else:
FullPath += Dir
print("Checking to create path: ", FullPath)
if os.path.isdir(FullPath) == False:
os.makedirs(FullPath)
print("Copying: ", OldFileName, " --> ", NewFileName)
shutil.copy(OldFileName, NewFileName)
# -----------------------------------------------------------------------------------
def getMainIndex(Title, Menus):
Out="<!DOCTYPE html>\n"
Out += "<html>\n"
Out += "\n"
Out += "<head>\n"
Out += "\n"
Out += " <meta charset=\"UTF-8\">\n"
Out += " <title>" + Title + "</title>\n"
Out += "\n"
Out += " <script language=\"javascript\" type=\"text/javascript\">\n"
Out += " console.log('Width: %d', screen.width);\n"
Out += " window.location=\"" + list(Menus.values())[0].mFileName + "\";\n"
Out += " </script>\n"
Out += "\n"
Out += "</head>\n"
Out += "\n"
Out += "<body>\n"
Out += "\n"
Out += " <h1>You seem to have disabled Javascript. But unfortunately, this website's main engine is written in Javascript! Thus please, please enable it.</h1>\n"
Out += "\n"
Out += "</body>\n"
Out += "\n"
Out += "</html>\n"
return Out
# MAIN ------------------------------------------------------------------------------------------
Config = ZConfiguration()
Articles = []
Bibliographies = []
MenuTitles = []
# (1) Read and sanity check the configuration file
# (1a) Read it
if Config.read("neodym.conf") == False:
print("Error: Unable to read configuation file")
sys.exit(1)
Config.print()
# (1b) ... and do some sanity checks
if os.path.isdir(Config.mContentDirectory) == False:
print("Error: Unable to find content directory")
sys.exit(1)
if os.path.isdir(Config.mTemplateDirectory) == False:
print("Error: Unable to find template directory")
sys.exit(1)
if os.path.isfile(Config.mTemplateDirectory + os.sep + "neodym.js") == False:
print("Error: The template directory does not contain the templates: \"" + Config.mTemplateDirectory + "\"")
sys.exit(1)
if Config.mLogo != "" and os.path.isfile(Config.mContentDirectory + os.sep + Config.mLogo) == False:
print("Error: Unable to find Logo file")
sys.exit(1)
# (2) Read all the content files (html)
for SubDir, Dirs, Files in os.walk(Config.mContentDirectory):
for File in Files:
if File.endswith(".html") == False: continue
FilePath = SubDir + os.sep + File
print ("Reading %s..." % (FilePath))
Reader = ZReader()
Reader.read(FilePath)
if Reader.has("Type", "Article"):
Article = ZArticle()
Article.assimilate(Reader)
Articles.append(Article)
if len(Articles) == 0:
print("Error: No articles found")
sys.exit(1)
for A in Articles:
print("Found page with menu title: %s" % (A.mMenuTitle))
# (3) Creating the main menu based on the articles
mMenu = {}
for A in Articles:
mMenu[A.mMenuEntry] = A
mMenu = OrderedDict(sorted(mMenu.items()))
if len(mMenu) == 0:
print("Error: No menu entries found")
sys.exit(1)
print(mMenu)
# (4) Handle all neodym features
for A in Articles:
# (4a) Pictureshow
P = ZPictureShow()
P.apply(A)
# (4b) Bibliography
B = ZBibliography()
B.apply(A)
# (4c) Blog
Blog = ZBlog()
Blog.apply(A)
# (5) Start putting it all together and copy all files
# (5a) Create the target directory if it does not exit
if os.path.isdir(Config.mTargetDirectory) == False:
os.makedirs(Config.mTargetDirectory)
# (5b) Copy template files
shutil.copy(Config.mTemplateDirectory + os.sep + "neodym.js", Config.mTargetDirectory + os.sep + "neodym.js")
shutil.copy(Config.mTemplateDirectory + os.sep + "neodym.css", Config.mTargetDirectory + os.sep + "neodym.css")
if os.path.isfile(Config.mTemplateDirectory + os.sep + "neodym-user.css"):
shutil.copy(Config.mTemplateDirectory + os.sep + "neodym-user.css", Config.mTargetDirectory + os.sep + "neodym-user.css")
# (5c) Copy logo
if Config.mLogo != "":
CopyFile(Config.mLogo)
# (5d) Create index file
File = open(Config.mTargetDirectory + os.sep + "index.html","w")
File.write(getMainIndex(Config.mTitle, mMenu))
File.close()
# (5e) Create articles, add all features and copy all files associated with the article
for A in Articles:
# Create the articles
PageText = A.createPage(Config.mTitle, Config.mSubTitle, Config.mDescription, Config.mLogo, Config.mFooter, mMenu)
FileName = Config.mTargetDirectory + os.sep + os.path.basename(A.mFileName)
print(FileName)
File = open(FileName, "w")
File.write(PageText)
File.close()
# Copy all associated files:
CopyFiles(A)
# (5f) Set the correct permissions
os.system("chmod -R " + Config.mTargetPermissions + " " + Config.mTargetDirectory)
# ... and we are done!
print("DONE!")
# -----------------------------------------------------------------------------------