diff options
-rw-r--r-- | _posts/2017/12/movies.rst (renamed from _posts/2017/movies.rst) | 4 | ||||
-rw-r--r-- | rss.py | 73 |
2 files changed, 40 insertions, 37 deletions
diff --git a/_posts/2017/movies.rst b/_posts/2017/12/movies.rst index 10fcbc3..94ecee5 100644 --- a/_posts/2017/movies.rst +++ b/_posts/2017/12/movies.rst | |||
@@ -2,9 +2,11 @@ | |||
2 | 2 | ||
3 | .. _2017movie: | 3 | .. _2017movie: |
4 | 4 | ||
5 | |||
5 | 2017观影记录 | 6 | 2017观影记录 |
6 | =================== | 7 | =================== |
7 | 8 | ||
9 | :Publish Date: 2010-01-01 | ||
8 | 10 | ||
9 | 2017年马上就结束了。掐指一算,2017年看了好多电影。包括线上和线下电影院的。 | 11 | 2017年马上就结束了。掐指一算,2017年看了好多电影。包括线上和线下电影院的。 |
10 | 12 | ||
@@ -67,7 +69,7 @@ | |||
67 | 69 | ||
68 | 简单分析了一下,线下观影的统计 | 70 | 简单分析了一下,线下观影的统计 |
69 | 71 | ||
70 | .. image:: ../../_static/img/2017movie.png | 72 | .. image:: ../../../_static/img/2017movie.png |
71 | :alt: 2017movie | 73 | :alt: 2017movie |
72 | :align: center | 74 | :align: center |
73 | 75 | ||
@@ -1,68 +1,69 @@ | |||
1 | #!/usr/bin/env python | ||
2 | # -*- coding: utf-8 -*- | ||
3 | # Created by [email protected] at 2015-07-30 | ||
4 | |||
5 | import os | 1 | import os |
6 | import argparse | ||
7 | import yaml | ||
8 | |||
9 | import jinja2 | 2 | import jinja2 |
10 | 3 | ||
4 | import glob | ||
5 | from bs4 import BeautifulSoup | ||
11 | 6 | ||
12 | class FilePathLoader(jinja2.BaseLoader): | ||
13 | """ Custom Jinja2 template loader which just loads a single template file """ | ||
14 | 7 | ||
8 | class FilePathLoader(jinja2.BaseLoader): | ||
15 | def __init__(self, cwd): | 9 | def __init__(self, cwd): |
16 | self.cwd = cwd | 10 | self.cwd = cwd |
17 | 11 | ||
18 | def get_source(self, environment, template): | 12 | def get_source(self, environment, template): |
19 | # Path | ||
20 | filename = os.path.join(self.cwd, template) | 13 | filename = os.path.join(self.cwd, template) |
21 | 14 | ||
22 | # Read | ||
23 | try: | 15 | try: |
24 | with open(filename, 'r') as f: | 16 | with open(filename, 'r') as f: |
25 | contents = f.read() | 17 | contents = f.read() |
26 | except IOError: | 18 | except IOError: |
27 | raise jinja2.TemplateNotFound(filename) | 19 | raise jinja2.TemplateNotFound(filename) |
28 | 20 | ||
29 | # Finish | 21 | return contents, filename, lambda: False |
30 | uptodate = lambda: False | ||
31 | return contents, filename, uptodate | ||
32 | 22 | ||
33 | 23 | ||
34 | def render_template(cwd, template_path, context): | 24 | def render_template(cwd, template_path, context): |
35 | env = jinja2.Environment(loader=FilePathLoader(cwd)) | 25 | env = jinja2.Environment(loader=FilePathLoader(cwd)) |
26 | return env.get_template(template_path).render(context) | ||
36 | 27 | ||
37 | return env \ | 28 | |
38 | .get_template(template_path) \ | 29 | def _remove_attrs(soup): |
39 | .render(context) | 30 | for tag in soup.findAll(True): |
31 | tag.attrs = None | ||
32 | return soup | ||
40 | 33 | ||
41 | 34 | ||
42 | def main(): | 35 | def main(): |
43 | parser = argparse.ArgumentParser() | 36 | |
44 | 37 | filenames = glob.glob("./_build/html/_posts/*/*/*.html") | |
45 | parser.add_argument('--dir', default='.', help='Template directory') | 38 | print(filenames) |
46 | parser.add_argument('--data', nargs='*', help='One or more YAML data file') | 39 | |
47 | parser.add_argument('--template', help='Template file to process') | 40 | posts = [] |
48 | parser.add_argument('--body', help='Body content') | 41 | |
49 | args = parser.parse_args() | 42 | for p in filenames: |
50 | 43 | soup = BeautifulSoup(open(p), "html5lib") | |
51 | context = dict() | 44 | body = soup.find_all("div", class_="body") |
52 | for data_f in args.data if args.data else []: | 45 | posts.append({ |
53 | key, filename = data_f.split(':') | 46 | "title": soup.title.string, |
54 | if filename: | 47 | "body": body[0].text, |
55 | context[key] = yaml.load(open(filename).read()) | 48 | "date_rss": "", |
56 | else: | 49 | "permalink": "/".join(p.split("/")[3:]) |
57 | context[key] = {'_': True} | 50 | }) |
58 | if args.body: | 51 | |
59 | context['body'] = open(args.body).read() | 52 | context = { |
53 | "site": { | ||
54 | "name": "Hello World", | ||
55 | "url": "https://blog.jinwei.me", | ||
56 | "tagline": "Freedom is my birthright and I shall have it." | ||
57 | }, | ||
58 | "posts": posts | ||
59 | } | ||
60 | 60 | ||
61 | return render_template( | 61 | return render_template( |
62 | args.dir, | 62 | ".", |
63 | args.template, | 63 | "all.rss.xml", |
64 | context | 64 | context |
65 | ) | 65 | ) |
66 | 66 | ||
67 | |||
67 | if __name__ == '__main__': | 68 | if __name__ == '__main__': |
68 | print(main()) | 69 | print(main()) |