From 3470d47f0a2d14cb59f004e15e554cfa29fd29d2 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Thu, 16 May 2019 18:45:50 -0400 Subject: bin/news -> bin/html --- Makefile | 6 +-- bin/html | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/news | 134 --------------------------------------------------------------- 3 files changed, 137 insertions(+), 137 deletions(-) create mode 100644 bin/html delete mode 100644 bin/news diff --git a/Makefile b/Makefile index 255d0b8..12acd3a 100644 --- a/Makefile +++ b/Makefile @@ -16,9 +16,9 @@ static: $(BUILD_DIR) .PHONY: static -news: - @python3 ./bin/news -.PHONY: news +html: + @python3 ./bin/html +.PHONY: html css: $(CSS) diff --git a/bin/html b/bin/html new file mode 100644 index 0000000..503a254 --- /dev/null +++ b/bin/html @@ -0,0 +1,134 @@ +#!/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 + + +# placeholders +PH = { + 'title': '', + 'date': '', + 'content': '', +} + + +def err(s): + print('Error: {}'.format(s)) + sys.exit(1) + + +def files(): + return os.scandir('md/news') + + +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 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(t, d, c): + h = template('news') + h = h.replace(PH['title'], t, 2) + h = h.replace(PH['date'], datefmt(d), 1) + h = h.replace(PH['content'], markdown(c), 1) + + return h + + +def process(f): + c = read(f.path) + + s = slug(f.path) + t = title(c) + d = date(c) + c = content(c) + + h = html(t, d, c) + + write('/'.join(['_build', 'news', s, 'index.html']), h) + + +def run(): + for f in files(): + process(f) + + +if __name__ == "__main__": + run() diff --git a/bin/news b/bin/news deleted file mode 100644 index 503a254..0000000 --- a/bin/news +++ /dev/null @@ -1,134 +0,0 @@ -#!/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 - - -# placeholders -PH = { - 'title': '', - 'date': '', - 'content': '', -} - - -def err(s): - print('Error: {}'.format(s)) - sys.exit(1) - - -def files(): - return os.scandir('md/news') - - -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 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(t, d, c): - h = template('news') - h = h.replace(PH['title'], t, 2) - h = h.replace(PH['date'], datefmt(d), 1) - h = h.replace(PH['content'], markdown(c), 1) - - return h - - -def process(f): - c = read(f.path) - - s = slug(f.path) - t = title(c) - d = date(c) - c = content(c) - - h = html(t, d, c) - - write('/'.join(['_build', 'news', s, 'index.html']), h) - - -def run(): - for f in files(): - process(f) - - -if __name__ == "__main__": - run() -- cgit v1.2.3