From fdf5165ebb37cf7cf7c02bec0f7c35f800ade755 Mon Sep 17 00:00:00 2001 From: Matt Singleton Date: Sat, 15 Jan 2022 12:50:28 -0600 Subject: html template --- TODO.md | 2 +- gemini.py | 19 +++++++++++-------- page_template.html | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 page_template.html diff --git a/TODO.md b/TODO.md index 0431ffb..c0e33d7 100644 --- a/TODO.md +++ b/TODO.md @@ -9,5 +9,5 @@ - [ ] work with search pages - [x] stdlib url parsing in gemini module - [x] nice typography - - [ ] html templates + - [x] html templates - [ ] deal with non utf8 charsets diff --git a/gemini.py b/gemini.py index 2d29236..e820619 100644 --- a/gemini.py +++ b/gemini.py @@ -2,6 +2,7 @@ import io import re import socket import ssl +import string import urllib.parse import fsm @@ -12,16 +13,18 @@ def htmlescape(text: str) -> str: def gem2html(gem: str) -> str: - html = io.StringIO() - html.write('\n\n\n\n\n\n\n\n
') - parser = fsm.Parser(gem.split('\n'), html) + template = string.Template(open('page_template.html').read()) + body = io.StringIO() + parser = fsm.Parser(gem.split('\n'), body) parser.parse() - html.write('
\n\n') + html = template.substitute( + body=body.getvalue(), + charset='utf-8', + lang='en', + ) with open('latest.html', 'w') as fp: - fp.write(html.getvalue()) - return html.getvalue() + fp.write(html) + return html def urljoin(base: str, url: str) -> str: diff --git a/page_template.html b/page_template.html new file mode 100644 index 0000000..2a4baf5 --- /dev/null +++ b/page_template.html @@ -0,0 +1,41 @@ + + + + + + + + +$body + + -- cgit v1.2.3