Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "rda_python_miscs"
version = "2.0.4"
version = "2.0.5"
authors = [
{ name="Zaihua Ji", email="zji@ucar.edu" },
]
Expand Down
47 changes: 19 additions & 28 deletions src/rda_python_miscs/pg_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def process_docs(self, docname, opts, alias):

This is the main entry point. It populates ``self.sections``,
``self.options``, and ``self.examples`` by calling ``parse_docs``, then
writes ``index.rst``, ``toc.rst``, and one ``section<id>.rst`` per
writes ``index.rst`` and one ``section<id>.rst`` per
section into ``DOCDIR``.

Args:
Expand All @@ -155,8 +155,6 @@ def process_docs(self, docname, opts, alias):
else:
self.write_index(self.sections[0])

self.write_toc()

for section in self.sections:
self.write_section(section)

Expand Down Expand Up @@ -404,29 +402,17 @@ def init_example(self, opt, desc):
def write_index(self, section):
"""Write ``index.rst`` from the ``index.rst.temp`` template.

Passes ``TITLE`` (document title) and ``SECID`` (first section id)
as substitution variables.
Passes ``TITLE`` (document title), ``SECID`` (first section id),
and the generated ``TOC`` RST content as substitution variables.

Args:
section (dict): The first section dict, used to supply ``SECID``.
"""
hash = {'TITLE' : self.DOCS['DOCTIT'], 'SECID' : section['secid']}
hash = {'TITLE' : self.DOCS['DOCTIT'], 'SECID' : section['secid'],
'TOC' : self.create_toc()}
Comment on lines +411 to +412

Copilot AI Mar 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write_index() now injects self.create_toc() into the index template. create_toc() currently generates links like `... <sectionX.rst>`_ and <sectionX.rst#eN> (file/fragment URIs), which won't match the new label-based linking approach and typically won't resolve correctly in Sphinx-built HTML (it will literally point at .rst sources). Update create_toc() (or the index template expectations) to emit Sphinx-friendly cross-refs (e.g. :ref: / :doc:) targeting the existing labels (like section{secid} and e{N}) so the TOC links work after this change.

Copilot uses AI. Check for mistakes.

self.template_to_rst("index", hash)

#
# write the table of contents: toc.rst
#
def write_toc(self):
"""Write ``toc.rst`` from the ``toc.rst.temp`` template.

Passes ``TITLE`` and the generated ``TOC`` RST content as substitution
variables.
"""
hash = {'TITLE' : self.DOCS['DOCTIT'], 'TOC' : self.create_toc()}

self.template_to_rst("toc", hash)

#
# write a section rst file
#
Expand Down Expand Up @@ -651,8 +637,9 @@ def replace_option_link(self, line, csecid, ptype=None, dtype=None):
"""Scan *line* for option references, section-category keywords, URLs, and
quoted program names, and replace each with an RST hyperlink.

Link targets are formatted as RST anonymous hyperlinks:
`` `text <url>`_ ``.
All links use RST named anchor references (`` `name`_ `` or
`` `text <name_>`_ ``) targeting anchors of the form ``.. _name:``.
Cross-file option links target ``.. _section{secid}:`` anchors.

Args:
line (str): Source text line to process.
Expand Down Expand Up @@ -687,14 +674,15 @@ def replace_option_link(self, line, csecid, ptype=None, dtype=None):
pre = optary[0]
after = optary[2]
secid = self.options[opt]['secid']
anchor = None # RST anchor name for same-file or cross-file links (.. _NAME:)
if secid == csecid:
link = "#{}".format(opt)
anchor = opt
elif self.options[opt]['type'] == "Action":
link = "section{}.rst".format(secid)
anchor = "section{}".format(secid)
elif ptype == 2 and opt == "FN":
link = "#field"
anchor = "field"
else:
link = "section{}.rst#{}".format(secid, opt)
anchor = "section{}".format(secid)

ms = re.search(r'-\(({}\|\w+)\)'.format(opt), line)
if ms:
Expand All @@ -703,7 +691,10 @@ def replace_option_link(self, line, csecid, ptype=None, dtype=None):
after = ')'

replace = pre + opt + after
link = "{}`{} <{}>`_{}".format(pre, opt, link, after)
if opt == anchor:
link = "{}`{}`_{}".format(pre, opt, after)
else:
link = "{}`{} <{}_>`_{}".format(pre, opt, anchor, after)
line = line.replace(replace, link)
Comment on lines 693 to 698

Copilot AI Mar 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace_option_link() now emits plain reST named references (e.g. `text <section1_>`_). In Sphinx, these generally only resolve within the same document; cross-file label references should use the :ref: role (e.g. :ref:text ``) to reliably target labels defined in other .rst files. Update the link formatting here to use `:ref:` (and reference the actual target label without the trailing underscore) so cross-section option/category links resolve correctly during builds.

Copilot uses AI. Check for mistakes.

opts = re.findall(r'(^|\W){}( Options*\W|\W|$)'.format(self.SEARCH), line)
Expand All @@ -720,9 +711,9 @@ def replace_option_link(self, line, csecid, ptype=None, dtype=None):
opt += ms.group(1)
after = after.replace(ms.group(1), '')
if ptype == 2 and re.search(r'Mode Options*', opt) and dtype == 3:
link = "{}`{} <#mode>`_{}".format(pre, opt, after)
link = "{}`{} <mode_>`_{}".format(pre, opt, after)
else:
link = "{}`{} <section{}.rst>`_{}".format(pre, opt, secid, after)
link = "{}`{} <section{}_>`_{}".format(pre, opt, secid, after)
line = line.replace(replace, link)
Comment on lines 713 to 717

Copilot AI Mar 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The category keyword links are being rewritten to targets like mode_ and section{secid}_ (plain named references). If these targets are defined in other section files, Sphinx won't reliably resolve them without :ref: cross-reference roles. Consider generating :ref: links here as well (e.g. :ref:Mode Options / `:ref:`Info Options <section2>) so the references resolve across documents.

Copilot uses AI. Check for mistakes.

ms = re.search(r'(https*://\S+)(\.|\,)', line)
Expand Down
11 changes: 3 additions & 8 deletions src/rda_python_miscs/rst_templates/index.rst.temp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@
A GUIDE TO __TITLE__
============================

.. contents:: Table of Contents
:depth: 2
:local:

See the :doc:`toc` for the full table of contents.

.. toctree::
:maxdepth: 2
:caption: Contents

toc
section__SECID__
section__SECID__

__TOC__
3 changes: 0 additions & 3 deletions src/rda_python_miscs/rst_templates/section.rst.temp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ __SECID__ - __TITLE__

__SECTION__

.. raw:: html

<br>

:ref:`Back to Top <index>`
Loading