Skip to content

Commit 709988f

Browse files
committed
[#1491] Generate id from :name: option in index
1 parent 7b45a93 commit 709988f

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

sphinxcontrib/qthelp/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
_idpattern = re.compile(
4040
r'(?P<title>.+) (\((class in )?(?P<id>[\w\.]+)( (?P<descr>\w+))?\))$')
4141

42+
_refidpattern = re.compile(
43+
r'.*#(?P<id>.*)$')
4244

4345
section_template = '<section title="%(title)s" ref="%(ref)s"/>'
4446

@@ -213,6 +215,13 @@ def keyword_item(self, name: str, ref: Any) -> str:
213215

214216
nameattr = html.escape(name, quote=True)
215217
refattr = html.escape(ref[1], quote=True)
218+
if not id:
219+
matchobj = _refidpattern.match(refattr)
220+
if matchobj:
221+
groupdict = matchobj.groupdict()
222+
id = groupdict.get('id')
223+
if id.startswith("index-"):
224+
id = None
216225
if id:
217226
item = ' ' * 12 + '<keyword name="%s" id="%s" ref="%s"/>' % (nameattr, id, refattr)
218227
else:

tests/roots/test-id/conf.py

Whitespace-only changes.

tests/roots/test-id/index.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. index:: test-id
2+
:name: MY_TESTID
3+
4+
test-id
5+
=======
6+
7+
This is some text
8+
9+
10+
.. index:: test-id-next
11+
12+
test-id2
13+
========
14+
15+
16+
17+

tests/test_qthelp.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,18 @@ def test_qthelp_title(app, status, warning):
132132

133133
qhcp = (app.outdir / 'Python.qhcp').text()
134134
assert '<title>Sphinx &lt;b&gt;&#34;short&#34;&lt;/b&gt; title</title>' in qhcp
135+
136+
137+
@pytest.mark.sphinx('qthelp', testroot='id')
138+
def test_qthelp_id(app, status, warning):
139+
app.builder.build_all()
140+
141+
et = etree_parse(app.outdir / 'Python.qhp')
142+
keywords = et.find('.//keywords')
143+
assert len(keywords) == 2
144+
assert keywords[0].attrib == {'name': 'test-id',
145+
'ref': 'index.html#my-testid',
146+
'id': 'my-testid'}
147+
148+
assert keywords[1].attrib == {'name': 'test-id-next',
149+
'ref': 'index.html#index-0'}

0 commit comments

Comments
 (0)