summaryrefslogtreecommitdiff
path: root/generatesite.py
blob: a5c0e171413a6f8141c13d16b3306f5f90ded769 (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python3
'''
Copyright Daniel Jones daniel@danieljon.es

script to auto-generate my static website/blog at https://danieljon.es

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import configparser;
import errno;
import os;
import glob;
import sys;
from shutil import copyfile;
from tempfile import mkstemp;
from shutil import move;
from os import fdopen, remove;
from time import gmtime, strftime;

def replace(file_path, pattern, subst):
    fh, abs_path = mkstemp();
    with fdopen(fh, "w") as new_file:
        with open(file_path) as old_file:
            for line in old_file:
                new_file.write(line.replace(pattern, subst));
    remove(file_path);
    move(abs_path, file_path);

def generateindex(special):
    if special:
        outdir = specialoutput;
    else:
        outdir = cfg.get("output", "dir");
    indexsrc = cfg.get("index", "src");
    print("generating {}/index.html from {}".format(outdir, indexsrc));
    copyfile(template, outdir + "/index.html");
    with open(indexsrc, "r") as contentfile:
        content = contentfile.read();
    replace(outdir + "/index.html", "{TITLE}", cfg.get("index", "title"));
    replace(outdir + "/index.html", "{INFO}", cfg.get("index", "header"));
    replace(outdir + "/index.html", "{CONTENT}", content);
    replace(outdir + "/index.html", "{TIME}", strftime("%d-%m-%Y", gmtime()));

def generatepagebar(currentpage, pagecount):
    print("generating page bar for {} total {}".format(currentpage, pagecount));
    pages = "<div class='middle'> <a href='" + str(pagecount if int(currentpage) == 1 else int(currentpage)-1) + "'>prev</a> ";
    for x in range(1, int(pagecount)+1):
        if x == int(currentpage):
            pages += "<strong><i>" + str(x) + "</i></strong> ";
        else: 
            pages += "<a href=" + str(x) + ">" + str(x) + "</a> ";
    pages += "<a href='" + str(1 if currentpage == pagecount else int(currentpage)+1) + "'>next</a></div>";
    return pages;

def generateportfolio(special):
    if special:
        outdir = specialoutput;
    else:
       outdir = cfg.get("output", "dir");
    portfoliosrc = cfg.get("portfolio", "src");
    print("generating {}/portfolio.html from {}".format(outdir, portfoliosrc));
    copyfile(template, outdir + "/portfolio.html");
    with open(portfoliosrc, "r") as contentfile:
        content = contentfile.read();
    replace(outdir + "/portfolio.html", "{TITLE}", cfg.get("portfolio", "title"));
    replace(outdir + "/portfolio.html", "{INFO}", cfg.get("portfolio", "header"));
    replace(outdir + "/portfolio.html", "{CONTENT}", content);
    replace(outdir + "/portfolio.html", "{TIME}", strftime("%d-%m-%Y", gmtime()));

def deletefiles(ddir):
     # delete all blog files
    for the_file in os.listdir(ddir):
        file_path = os.path.join(ddir, the_file)
        if os.path.isfile(file_path):
            os.unlink(file_path);

def generateblog(special):
    if special:
        outdir = specialoutput;
    else:
        outdir = cfg.get("output", "dir");
    blogdir = cfg.get("blog", "dir");
    directdir = cfg.get("blog", "direct");
    blogsrc = cfg.get("blog", "srcdir");
    perpage = cfg.get("blog", "perpage");
    print("generating {}/{}/ from directory /{}/final".format(outdir, blogdir, blogsrc));
    os.makedirs(outdir + "/" + blogdir, exist_ok=True);
    os.makedirs(outdir + "/" + blogdir + "/" + directdir, exist_ok=True);
    os.makedirs(blogsrc + "/final", exist_ok=True);
   # number of blog posts
    postcount = len(glob.glob1(blogsrc, "*.txt"));
    pagecount = postcount/int(perpage)+1;
    print("{} posts, {} per page = {} pages".format(postcount, perpage, int(pagecount)));
    folder = outdir + "/" + blogdir;
    # delete generated blog files
    deletefiles(outdir + "/" + blogdir);
    # delete renamed blog files
    deletefiles(blogsrc + "/final");
    '''
    copy our blog posts to blogsrc/final, rename them, 60 becomes 1, 59 becomes 2 etc
    this is done because I am extremely lazy and had troubles doing it any other way.
    sorry if you're reading this.
    '''
    #integrate this with the loop below it?
    count = 1;
    for x in range(int(postcount), 0, -1):
        copyfile(blogsrc + "/" + str(x) + ".txt", blogsrc + "/final/" + str(count) + ".txt");
        count += 1;
    # generate pages
    for x in range(1, int(pagecount) + 1):
        copyfile(template, outdir + "/" + blogdir + "/" + str(x) + ".html");
        with open(outdir + "/" + blogdir + "/" + str(x) + ".html", "r") as contentfile:
            content = contentfile.read().replace('\n', '');
        replace(outdir + "/" + blogdir + "/" + str(x) + ".html", "{TITLE}", cfg.get("blog", "title"));
        replace(outdir + "/" + blogdir + "/" + str(x) + ".html", "{INFO}", cfg.get("blog", "header"));
        replace(outdir + "/" + blogdir + "/" + str(x) + ".html", "{TIME}", strftime("%d-%m-%Y", gmtime()));
    # place blog posts into their pages
    count = 1;
    cpage = 1;
    page = "";
    total_count = postcount;
    # count from 1 - perpage, add perpage to count, count from count - count + perpage
    while count < postcount + 1:
        for x in range(count, count + int(perpage)):
            if count < postcount + 1:
                with open(blogsrc + "/final/" + str(x) + ".txt") as contentfile:
                    content = contentfile.read();
                    postnum = str((postcount+1)-x);
                    if postnum == "101":
                        postnum = "5";
                    page += "<div id='" + str(total_count) + "'> post #" + postnum + "<br><a href='" + directdir + "/" + str(total_count) + "'>direct link</a>" + content + "</div><hr>";
                    count += 1;
                    total_count -= 1;
        if cpage <= pagecount:
            page += generatepagebar(str(cpage), str(int(pagecount)));
            page = generatepagebar(str(cpage), str(int(pagecount))) + page;
            replace(outdir + "/" + blogdir + "/" + str(cpage) + ".html", "{CONTENT}", page);
        page = "";
        cpage += 1;
    # generate /direct/ pages
    for x in range(1, int(postcount) + 1):
        copyfile(template, outdir + "/" + blogdir + "/" + directdir + "/" + str(x) + ".html");
        replace(outdir + "/" + blogdir + "/" + directdir + "/" + str(x) + ".html", "{TITLE}", cfg.get("blog", "title"));
        replace(outdir + "/" + blogdir + "/" + directdir + "/" + str(x) + ".html", "{INFO}", cfg.get("blog", "header"));
        replace(outdir + "/" + blogdir + "/" + directdir + "/" + str(x) + ".html", "{TIME}", strftime("%d-%m-%Y %H:%M:%S", gmtime()));
        with open(blogsrc + "/" + str(x) + ".txt") as contentfile:
            content = contentfile.read();
            replace(outdir + "/" + blogdir + "/" + directdir + "/" + str(x) + ".html", "{CONTENT}", content);
            replace(outdir + "/" + blogdir + "/" + directdir + "/" + str(x) + ".html", "media/", "../media/");




def generateopinions(special):
    if special:
        outdir = specialoutput;
    else:
        outdir = cfg.get("output", "dir");
    opinionssrc = cfg.get("opinions", "src");
    opinionsdir = cfg.get("opinions", "dir");
    os.makedirs(outdir + "/" + opinionsdir, exist_ok=True);
    print("generating {}/{}/index.html from {}".format(outdir, opinionsdir, opinionssrc));
    copyfile(template, outdir + "/" + opinionsdir + "/index.html");
    with open(opinionssrc, "r") as contentfile:
        content = contentfile.read();
    replace(outdir + "/" + opinionsdir + "/index.html", "{TITLE}", cfg.get("opinions", "title"));
    replace(outdir + "/" + opinionsdir + "/index.html", "{INFO}", cfg.get("opinions", "header"));
    replace(outdir + "/" + opinionsdir + "/index.html", "{CONTENT}", content);
    replace(outdir + "/" + opinionsdir + "/index.html", "{TIME}", strftime("%d-%m-%Y", gmtime()));
    # anime
    animesrc = cfg.get("anime", "src");
    print("generating {}/{}/index.html from {}".format(outdir, opinionsdir, animesrc));
    copyfile(template, outdir + "/" + opinionsdir + "/anime.html");
    with open(animesrc, "r") as contentfile:
        content = contentfile.read();
    replace(outdir + "/" + opinionsdir + "/anime.html", "{TITLE}", cfg.get("anime", "title"));
    replace(outdir + "/" + opinionsdir + "/anime.html", "{INFO}", cfg.get("anime", "header"));
    replace(outdir + "/" + opinionsdir + "/anime.html", "{CONTENT}", content);
    replace(outdir + "/" + opinionsdir + "/anime.html", "{TIME}", strftime("%d-%m-%Y", gmtime()));
    #everything else
    everythingsrc = cfg.get("everything", "src");
    print("generating {}/{}/index.html from {}".format(outdir, opinionsdir, everythingsrc));
    copyfile(template, outdir + "/" + opinionsdir + "/everything.html");
    with open(everythingsrc, "r") as contentfile:
        content = contentfile.read();
    replace(outdir + "/" + opinionsdir + "/everything.html", "{TITLE}", cfg.get("everything", "title"));
    replace(outdir + "/" + opinionsdir + "/everything.html", "{INFO}", cfg.get("everything", "header"));
    replace(outdir + "/" + opinionsdir + "/everything.html", "{CONTENT}", content);
    replace(outdir + "/" + opinionsdir + "/everything.html", "{TIME}", strftime("%d-%m-%Y", gmtime()));


def generatewaifus(special):
    if special:
        outdir = specialoutput;
    else:
        outdir = cfg.get("output", "dir");
    waifussrc = cfg.get("waifus", "src");
    waifusdir = cfg.get("waifus", "dir");
    os.makedirs(outdir + "/" + waifusdir, exist_ok=True);
    print("generating {}/{}/index.html from {}".format(outdir, waifusdir, waifussrc));
    copyfile(template, outdir + "/" + waifusdir + "/index.html");
    with open(waifussrc, "r") as contentfile:
        content = contentfile.read();
    replace(outdir + "/" + waifusdir + "/index.html", "{TITLE}", cfg.get("waifus", "title"));
    replace(outdir + "/" + waifusdir + "/index.html", "{INFO}", cfg.get("waifus", "header"));
    replace(outdir + "/" + waifusdir + "/index.html", "{CONTENT}", content);
    replace(outdir + "/" + waifusdir + "/index.html", "{TIME}", strftime("%d-%m-%Y", gmtime()));

def generateinteresting(special):
    if special:
        outdir = specialoutput;
    else:
       outdir = cfg.get("output", "dir");
    interestingsrc = cfg.get("interesting", "src");
    print("generating {}/interesting.html from {}".format(outdir, interestingsrc));
    copyfile(template, outdir + "/interesting.html");
    with open(interestingsrc, "r") as contentfile:
        content = contentfile.read();
    replace(outdir + "/interesting.html", "{TITLE}", cfg.get("interesting", "title"));
    replace(outdir + "/interesting.html", "{INFO}", cfg.get("interesting", "header"));
    replace(outdir + "/interesting.html", "{CONTENT}", content);
    replace(outdir + "/interesting.html", "{TIME}", strftime("%d-%m-%Y", gmtime()));

def generatelikes(special):
    if special:
        outdir = specialoutput;
    else:
       outdir = cfg.get("output", "dir");
    likesrc = cfg.get("like", "src");
    print("generating {}/like.html from {}".format(outdir, likesrc));
    copyfile(template, outdir + "/like.html");
    with open(likesrc, "r") as contentfile:
        content = contentfile.read();
    replace(outdir + "/like.html", "{TITLE}", cfg.get("like", "title"));
    replace(outdir + "/like.html", "{INFO}", cfg.get("like", "header"));
    replace(outdir + "/like.html", "{CONTENT}", content);
    replace(outdir + "/like.html", "{TIME}", strftime("%d-%m-%Y", gmtime()));


def generatedislikes(special):
    if special:
        outdir = specialoutput;
    else:
       outdir = cfg.get("output", "dir");
    dislikesrc = cfg.get("dislike", "src");
    print("generating {}/dislike.html from {}".format(outdir, dislikesrc));
    copyfile(template, outdir + "/dislike.html");
    with open(dislikesrc, "r") as contentfile:
        content = contentfile.read();
    replace(outdir + "/dislike.html", "{TITLE}", cfg.get("dislike", "title"));
    replace(outdir + "/dislike.html", "{INFO}", cfg.get("dislike", "header"));
    replace(outdir + "/dislike.html", "{CONTENT}", content);
    replace(outdir + "/dislike.html", "{TIME}", strftime("%d-%m-%Y", gmtime()));

if __name__ == "__main__":
    cfg = configparser.ConfigParser();
    cfg.read("settings.cfg");
    orgoutdir = cfg.get("output", "dir");
    if len(sys.argv) > 2:
        template = sys.argv[1];
        special = True; # if set output will be output/sys.argv[2]
        specialoutput = orgoutdir + "/" + sys.argv[2];
    else:
        special = False;
        template = cfg.get("output", "template");
    os.makedirs(orgoutdir, exist_ok=True);
    generateindex(special); # index
    generateblog(special); # blog with individual pages
    generateportfolio(special); # portfolio
    generateopinions(special); # opinions/anime
    generatewaifus(special); # my waifus
    generateinteresting(special); # interesting stuff
    generatelikes(special); # likes
    generatedislikes(special); #dislikes