From 0331311dd08353bd252db0a968ebf54875d2aeb5 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 28 Jun 2022 10:03:34 +0100 Subject: [PATCH] Ensure that specification example numbers are sequential --- markdown.py | 24 ++++++++++++++++++++++++ webapp_specification.py | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/markdown.py b/markdown.py index 192a9e7a7..90eb71ebd 100644 --- a/markdown.py +++ b/markdown.py @@ -312,6 +312,30 @@ def _markdown_replace_code(markdown: str) -> str: return markdown +def markdown_example_numbers(markdown: str) -> str: + """Ensures that example numbers in the ActivityPub specification + document are sequential + """ + lines = markdown.split('\n') + example_number = 1 + line_ctr = 0 + for line in lines: + if not line.strip(): + # skip blank lines + line_ctr += 1 + continue + if line.startswith('##') and '## Example ' in line: + header_str = line.split(' Example ')[0] + lines[line_ctr] = header_str + ' Example ' + str(example_number) + example_number += 1 + line_ctr += 1 + + markdown = '' + for line in lines: + markdown += line + '\n' + return markdown + + def markdown_to_html(markdown: str) -> str: """Converts markdown formatted text to html """ diff --git a/webapp_specification.py b/webapp_specification.py index 0dd73da47..aa1a0baee 100644 --- a/webapp_specification.py +++ b/webapp_specification.py @@ -12,6 +12,7 @@ from shutil import copyfile from utils import get_config_param from webapp_utils import html_header_with_website_markup from webapp_utils import html_footer +from markdown import markdown_example_numbers from markdown import markdown_to_html @@ -34,7 +35,8 @@ def html_specification(css_cache: {}, base_dir: str, http_prefix: str, if os.path.isfile(base_dir + '/accounts/activitypub.md'): with open(base_dir + '/accounts/activitypub.md', 'r', encoding='utf-8') as fp_specification: - specification_text = markdown_to_html(fp_specification.read()) + md_text = markdown_example_numbers(fp_specification.read()) + specification_text = markdown_to_html(md_text) specification_form = '' css_filename = base_dir + '/epicyon-profile.css'