summaryrefslogtreecommitdiffstats
path: root/linkcloud.py
blob: e74c9ca2072591c81d07ee5fd81f113a24df2262 (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
import os
import json
import logging
import random
from yattag import Doc
from pelican.generators import Generator
from pelican import signals

logger = logging.getLogger(__name__)

links = ""

# bulma color classes for buttons
colors = [
   'is-primary',
   'is-link',
   'is-info',
   'is-success',
   'is-warning',
   'is-danger',
   'is-primary is-light',
   'is-link is-light',
   'is-info is-light',
   'is-success is-light',
   'is-warning is-light',
   'is-danger is-light'
]

def gen_link_cloud (gen):
    global links
    global colors

    path = os.path.join (gen.settings.get ('PATH'), 'links.json')
    data = None;

    try:
        with open (path, 'r') as f:
            data = json.load (f);
    except Exception as e:
        logger.error('Exception occurred: %s', e, exc_info=gen.settings.get('DEBUG', False))

    doc, tag, text = Doc().tagtext()
    for cat, entries in data.items ():
        with tag ('h2'):
            text (cat)

        with tag ('div', klass='buttons'):
            for entry in entries:
                if 'show' in entry and entry['show'] is not False:
                    if 'date' in entry:
                        mtitle = '{}; viewed on {}'.format (entry['title'], entry['date'])
                    else:
                        mtitle = '{}.'.format (entry['title'])
                    with tag ('a', klass='button {}'.format (random.choice (colors)), href=entry['href'], title=mtitle):
                        text (entry['name'])

    links = doc.getvalue ()

def add_links (gen, metadata):
    gen.context['links'] = links

def register ():
    signals.initialized.connect(gen_link_cloud)
    signals.page_generator_context.connect(add_links)