Add standard pages to category lists; Code cleaning

This commit is contained in:
2022-06-26 00:06:34 +02:00
parent 26e24036b5
commit f6291726f6
3 changed files with 144 additions and 103 deletions

52
Source/Modules/Feed.py Normal file
View File

@ -0,0 +1,52 @@
""" ================================= |
| This file is part of |
| staticoso |
| Just a simple Static Site Generator |
| |
| Licensed under the AGPLv3 license |
| Copyright (C) 2022, OctoSpacc |
| ================================= """
from Libs.feedgen.feed import FeedGenerator
from Modules.Utils import *
def MakeFeed(Pages, SiteName, SiteTagline, SiteDomain, MaxEntries, Lang, Minify=False):
Feed = FeedGenerator()
Link = SiteDomain if SiteDomain else ' '
Feed.id(Link)
Feed.title(SiteName if SiteName else ' ')
Feed.link(href=Link, rel='alternate')
Feed.description(SiteTagline if SiteTagline else ' ')
if SiteDomain:
Feed.logo(SiteDomain.rstrip('/') + '/favicon.png')
Feed.language(Lang)
DoPages = []
for e in Pages:
if MaxEntries != 0 and e[3]['Type'] == 'Post':
DoPages += [e]
MaxEntries -= 1
DoPages.reverse()
for File, Content, Titles, Meta, HTMLContent, Description, Image in DoPages:
if Meta['Type'] == 'Post':
Entry = Feed.add_entry()
File = '{}.html'.format(StripExt(File))
Content = ReadFile('public/'+File)
Link = SiteDomain+'/'+File if SiteDomain else ' '
CreatedOn = GetFullDate(Meta['CreatedOn'])
EditedOn = GetFullDate(Meta['EditedOn'])
Entry.id(Link)
Entry.title(Meta['Title'] if Meta['Title'] else ' ')
Entry.description(Description)
Entry.link(href=Link, rel='alternate')
Entry.content(HTMLContent, type='html')
if CreatedOn:
Entry.pubDate(CreatedOn)
EditedOn = EditedOn if EditedOn else CreatedOn if CreatedOn and not EditedOn else '1970-01-01T00:00+00:00'
Entry.updated(EditedOn)
os.mkdir('public/feed')
Feed.atom_file('public/feed/atom.xml', pretty=(not Minify))
Feed.rss_file('public/feed/rss.xml', pretty=(not Minify))

66
Source/Modules/Utils.py Normal file
View File

@ -0,0 +1,66 @@
""" ================================= |
| This file is part of |
| staticoso |
| Just a simple Static Site Generator |
| |
| Licensed under the AGPLv3 license |
| Copyright (C) 2022, OctoSpacc |
| ================================= """
import json
import os
from datetime import datetime
def ReadFile(p):
try:
with open(p, 'r') as f:
return f.read()
except Exception:
print("Error reading file {}".format(p))
return None
def WriteFile(p, c):
try:
with open(p, 'w') as f:
f.write(c)
return True
except Exception:
print("Error writing file {}".format(p))
return False
def FileToStr(File, Truncate=''):
return str(File)[len(Truncate):]
def StripExt(Path):
return ".".join(Path.split('.')[:-1])
def UndupeStr(Str, Known, Split):
while Str in Known:
Sections = Title.split(Split)
try:
Sections[-1] = str(int(Sections[-1]) + 1)
except ValueError:
Sections[-1] = Sections[-1] + str(Split) + '2'
Str = Split.join(Sections)
return Str
def DashifyStr(s, Limit=32):
Str, lc = '', Limit
for c in s[:Limit].replace(' ','-').replace(' ','-'):
if c.lower() in '0123456789qwfpbjluyarstgmneiozxcdvkh-':
Str += c
return '-' + Str
def GetFullDate(Date):
if not Date:
return None
return datetime.strftime(datetime.strptime(Date, '%Y-%m-%d'), '%Y-%m-%dT%H:%M+00:00')
def LoadLocale(Lang):
Lang = Lang + '.json'
Folder = os.path.dirname(os.path.abspath(__file__)) + '/../../Locale/'
File = ReadFile(Folder + Lang)
if File:
return json.loads(File)
else:
return json.loads(ReadFile(Folder + 'en.json'))