summaryrefslogtreecommitdiffstats
path: root/bin/feed
blob: b2300d79d5bf88d34c2b527ff5083e56c34624fb (plain) (blame)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
#
#   SPDX-License-Identifier: ISC
#
#   Copyright © 2019 Free Software Foundation of India.
#

import html
import datetime
import os
import os.path
import re
import sys

import mistune

URL = "https://fsf.org.in"
SECTIONS = ["news", "article", "case-study"]
TARGET = "/".join(["_build", "feed.atom"])

F_PH = {
    "updated": "<!-- Feed Updated -->",
    "entries": "<!-- Entries -->",
}

E_PH = {
    "id": "<!-- Entry Id -->",
    "title": "<!-- Entry Title -->",
    "link": "<!-- Entry Link -->",
    "updated": "<!-- Entry Updated -->",
    "content": "<!-- Entry Content -->",
}


def err(s):
    print("Error: {}".format(s))
    sys.exit(1)


def fok(f):
    p = os.path.basename(f.path)

    if re.search(r"(^[\.\#])|(~$)", p):
        return False

    return True


def files(sec):
    files = os.scandir("md" + "/" + sec)

    fs = []
    for f in files:
        if not fok(f):
            print("Ignoring {}".format(f.path))
        else:
            fs.append(f)

    return fs


def read(f):
    with open(f) as f:
        c = f.read()
    return c


def write(p, c):
    d = os.path.dirname(p)

    if not os.path.exists(d):
        os.makedirs(d)

    with open(p, "w") as f:
        f.write(c)


def slug(p):
    m = re.search(r"(([0-9a-zA-Z\-]+)(\.([a-z]{2}))?)\.md", p)

    if not m:
        err("Unable to get slug")

    return m.group(1), m.group(2), m.group(4)


def template(type):
    return read("templates/atom/{}.atom".format(type))


def title(c):
    m = re.search(r"^\# (.+)$", c, re.M)

    if not m:
        err("Title not found")

    return m.group(1)


def elink(sec, s, l):
    if l is None:
        return "/".join([URL, sec, s])
    else:
        return "/".join([URL, sec, s, l])


def time(c):
    m = re.search(r"pubdate: ([0-9]{8})", c)

    if not m:
        err("Publication date not found")

    d = m.group(1)
    d = datetime.datetime.strptime(d, "%Y%m%d").strftime("%Y-%m-%d")

    return d + "T00:00:00Z"


def markdown(c):
    try:
        r = mistune.markdown(c, False, parse_block_html=True, parse_inline_html=True)
    except Exception as e:
        err("Markdown parsing failed for {}".format(e))

    return r


def massage(c):
    c = html.escape(c)
    c = c.replace("\n", "&#xA;")
    c = re.sub(r" +", " ", c)

    return c


def content(c):
    m = re.search(r"^\# (.+)$", c, re.M)

    if not m:
        err("Unable to slurp content")

    c = c[m.end() :]
    c = markdown(c)

    return massage(c)


def now():
    n = datetime.datetime.today()

    return n.strftime("%Y-%m-%dT%H:%M:%SZ")


def entry(sec, f):
    c = read(f.path)
    u, s, l = slug(f.path)

    t = time(c)
    id = t + ":" + u

    e = template("entry")
    e = e.replace(E_PH["id"], id, 1)
    e = e.replace(E_PH["title"], title(c), 1)
    e = e.replace(E_PH["link"], elink(sec, s, l), 1)
    e = e.replace(E_PH["updated"], t, 1)
    e = e.replace(E_PH["content"], content(c), 1)

    return id, e


def esort(esd):
    ids = sorted(esd.keys(), reverse=True)

    es = []
    for id in ids:
        es.append(esd[id])

    return es


def feed(es):
    f = template("feed")
    f = f.replace(F_PH["updated"], now(), 1)
    f = f.replace(F_PH["entries"], es, 1)

    return f


def process(sec, esd):
    for f in files(sec):
        id, e = entry(sec, f)
        esd[id] = e

    return esd


def stringify(esd):
    return "".join(esort(esd))


def commit(es):
    write(TARGET, feed(es))


def run():
    esd = {}
    for sec in SECTIONS:
        esd = process(sec, esd)

    commit(stringify(esd))


if __name__ == "__main__":
    run()