From ecfe76a231a882fdc94e9dd318ae19d02b880ff4 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sat, 27 Nov 2021 21:39:44 -0500 Subject: [PATCH] [O] Catch errors in markdown formatting --- src/report/report.py | 50 ++++++++++++++++++++-------------- src/report/resources/style.css | 25 +++++++++++++++++ 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/report/report.py b/src/report/report.py index 09d86cd..0bf892f 100644 --- a/src/report/report.py +++ b/src/report/report.py @@ -1,6 +1,7 @@ import base64 import json import os.path +import traceback import webbrowser from pathlib import Path @@ -22,33 +23,42 @@ def generate_report() -> str: # Load markdown md = read(str(src_dir.joinpath('report_document.md'))).replace('\r\n', '\n').split('\n') - # Process @include statements + # Process line by line for i in range(len(md)): line = md[i] if not line.startswith('@include'): continue - path = line[line.index('`') + 1:] - path = path[:path.index('`')] - md[i] = read(REPORT_DIR + path) + # Process @include statements + try: + path = line[line.index('`') + 1:] + path = path[:path.index('`')] + md[i] = read(REPORT_DIR + path) - # Cut lines - # Format: @include-cut `path` [end, not inclusive] - if line.startswith('@include-cut'): - args = [int(i) for i in line.split()[2:]] - if len(args) == 1: - md[i] = '\n'.join(md[i].split('\n')[args[0]:]) - if len(args) == 2: - md[i] = '\n'.join(md[i].split('\n')[args[0]:args[1]]) + # Cut lines + # Format: @include-cut `path` [end, not inclusive] + if line.startswith('@include-cut'): + args = [int(i) for i in line.split()[2:]] + if len(args) == 1: + md[i] = '\n'.join(md[i].split('\n')[args[0]:]) + if len(args) == 2: + md[i] = '\n'.join(md[i].split('\n')[args[0]:args[1]]) - # Specific lines - # Format: @include-lines `path` <...lines> - # Example: @include-lines `path` 1 2 5 - if line.startswith('@include-lines'): - args = [int(i) for i in line.split()[2:]] - lines = md[i].split('\n') - lines = [lines[ln] for ln in range(len(lines)) if ln in args] - md[i] = '\n'.join(lines) + # Specific lines + # Format: @include-lines `path` <...lines> + # Example: @include-lines `path` 1 2 5 + if line.startswith('@include-lines'): + args = [int(i) for i in line.split()[2:]] + lines = md[i].split('\n') + lines = [lines[ln] for ln in range(len(lines)) if ln in args] + md[i] = '\n'.join(lines) + + # Handle errors. (It prompts "too broad an exception clause" but I actually need to catch + # every possible exception.) + except Exception as e: + md[i] = f"
" \
+                    f"\nInvalid @include statement. \n{traceback.format_exc()}" \
+                    f"
" return '\n'.join(md) diff --git a/src/report/resources/style.css b/src/report/resources/style.css index 328c6b3..a70db88 100644 --- a/src/report/resources/style.css +++ b/src/report/resources/style.css @@ -42,6 +42,31 @@ span.highlight { background-color: beige; } +pre.error { + background-color: #ffeef2; + color: #ff5b74; + padding: 0.8em 0.8em 0.8em 1em; +} + +/*blockquote {*/ +/* position: relative;*/ +/* margin: 0 0 1.1em 0;*/ +/* padding: 0.8em 0.8em 0.8em 1em;*/ +/* color: gray;*/ +/*}*/ + +/*blockquote:before {*/ +/* content: "";*/ +/* display: block;*/ +/* position: absolute;*/ +/* top: 0;*/ +/* bottom: 0;*/ +/* left: 0;*/ +/* width: 4px;*/ +/* border-radius: 8px;*/ +/* background: gray;*/ +/*}*/ + table { border-collapse: collapse; border: 2px solid #5b3300;