From a771e4fe163ab598d61687c27ee344925ec577d2 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Tue, 30 Jun 2020 11:34:58 +0800 Subject: + fix --- rss.py | 64 ---------------------------------------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 rss.py (limited to 'rss.py') diff --git a/rss.py b/rss.py deleted file mode 100644 index af9f0ca..0000000 --- a/rss.py +++ /dev/null @@ -1,64 +0,0 @@ -import glob -import os - -import jinja2 -from bs4 import BeautifulSoup - - -class FilePathLoader(jinja2.BaseLoader): - def __init__(self, cwd): - self.cwd = cwd - - def get_source(self, environment, template): - filename = os.path.join(self.cwd, template) - - try: - with open(filename, 'r') as f: - contents = f.read() - except IOError: - raise jinja2.TemplateNotFound(filename) - - return contents, filename, lambda: False - - -def render_template(cwd, template_path, context): - env = jinja2.Environment(loader=FilePathLoader(cwd)) - return env.get_template(template_path).render(context) - - -def main(): - - filenames = glob.glob("./_build/html/_posts/*/*/*.html") - posts = [] - - for file in filenames: - soup = BeautifulSoup(open(file), "html5lib") - body = soup.find_all("div", class_="body")[0].text - - posts.append({ - "title": soup.title.string, - "body": body, - "date_rss": body[body.find("Publish Date:")+13:body.find("Publish Date:")+23], - "permalink": "/".join(file.split("/")[3:]) - }) - with open(file, "w") as file: - file.write(str(BeautifulSoup(str(soup).replace("\n", "").replace("\r", ""), "html5lib").prettify())) - - context = { - "site": { - "name": "Hello World", - "url": "https://blog.jinwei.me", - "tagline": "Freedom is my birth right and I shall have it." - }, - "posts": posts - } - - return render_template( - ".", - "all.rss.xml", - context - ) - - -if __name__ == '__main__': - print(main()) -- cgit v1.2.3