From 2c3850c38b0e2c75fb1fc7023563ab3d9a6a357b Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Thu, 11 Jan 2018 11:30:15 +0800 Subject: + init --- all.rss.xml | 18 ++++++++++++++++ blog.iml | 9 -------- rss.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 all.rss.xml delete mode 100644 blog.iml create mode 100644 rss.py diff --git a/all.rss.xml b/all.rss.xml new file mode 100644 index 0000000..cafc7fd --- /dev/null +++ b/all.rss.xml @@ -0,0 +1,18 @@ + + + + {{ site.name }} + {{ site.url }} + {{ site.tagline }} + + {% for post in posts[:20] %} + + {{ post.title | e }} + {{ post.body | e }} + {{ post.date_rss }} + {{ site.url }}/{{ post.permalink }} + {{ site.url }}/{{ post.permalink }} + + {% endfor %} + + diff --git a/blog.iml b/blog.iml deleted file mode 100644 index ad3c0a3..0000000 --- a/blog.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/rss.py b/rss.py new file mode 100644 index 0000000..fbf70d1 --- /dev/null +++ b/rss.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Created by i@BlahGeek.com at 2015-07-30 + +import os +import argparse +import yaml + +import jinja2 + + +class FilePathLoader(jinja2.BaseLoader): + """ Custom Jinja2 template loader which just loads a single template file """ + + def __init__(self, cwd): + self.cwd = cwd + + def get_source(self, environment, template): + # Path + filename = os.path.join(self.cwd, template) + + # Read + try: + with open(filename, 'r') as f: + contents = f.read() + except IOError: + raise jinja2.TemplateNotFound(filename) + + # Finish + uptodate = lambda: False + return contents, filename, uptodate + + +def render_template(cwd, template_path, context): + env = jinja2.Environment(loader=FilePathLoader(cwd)) + + return env \ + .get_template(template_path) \ + .render(context) + + +def main(): + parser = argparse.ArgumentParser() + + parser.add_argument('--dir', default='.', help='Template directory') + parser.add_argument('--data', nargs='*', help='One or more YAML data file') + parser.add_argument('--template', help='Template file to process') + parser.add_argument('--body', help='Body content') + args = parser.parse_args() + + context = dict() + for data_f in args.data if args.data else []: + key, filename = data_f.split(':') + if filename: + context[key] = yaml.load(open(filename).read()) + else: + context[key] = {'_': True} + if args.body: + context['body'] = open(args.body).read() + + return render_template( + args.dir, + args.template, + context + ) + +if __name__ == '__main__': + print(main()) -- cgit v1.2.3