Skip to content

Commit 5591a2c

Browse files
committed
Prepend prefix to all ids to guarantee that they are unique
1 parent b3b6497 commit 5591a2c

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

sphinxarg/ext.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def print_subcommands(data, nested_content, markDownHelp=False, settings=None):
221221
return items
222222

223223

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

418419
def run(self):
420+
prefix = ''
419421
if 'module' in self.options and 'func' in self.options:
420422
module_name = self.options['module']
421423
attr_name = self.options['func']
424+
prefix = 'module-%s.%s' % (module_name, attr_name)
422425
elif 'ref' in self.options:
423426
_parts = self.options['ref'].split('.')
424427
module_name = '.'.join(_parts[0:-1])
425428
attr_name = _parts[-1]
429+
prefix = 'module-%s.%s' % (module_name, attr_name)
426430
elif 'filename' in self.options and 'func' in self.options:
431+
filename = self.options['filename']
427432
mod = {}
428433
try:
429-
f = open(self.options['filename'])
434+
f = open(filename)
430435
except IOError:
431436
# try open with abspath
432-
f = open(os.path.abspath(self.options['filename']))
433-
code = compile(f.read(), self.options['filename'], 'exec')
437+
f = open(os.path.abspath(filename))
438+
code = compile(f.read(), filename, 'exec')
434439
exec(code, mod)
435440
attr_name = self.options['func']
436441
func = mod[attr_name]
442+
prefix = '%s.%s' % (filename, attr_name)
437443
else:
438444
raise self.error(
439445
':module: and :func: should be specified, or :ref:, or :filename: and :func:')
@@ -503,7 +509,7 @@ def run(self):
503509
items.append(self._nested_parse_paragraph(result['epilog']))
504510

505511
# Traverse the returned nodes, modifying the title IDs as necessary to avoid repeats
506-
ensureUniqueIDs(items)
512+
ensureUniqueIDs(items, prefix)
507513

508514
return items
509515

0 commit comments

Comments
 (0)