Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit b7a62b0

Browse files
authored
Merge pull request #1104 from bjones1/num_fix
Fix: Only restart question numbers on numbered sections.
2 parents 8bfdcd2 + a60a08e commit b7a62b0

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

runestone/common/question_number.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,30 @@
1919
def _insert_qnum(app, doctree, docname):
2020
toc = app.env.toc_secnumbers.get(docname, {})
2121

22-
def get_secnum_tuple(section_ref, current_secnum_tuple_):
22+
# Return the section number tuple for the given ``section_ref`` if it exists, or None if not.
23+
def get_secnum_tuple(section_ref):
2324
# The Sphinx TOC structure is::
2425
#
2526
# Dict[docname: str,
2627
# Dict[ref: str, secnum_tuple: Tuple[int, ...]
2728
# ]
2829
#
2930
# where a ``ref`` of ``''`` provides a tuple for the document as a whole.
30-
#
31-
# Not all section references will have an associated number. Return the current section if it was numbered and not None; otherwise, current the existing section number.
32-
return toc.get(section_ref, current_secnum_tuple_) or current_secnum_tuple_
3331

34-
# Set the current section number to the section for the overall document, if it's available.
35-
current_secnum_tuple = get_secnum_tuple("", tuple())
32+
return toc.get(section_ref)
33+
34+
# Set the current section number to the section for the overall document, if it's available; otherwise, use no number (an empty tuple).
35+
current_secnum_tuple = get_secnum_tuple("") or tuple()
3636

3737
current_question_number = 1
3838
for node in doctree.traverse():
3939
if isinstance(node, nodes.section):
4040
# To create a reference for a section node, I used this string, which was taken from ``sphinx.environment.collectors.toctree.TocTreeCollector.process_doc.build_toc``.
41-
current_secnum_tuple = get_secnum_tuple('#' + node['ids'][0], current_secnum_tuple)
42-
# Question numbering restarts at the beginning of each section.
43-
current_question_number = 1
41+
new_secnum_tuple = get_secnum_tuple('#' + node['ids'][0])
42+
# Question numbering restarts at the beginning of each numbered section; otherwise, continue to existing numbering through unnumbered sections.
43+
if new_secnum_tuple:
44+
current_secnum_tuple = new_secnum_tuple
45+
current_question_number = 1
4446
elif isinstance(node, RunestoneIdNode):
4547
question_number_tuple = current_secnum_tuple + (current_question_number, )
4648
question_number_str = ".".join(map(str, question_number_tuple))

0 commit comments

Comments
 (0)