From 6a743a7ef8920cca31b833d26a093b84832b0362 Mon Sep 17 00:00:00 2001 From: neodarz Date: Sun, 26 May 2019 18:06:28 +0200 Subject: Move preview to external file --- cli/cli.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'cli/cli.py') diff --git a/cli/cli.py b/cli/cli.py index 7a5c4bd0..ed2680f4 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -11,6 +11,8 @@ import fileinput import time +import signal + from config.config import * from utils import utils @@ -319,3 +321,37 @@ def gen_deploy(args): generators.generate_blog(fresh=True) deploy(None) + + +def preview(args): + """Serve the blog and auto regenerate upon changes.""" + + # pylint: disable=unused-argument + + server_process = utils.HTTPServerProcess(BUILDDIR) + server_process.start() + sys.stderr.write("watching for changes\n") + sys.stderr.write("send SIGINT to stop\n") + + # install a SIGINT handler only for this process + sigint_raised = False + + def sigint_mitigator(signum, frame): + """Translate SIGINT to setting the sigint_raised flag.""" + nonlocal sigint_raised + sigint_raised = True + + signal.signal(signal.SIGINT, sigint_mitigator) + + # Watch and auto-regen. + # No need to actually implement watch separately, since + # generate_blog(fresh=False, report_total_errors=False) already + # watches for modifications and only regens upon changes, and it is + # completely silent when there's no change. + while not sigint_raised: + generators.generate_blog(fresh=False, report_total_errors=False) + time.sleep(0.5) + + sys.stderr.write("\nSIGINT received, cleaning up...\n") + server_process.join() + return 0 -- cgit v1.2.1