diff options
author | rusinthread <rusinthread@cock.li> | 2017-01-06 20:19:52 +0300 |
---|---|---|
committer | rusinthread <rusinthread@cock.li> | 2017-01-06 20:19:52 +0300 |
commit | 86af59be43f19cc612de9a13d1de6a0c03d7102f (patch) | |
tree | 9358ce06ccac66b12d37754f20f7c01adeb079f0 /gen_md.py | |
parent | 672ed1a11cbd33a8058c3af6db7d7c3ebc0c9fb8 (diff) |
use image previews on data.md page
Diffstat (limited to 'gen_md.py')
-rwxr-xr-x | gen_md.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/gen_md.py b/gen_md.py new file mode 100755 index 0000000..dbf44a2 --- /dev/null +++ b/gen_md.py @@ -0,0 +1,86 @@ +#!/usr/bin/python3.4 +import datetime +import time +import sys +import os +import shutil +import subprocess +from pprint import pprint +from data_lib import load_data, decode_auto + +CWD = os.path.dirname(os.path.realpath(__file__)) + +def sort_data_by_date(item): + return int(time.mktime(datetime.datetime.strptime(item['date'], '%d/%m/%y').timetuple())) + +def resize(in_path, out_path): + subprocess.call(['convert', in_path, '-resize', '250', out_path]) + +def gen_previews(): + img_dir = os.path.join(CWD, 'img') + img_previews_dir = os.path.join(CWD, 'img_previews') + if os.path.exists(img_previews_dir): + shutil.rmtree(img_previews_dir) + + os.makedirs(img_previews_dir) + for img in os.listdir(img_dir): + img_path = os.path.join(img_dir, img) + img_preview_path = os.path.join(img_previews_dir, img) + + resize(img_path, img_preview_path) + +def main(): + data = load_data() + data = sorted(data, key=sort_data_by_date) + + print("Generating previews (don't forget to git add them)...") + gen_previews() + + print("Generating page...") + + buf = [] + for post in data: + cipher_type = post['type'] if 'type' in post else 1 + decoded_text = decode_auto(post['text'], cipher_type) + + post_buf = '' + post_buf += '**Дата**: %s\n\n' % post['date'] + + if 'pic' in post and post['pic']: + # make sure it is a list + pic = post['pic'] if isinstance(post['pic'], list) else [post['pic']] + + pic_buf = [] + for p in pic: + pic_buf.append('![](./img_previews/%s =300x)' % p) + post_buf += '**Пикрилейтед:** %s\n\n' % ''.join(pic_buf) + + if 'link' in post: + # make sure it is a list + link = post['link'] if isinstance(post['link'], list) else [post['link']] + + link_buf = [] + for l in link: + link_buf.append('[%s](%s)' % (l, l)) + post_buf += '**Ссылки:** %s\n\n' % ', '.join(link_buf) + + if 'source' in post: + post_buf += '**Источник:** %s' % post['source'] + if 'source_link' in post: + post_buf += ', [%s](%s)\n\n' % (post['source_link'], post['source_link']) + else: + post_buf += '\n\n' + + post_buf += '**Шифровка (тип %d)**: \n```\n%s\n```\n\n' % (cipher_type, post['text']) + post_buf += '**Расшифровка:** `%s`' % decoded_text + + buf.append(post_buf) + + md = "\n---\n".join(buf) + with open(os.path.join(CWD, 'data.md'), 'w') as f: + f.write(md) + + print("Done.") + +if __name__ == '__main__': + sys.exit(main()) |