summaryrefslogtreecommitdiff
path: root/format.py
blob: 787349447cb483345243aefa551b8053b364e0a2 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3

# SPDX-License-Identifier: CC0-1.0
#
# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
#
# Available under the terms of Creative Commons Zero v1.0 Universal.

import json
import random
import click
import sys
from pathlib import Path

MAX_WIDTH = 80
INDENT    = 3

here = Path(__file__).resolve().parent

trans_suggestions = {
    'en': 'suggestions',
    'pl': 'sugestie'
}

trans_hint = {
    'en': 'Random digital freedom hint',
    'pl': 'Losowa wskazówka dla wolności cyfrowej'
}

trans_note = {
    'en': 'FSF concerns',
    'pl': 'zastrzeżenia FSF'
}

no_dangling_chars_langs = {'pl'}

def wrap_text_block(text, max_width, forbid_dangling_chars=False,
                    subsequent_indent=0):
    lines = []

    for paragraph in text.split('\n'):
        line = ''
        accumulated_word = ''

        for word in paragraph.split(' '):
            if not word:
                continue

            accumulated_word += word

            if forbid_dangling_chars and len(accumulated_word) == 1:
                accumulated_word += ' '
                continue

            if len(line) + len(accumulated_word) + 1 <= max_width:
                if line:
                    line += ' '
                line += accumulated_word
            else:
                lines.append(line)
                line = ' ' * subsequent_indent + accumulated_word

            accumulated_word = ''

        if line and line != ' ' * subsequent_indent:
            lines.append(line)
            line = ''

    return lines

def format_hint(hint, hint_id, langs, max_width=MAX_WIDTH, indent=INDENT):
    lines = []

    for lang in langs:
        lines.append(f'{trans_hint[lang]} #{hint_id}: {hint["name"][lang]}')

        block = wrap_text_block(hint['text'][lang], max_width - indent,
                                lang in no_dangling_chars_langs)

        lines.extend(' ' * indent + l for l in block)

    suggested = hint.get('suggested')
    if suggested:
        lines.append('/'.join(trans_suggestions[l] for l in langs) + ':')

        for sug_id, sug in enumerate(suggested):
            sug_line = f'{sug_id + 1}. {sug["name"]} <{sug["url"]}>'
            block = wrap_text_block(sug_line, max_width,
                                    lang in no_dangling_chars_langs, indent)
            lines.extend(block)

            note = sug.get('note')
            if not note:
                continue

            for lang in langs:
                block = wrap_text_block(f'{trans_note[lang]}: {note[lang]}',
                                        max_width - indent,
                                        lang in no_dangling_chars_langs, indent)

                lines.extend(' ' * indent + l for l in block)

    return '\n'.join(lines)

hints = json.load((here / 'hints.json').open())

@click.command()
@click.option('-i', '--hint-id', default='-1', type=click.types.INT,
              show_default=True, help='which hint to format (-1 for random)')
def main(hint_id):
    if hint_id == -1:
        [(hint_id, hint)] = random.sample([*hints.items()], 1)
    else:
        hint_id = str(hint_id)
        hint = hints.get(hint_id)

        if hint is None:
            print("No such hint!", file=sys.stderr)
            return sys.exit(1)

    print(format_hint(hint, hint_id, [*hint['name'].keys()]))

if __name__ == '__main__':
    main()