#!/usr/bin/env python3 # # SPDX-License-Identifier: ISC # # Copyright © 2019 Free Software Foundation of India. # import datetime import os import os.path import re import subprocess as subp import sys SECTIONS = ['news', 'article'] # placeholders PH = { 'title': '', 'author': '', 'date': '', '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'([a-zA-Z\-]+)\.md', p) if not m: err('Unable to get slug') return m.group(1) def title(c): m = re.search(r'^\# (.+)$', c, re.M) if not m: err('Title not found') return m.group(1) def author(c): m = re.search(r'', c) if not m: return '' return 'By ' + m.group(1) def date(c): m = re.search(r'pubdate: ([0-9]{8})', c) if not m: err('Publication date not found') return m.group(1) def content(c): m = re.search(r'^\# (.+)$', c, re.M) if not m: err('Unable to slurp content') return c[m.end():] def template(type): return read('templates/html/{}.html'.format(type)) def datefmt(d): return datetime.datetime.strptime(d, '%Y%m%d').strftime('%B %d, %Y') def markdown(c): try: r = subp.run(['bin/markdown'], input=c, stdout=subp.PIPE, check=True, universal_newlines=True) except Exception as e: p('Markdown failed for {}'.format(c)) return r.stdout def html(sec, f): c = read(f.path) s = slug(f.path) t = title(c) a = author(c) d = date(c) c = content(c) h = template(sec) h = h.replace(PH['title'], t, 2) h = h.replace(PH['date'], datefmt(d), 1) h = h.replace(PH['content'], markdown(c), 1) if author: h = h.replace(PH['author'], a, 1) return s, h def process(sec): for f in files(sec): s, h = html(sec, f) write('/'.join(['_build', sec, s, 'index.html']), h) def run(): for sec in SECTIONS: process(sec) if __name__ == "__main__": run()