Skip to content

Commit

Permalink
Prepend prefix to all ids to guarantee that they are unique
Browse files Browse the repository at this point in the history
  • Loading branch information
wlupton committed Feb 16, 2022
1 parent 00805f2 commit 2c554c4
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions sphinxarg/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def print_subcommands(data, nested_content, markDownHelp=False, settings=None):
return items


def ensureUniqueIDs(items):
def ensureUniqueIDs(items, prefix):
"""
If action groups are repeated, then links in the table of contents will
just go to the first of the repeats. This may not be desirable, particularly
Expand All @@ -234,6 +234,7 @@ def ensureUniqueIDs(items):
for n in item.traverse(descend=True, siblings=True, ascend=False):
if isinstance(n, nodes.section):
ids = n['ids']
ids = ['%s %s' % (prefix, i) for i in ids]
for idx, id in enumerate(ids):
if id not in s:
s.add(id)
Expand Down Expand Up @@ -416,24 +417,29 @@ def _nested_parse_paragraph(self, text):
return content

def run(self):
prefix = ''
if 'module' in self.options and 'func' in self.options:
module_name = self.options['module']
attr_name = self.options['func']
prefix = 'module-%s.%s' % (module_name, attr_name)
elif 'ref' in self.options:
_parts = self.options['ref'].split('.')
module_name = '.'.join(_parts[0:-1])
attr_name = _parts[-1]
prefix = 'module-%s.%s' % (module_name, attr_name)
elif 'filename' in self.options and 'func' in self.options:
filename = self.options['filename']
mod = {}
try:
f = open(self.options['filename'])
f = open(filename)
except IOError:
# try open with abspath
f = open(os.path.abspath(self.options['filename']))
code = compile(f.read(), self.options['filename'], 'exec')
f = open(os.path.abspath(filename))
code = compile(f.read(), filename, 'exec')
exec(code, mod)
attr_name = self.options['func']
func = mod[attr_name]
prefix = '%s.%s' % (filename, attr_name)
else:
raise self.error(
':module: and :func: should be specified, or :ref:, or :filename: and :func:')
Expand Down Expand Up @@ -503,7 +509,7 @@ def run(self):
items.append(self._nested_parse_paragraph(result['epilog']))

# Traverse the returned nodes, modifying the title IDs as necessary to avoid repeats
ensureUniqueIDs(items)
ensureUniqueIDs(items, prefix)

return items

Expand Down

0 comments on commit 2c554c4

Please sign in to comment.