diff options
Diffstat (limited to 'rss.py')
-rw-r--r-- | rss.py | 64 |
1 files changed, 0 insertions, 64 deletions
@@ -1,64 +0,0 @@ | |||
1 | import glob | ||
2 | import os | ||
3 | |||
4 | import jinja2 | ||
5 | from bs4 import BeautifulSoup | ||
6 | |||
7 | |||
8 | class FilePathLoader(jinja2.BaseLoader): | ||
9 | def __init__(self, cwd): | ||
10 | self.cwd = cwd | ||
11 | |||
12 | def get_source(self, environment, template): | ||
13 | filename = os.path.join(self.cwd, template) | ||
14 | |||
15 | try: | ||
16 | with open(filename, 'r') as f: | ||
17 | contents = f.read() | ||
18 | except IOError: | ||
19 | raise jinja2.TemplateNotFound(filename) | ||
20 | |||
21 | return contents, filename, lambda: False | ||
22 | |||
23 | |||
24 | def render_template(cwd, template_path, context): | ||
25 | env = jinja2.Environment(loader=FilePathLoader(cwd)) | ||
26 | return env.get_template(template_path).render(context) | ||
27 | |||
28 | |||
29 | def main(): | ||
30 | |||
31 | filenames = glob.glob("./_build/html/_posts/*/*/*.html") | ||
32 | posts = [] | ||
33 | |||
34 | for file in filenames: | ||
35 | soup = BeautifulSoup(open(file), "html5lib") | ||
36 | body = soup.find_all("div", class_="body")[0].text | ||
37 | |||
38 | posts.append({ | ||
39 | "title": soup.title.string, | ||
40 | "body": body, | ||
41 | "date_rss": body[body.find("Publish Date:")+13:body.find("Publish Date:")+23], | ||
42 | "permalink": "/".join(file.split("/")[3:]) | ||
43 | }) | ||
44 | with open(file, "w") as file: | ||
45 | file.write(str(BeautifulSoup(str(soup).replace("\n", "").replace("\r", ""), "html5lib").prettify())) | ||
46 | |||
47 | context = { | ||
48 | "site": { | ||
49 | "name": "Hello World", | ||
50 | "url": "https://blog.jinwei.me", | ||
51 | "tagline": "Freedom is my birth right and I shall have it." | ||
52 | }, | ||
53 | "posts": posts | ||
54 | } | ||
55 | |||
56 | return render_template( | ||
57 | ".", | ||
58 | "all.rss.xml", | ||
59 | context | ||
60 | ) | ||
61 | |||
62 | |||
63 | if __name__ == '__main__': | ||
64 | print(main()) | ||