Skip to content

Commit bf270c7

Browse files
author
Matthias Koeppe
committed
src/sage/misc/sagedoc.py (process_optional_doctest_tags): Rename from process_optional_annotations; remove empty block-level tags
1 parent 26d6766 commit bf270c7

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/sage/misc/sagedoc.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -590,21 +590,33 @@ def process_mathtt(s):
590590
return s
591591

592592

593-
def process_optional_annotations(s):
593+
def process_optional_doctest_tags(s):
594594
r"""
595-
Remove ``# optional`` annotations for present features from docstring ``s``.
596-
"""
597-
lines = s.split('\n')
595+
Remove ``# optional/needs`` doctest tags for present features from docstring ``s``.
596+
597+
EXAMPLES:
598598
599+
sage: from sage.misc.sagedoc import process_optional_doctest_tags
600+
sage: process_optional_doctest_tags("sage: # needs sage.rings.finite_rings\nsage: K.<x> = FunctionField(GF(5^2,'a')); K\nRational function field in x over Finite Field in a of size 5^2") # needs sage.rings.finite_rings
601+
"sage: K.<x> = FunctionField(GF(5^2,'a')); K\nRational function field in x over Finite Field in a of size 5^2"
602+
"""
603+
import io
599604
from sage.doctest.external import available_software
600605
from sage.doctest.parsing import parse_optional_tags, update_optional_tags
601606

602-
for i, line in enumerate(lines):
603-
if re.match(' *sage: .*#', line):
604-
tags = parse_optional_tags(line)
605-
lines[i] = update_optional_tags(line, remove_tags=[tag for tag in tags
606-
if tag in available_software])
607-
return '\n'.join(lines)
607+
start = 0
608+
with io.StringIO() as output:
609+
for m in re.finditer('( *sage: *.*#.*)\n', s):
610+
output.write(s[start:m.start(0)])
611+
line = m.group(1)
612+
tags = [tag for tag in parse_optional_tags(line)
613+
if tag not in available_software]
614+
line = update_optional_tags(line, tags=tags)
615+
if not re.fullmatch(' *sage: *', line):
616+
print(line, file=output)
617+
start = m.end(0)
618+
output.write(s[start:])
619+
return output.getvalue()
608620

609621

610622
def format(s, embedded=False):
@@ -788,7 +800,7 @@ def format(s, embedded=False):
788800
s = detex(s, embedded=embedded)
789801

790802
if not embedded:
791-
s = process_optional_annotations(s)
803+
s = process_optional_doctest_tags(s)
792804

793805
return s
794806

0 commit comments

Comments
 (0)