|
19 | 19 | def _insert_qnum(app, doctree, docname): |
20 | 20 | toc = app.env.toc_secnumbers.get(docname, {}) |
21 | 21 |
|
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): |
23 | 24 | # The Sphinx TOC structure is:: |
24 | 25 | # |
25 | 26 | # Dict[docname: str, |
26 | 27 | # Dict[ref: str, secnum_tuple: Tuple[int, ...] |
27 | 28 | # ] |
28 | 29 | # |
29 | 30 | # 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_ |
33 | 31 |
|
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() |
36 | 36 |
|
37 | 37 | current_question_number = 1 |
38 | 38 | for node in doctree.traverse(): |
39 | 39 | if isinstance(node, nodes.section): |
40 | 40 | # 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 |
44 | 46 | elif isinstance(node, RunestoneIdNode): |
45 | 47 | question_number_tuple = current_secnum_tuple + (current_question_number, ) |
46 | 48 | question_number_str = ".".join(map(str, question_number_tuple)) |
|
0 commit comments