From e79c20426ab6433f217625eabbdd7068f723c297 Mon Sep 17 00:00:00 2001 From: AntoineGautier Date: Fri, 13 Nov 2026 14:34:04 +0100 Subject: [PATCH] Fix sequence-doc annotations split by comment references Word's comment reference mark sits inside an annotation when a comment is anchored on part of it, which split the annotation and broke its toggle. Skip comment reference runs when reading annotations and close the final annotation after the last run. --- docs/sequence-doc.md | 2 + server/scripts/sequence-doc/src/mogrifier.py | 32 +++- .../sequence-doc/tests/test_comments.py | 140 ++++++++++++++++++ 3 files changed, 168 insertions(+), 6 deletions(-) create mode 100644 server/scripts/sequence-doc/tests/test_comments.py diff --git a/docs/sequence-doc.md b/docs/sequence-doc.md index 7f872792..20c94fdd 100644 --- a/docs/sequence-doc.md +++ b/docs/sequence-doc.md @@ -24,6 +24,8 @@ The annotation is enclosed in square brackets `[]` The annotation text and ONLY the annotation text (which includes the brackets) has a style applied: `Toggle`. This style is used as an identifier by the sequence document generation script to find annotations. +An annotation is a continuous stretch of text with the `Toggle` style within one paragraph: text with another style ends it. A Word comment anchored on an annotation, e.g. on a short ID, does not split it. A section removed by an annotation is removed with the comments anchored in it. + ### Example Annotations: + `[YES CO2]` – Keep this section if a configuration has a CO2 sensor. Otherwise remove. diff --git a/server/scripts/sequence-doc/src/mogrifier.py b/server/scripts/sequence-doc/src/mogrifier.py index 77b575e4..9f0c86a4 100644 --- a/server/scripts/sequence-doc/src/mogrifier.py +++ b/server/scripts/sequence-doc/src/mogrifier.py @@ -17,6 +17,9 @@ BOOKMARK_TAGS = ['{http://schemas.openxmlformats.org/wordprocessingml/2006/main}bookmarkEnd', "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}bookmarkStart"] SECTION_TAG = ["{http://schemas.openxmlformats.org/wordprocessingml/2006/main}sectPr"] TABLE_TAG = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tbl' +W_NS = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main' +RUN_PROPERTIES_TAG = f'{{{W_NS}}}rPr' +COMMENT_REFERENCE_TAG = f'{{{W_NS}}}commentReference' ANNOTATION_STYLE = 'Toggle' UNITS_SHORT_NAME = 'UNITS' @@ -161,26 +164,40 @@ def get_column(table: Table, table_cell): if cell.text == table_cell.text: return column +def is_comment_reference(run): + ''' True if the run only holds the reference mark of a comment, which is + not text of the document (docs/sequence-doc.md, "Annotations") + ''' + content = [child for child in run.element if child.tag != RUN_PROPERTIES_TAG] + return len(content) == 1 and content[0].tag == COMMENT_REFERENCE_TAG + def create_control_structures(doc): ''' Sets up the control structure for Sections and Tables ''' control_structures = [] run_op_lookup = {} + def close(annotation): + ''' Adds an annotation once its last run is read ''' + if annotation: + annotation['text'] = annotation['text'].strip() + if annotation['text']: + control_structures.append(annotation) + current = None ## Sections for para in doc.paragraphs: for run in para.runs: + if is_comment_reference(run): + continue run_style_name = get_run_style_name(run) if ( (current and run_style_name != ANNOTATION_STYLE) or (current and para is not current['paragraph']) ): - current['text'] = current['text'].strip() - if current['text']: - control_structures.append(current) + close(current) current = None if not current and run_style_name == ANNOTATION_STYLE: @@ -195,6 +212,8 @@ def create_control_structures(doc): run_op_lookup[run.element] = current current['runs'].append(run) current['text'] += run.text + close(current) + current = None ## Tables for table in doc.tables: @@ -202,15 +221,15 @@ def create_control_structures(doc): for cell in row.cells: for para in cell.paragraphs: for run in para.runs: + if is_comment_reference(run): + continue run_style_name = get_run_style_name(run) if ( (current and run_style_name != ANNOTATION_STYLE) or (current and para is not current['paragraph']) ): - current['text'] = current['text'].strip() - if current['text']: - control_structures.append(current) + close(current) current = None if not current and run_style_name == ANNOTATION_STYLE: @@ -229,6 +248,7 @@ def create_control_structures(doc): run.op = current current['runs'].append(run) current['text'] += run.text + close(current) for op in control_structures: tokens = utils.remove_empty_strings(re.split(r'\W+', op['text'])) diff --git a/server/scripts/sequence-doc/tests/test_comments.py b/server/scripts/sequence-doc/tests/test_comments.py new file mode 100644 index 00000000..7d41a750 --- /dev/null +++ b/server/scripts/sequence-doc/tests/test_comments.py @@ -0,0 +1,140 @@ +''' +Comments anchored on annotations (docs/sequence-doc.md, "Annotations") + +Word places the reference mark of a comment right after the end of its range. +A comment anchored on part of an annotation puts that mark inside the +annotation. The mark is not text of the document, so it must not split the +annotation. An annotation is read up to its last run, even if no run +follows it. +''' +import pytest +from docx import Document +from docx.enum.style import WD_STYLE_TYPE +import mogrifier +import toggles + +NAME_MAP = { + 'A': 'P.A-c.p', + 'B': 'P.Types.B', + 'UNITS': 'P.AllSystems-sysUni', + 'SI': 'P.Units.SI', + 'IP': 'P.Units.IP', +} +SI_PROJECT = {'P.AllSystems-sysUni': 'P.Units.SI'} +IP_PROJECT = {'P.AllSystems-sysUni': 'P.Units.IP'} + + +def new_document(): + doc = Document() + doc.styles.add_style(mogrifier.ANNOTATION_STYLE, WD_STYLE_TYPE.CHARACTER) + return doc + + +def new_paragraph(doc, location): + ''' Empty paragraph in the body or in a table cell ''' + if location == 'body': + return doc.add_paragraph() + return doc.add_table(rows=1, cols=1).cell(0, 0).paragraphs[0] + + +def annotate(paragraph, parts, commented, style=mogrifier.ANNOTATION_STYLE): + ''' Adds the parts of an annotation as runs of the given style, with a + comment on the parts of index `commented`, and text after it + ''' + runs = [paragraph.add_run(part, style) for part in parts] + paragraph.part.document.add_comment( + [runs[i] for i in commented], text='Review', author='Reviewer') + paragraph.add_run(' Text') + return runs + + +def annotation_texts(doc): + control_structures, _ = mogrifier.create_control_structures(doc) + return [op['text'] for op in control_structures] + + +LOCATIONS = ['body', 'table'] + + +@pytest.mark.parametrize('location', LOCATIONS) +def test_comment_reference_does_not_split_an_annotation(location): + doc = new_document() + paragraph = new_paragraph(doc, location) + annotate(paragraph, ['[EQUALS A ', 'B', ']'], commented=[1]) + assert any(mogrifier.is_comment_reference(run) for run in paragraph.runs) + assert annotation_texts(doc) == ['[EQUALS A B]'] + + +@pytest.mark.parametrize('location', LOCATIONS) +@pytest.mark.parametrize('commented', [[0], [1], [2], [0, 1, 2]]) +def test_annotation_is_read_wherever_the_comment_is(location, commented): + doc = new_document() + annotate(new_paragraph(doc, location), ['[YES ', 'A', ']'], commented) + assert annotation_texts(doc) == ['[YES A]'] + + +@pytest.mark.parametrize('location', LOCATIONS) +def test_another_style_still_ends_an_annotation(location): + doc = new_document() + paragraph = new_paragraph(doc, location) + paragraph.add_run('[EQUALS A ', mogrifier.ANNOTATION_STYLE) + paragraph.add_run('B', mogrifier.ANNOTATION_STYLE) + paragraph.add_run(' ') + paragraph.add_run(']', mogrifier.ANNOTATION_STYLE) + paragraph.add_run(' Text') + texts = annotation_texts(doc) + assert texts == ['[EQUALS A B', ']'] + with pytest.raises(toggles.ToggleError, match='unbalanced brackets'): + toggles.parse(texts[0], NAME_MAP) + + +@pytest.mark.parametrize('location', LOCATIONS) +@pytest.mark.parametrize('project, text', [(SI_PROJECT, 'Setpoint 12 Pa Text'), (IP_PROJECT, 'Setpoint 0.05 in. Text')]) +def test_units_with_a_comment_reference_is_replaced(location, project, text): + doc = new_document() + paragraph = new_paragraph(doc, location) + paragraph.add_run('Setpoint ') + annotate(paragraph, ['[UNITS [12 Pa] ', '[0.05 in.]]'], commented=[0]) + mogrifier.mogrify_doc(doc, NAME_MAP, project, [project]) + assert paragraph.text == text + + +def comment_markers(doc): + ''' Comment IDs of each kind of marker of the body ''' + body = doc.element.body + return { + kind: sorted(element.get(f'{{{mogrifier.W_NS}}}id') for element in body.iter(f'{{{mogrifier.W_NS}}}{kind}')) + for kind in ('commentRangeStart', 'commentRangeEnd', 'commentReference') + } + + +@pytest.mark.parametrize('location', LOCATIONS) +def test_removed_sections_leave_complete_comments(location): + ''' A removed section takes the markers of its comments with it, and the + comments of a kept section keep all of their markers + ''' + doc = new_document() + annotate(new_paragraph(doc, location), ['[NO ', 'A', ']'], commented=[1]) + removed = doc.add_paragraph(style='Heading 1') + annotate(removed, ['[YES ', 'A', ']'], commented=[1]) + doc.add_paragraph('Removed') + kept_id = str(next(iter(doc.comments)).comment_id) + mogrifier.mogrify_doc(doc, NAME_MAP, {}, [{'P.A-c.p': False}]) + assert 'Removed' not in [paragraph.text for paragraph in doc.paragraphs] + markers = comment_markers(doc) + assert markers == {kind: [kept_id] for kind in markers} + + +@pytest.mark.parametrize('location', LOCATIONS) +def test_annotation_in_the_last_run_is_read(location): + ''' No run follows the annotation that ends the body or the last table ''' + doc = new_document() + new_paragraph(doc, location).add_run('[YES A]', mogrifier.ANNOTATION_STYLE) + assert annotation_texts(doc) == ['[YES A]'] + + +def test_annotation_that_ends_the_body_is_not_extended_by_a_table(): + doc = new_document() + doc.add_paragraph().add_run('[YES A]', mogrifier.ANNOTATION_STYLE) + new_paragraph(doc, 'table').add_run('[NO A]', mogrifier.ANNOTATION_STYLE) + assert annotation_texts(doc) == ['[YES A]', '[NO A]']