summaryrefslogtreecommitdiff
path: root/gen_md.py
blob: cf4e2396af547dc9966a43a54abdf31ca063c537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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/%s](![](./img_previews/%s))' % (p, p))
            post_buf += '**Пикрилейтед:**\n\n%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> %s\n\n' % (cipher_type, post['text'])
        post_buf += '**Расшифровка:**\n> %s' % decoded_text
        
        buf.append(post_buf)

    md = "\n\n\n\n---------\n\n\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())