Skip to content

Commit 8781d36

Browse files
committed
Group nodes generated by :kbd: role and add class to separators (#13876)
1 parent eaebbec commit 8781d36

File tree

4 files changed

+89
-30
lines changed

4 files changed

+89
-30
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ Features added
6666
Patch by Adam Turner.
6767
* #13805: LaTeX: add support for ``fontawesome7`` package.
6868
Patch by Jean-François B.
69+
* #13876: Group nodes generated by :kbd: role and add class to separators
70+
Patch by Brecht Machiels.
6971

7072
Bugs fixed
7173
----------

sphinx/roles.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -491,27 +491,24 @@ def run(self) -> tuple[list[Node], list[system_message]]:
491491
if 'classes' in self.options:
492492
classes.extend(self.options['classes'])
493493

494+
compound = nodes.inline(self.rawtext, classes=['kbdcompound'])
494495
parts = self._pattern.split(self.text)
495-
if len(parts) == 1 or self._is_multi_word_key(parts):
496-
return [nodes.literal(self.rawtext, self.text, classes=classes)], []
497-
498-
compound: list[Node] = []
499496
while parts:
500497
if self._is_multi_word_key(parts):
501498
key = ''.join(parts[:3])
502499
parts[:3] = []
503500
else:
504501
key = parts.pop(0)
505-
compound.append(nodes.literal(key, key, classes=classes))
502+
compound += nodes.literal(key, key, classes=['kbd'])
506503

507504
try:
508505
sep = parts.pop(0) # key separator ('-', '+', '^', etc)
509506
except IndexError:
510507
break
511508
else:
512-
compound.append(nodes.Text(sep))
509+
compound += nodes.inline(sep, sep, classes=['kbdsep'])
513510

514-
return compound, []
511+
return [compound], []
515512

516513
@staticmethod
517514
def _is_multi_word_key(parts: list[str]) -> bool:

tests/test_builders/test_build_html_5_output.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def checker(nodes: Iterable[Element]) -> Literal[True]:
127127
('markup.html', './/li/p/strong', r'^command\\n$'),
128128
('markup.html', './/li/p/strong', r'^program\\n$'),
129129
('markup.html', './/li/p/em', r'^dfn\\n$'),
130-
('markup.html', './/li/p/kbd', r'^kbd\\n$'),
130+
('markup.html', './/li/p/span/kbd', r'^kbd\\n$'),
131131
('markup.html', './/li/p/span', 'File \N{TRIANGULAR BULLET} Close'),
132132
('markup.html', ".//li/p/code/span[@class='pre']", '^a/$'),
133133
('markup.html', ".//li/p/code/em/span[@class='pre']", '^varpart$'),

tests/test_markup/test_markup.py

Lines changed: 82 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -332,25 +332,40 @@ def get_verifier(verify, verify_re):
332332
# kbd role
333333
'verify',
334334
':kbd:`space`',
335-
'<p><kbd class="kbd docutils literal notranslate">space</kbd></p>',
336-
'\\sphinxAtStartPar\n\\sphinxkeyboard{\\sphinxupquote{space}}',
335+
(
336+
'<p>'
337+
'<span class="kbdcompound">'
338+
'<kbd class="kbd docutils literal notranslate">space</kbd>'
339+
'</span>'
340+
'</p>'
341+
),
342+
(
343+
'\\sphinxAtStartPar\n'
344+
'\\DUrole{kbdcompound}{'
345+
'\\sphinxkeyboard{\\sphinxupquote{space}}'
346+
'}'
347+
),
337348
),
338349
(
339350
# kbd role
340351
'verify',
341352
':kbd:`Control+X`',
342353
(
343354
'<p>'
355+
'<span class="kbdcompound">'
344356
'<kbd class="kbd docutils literal notranslate">Control</kbd>'
345-
'+'
357+
'<span class="kbdsep">+</span>'
346358
'<kbd class="kbd docutils literal notranslate">X</kbd>'
359+
'</span>'
347360
'</p>'
348361
),
349362
(
350363
'\\sphinxAtStartPar\n'
364+
'\\DUrole{kbdcompound}{'
351365
'\\sphinxkeyboard{\\sphinxupquote{Control}}'
352-
'+'
366+
'\\DUrole{kbdsep}{+}'
353367
'\\sphinxkeyboard{\\sphinxupquote{X}}'
368+
'}'
354369
),
355370
),
356371
(
@@ -359,16 +374,20 @@ def get_verifier(verify, verify_re):
359374
':kbd:`Alt+^`',
360375
(
361376
'<p>'
377+
'<span class="kbdcompound">'
362378
'<kbd class="kbd docutils literal notranslate">Alt</kbd>'
363-
'+'
379+
'<span class="kbdsep">+</span>'
364380
'<kbd class="kbd docutils literal notranslate">^</kbd>'
381+
'</span>'
365382
'</p>'
366383
),
367384
(
368385
'\\sphinxAtStartPar\n'
386+
'\\DUrole{kbdcompound}{'
369387
'\\sphinxkeyboard{\\sphinxupquote{Alt}}'
370-
'+'
388+
'\\DUrole{kbdsep}{+}'
371389
'\\sphinxkeyboard{\\sphinxupquote{\\textasciicircum{}}}'
390+
'}'
372391
),
373392
),
374393
(
@@ -377,67 +396,108 @@ def get_verifier(verify, verify_re):
377396
':kbd:`M-x M-s`',
378397
(
379398
'<p>'
399+
'<span class="kbdcompound">'
380400
'<kbd class="kbd docutils literal notranslate">M</kbd>'
381-
'-'
401+
'<span class="kbdsep">-</span>'
382402
'<kbd class="kbd docutils literal notranslate">x</kbd>'
383-
' '
403+
'<span class="kbdsep"> </span>'
384404
'<kbd class="kbd docutils literal notranslate">M</kbd>'
385-
'-'
405+
'<span class="kbdsep">-</span>'
386406
'<kbd class="kbd docutils literal notranslate">s</kbd>'
407+
'</span>'
387408
'</p>'
388409
),
389410
(
390411
'\\sphinxAtStartPar\n'
412+
'\\DUrole{kbdcompound}{'
391413
'\\sphinxkeyboard{\\sphinxupquote{M}}'
392-
'\\sphinxhyphen{}'
414+
'\\DUrole{kbdsep}{\\sphinxhyphen{}}'
393415
'\\sphinxkeyboard{\\sphinxupquote{x}}'
394-
' '
416+
'\\DUrole{kbdsep}{ }'
395417
'\\sphinxkeyboard{\\sphinxupquote{M}}'
396-
'\\sphinxhyphen{}'
418+
'\\DUrole{kbdsep}{\\sphinxhyphen{}}'
397419
'\\sphinxkeyboard{\\sphinxupquote{s}}'
420+
'}'
398421
),
399422
),
400423
(
401424
# kbd role
402425
'verify',
403426
':kbd:`-`',
404-
'<p><kbd class="kbd docutils literal notranslate">-</kbd></p>',
405-
'\\sphinxAtStartPar\n\\sphinxkeyboard{\\sphinxupquote{\\sphinxhyphen{}}}',
427+
(
428+
'<p>'
429+
'<span class="kbdcompound">'
430+
'<kbd class="kbd docutils literal notranslate">-</kbd>'
431+
'</span>'
432+
'</p>'
433+
),
434+
(
435+
'\\sphinxAtStartPar\n'
436+
'\\DUrole{kbdcompound}{'
437+
'\\sphinxkeyboard{\\sphinxupquote{\\sphinxhyphen{}}}'
438+
'}'
439+
),
406440
),
407441
(
408442
# kbd role
409443
'verify',
410444
':kbd:`Caps Lock`',
411-
'<p><kbd class="kbd docutils literal notranslate">Caps Lock</kbd></p>',
412-
'\\sphinxAtStartPar\n\\sphinxkeyboard{\\sphinxupquote{Caps Lock}}',
445+
(
446+
'<p>'
447+
'<span class="kbdcompound">'
448+
'<kbd class="kbd docutils literal notranslate">Caps Lock</kbd>'
449+
'</span>'
450+
'</p>'
451+
),
452+
(
453+
'\\sphinxAtStartPar\n'
454+
'\\DUrole{kbdcompound}{'
455+
'\\sphinxkeyboard{\\sphinxupquote{Caps Lock}}'
456+
'}'
457+
),
413458
),
414459
(
415460
# kbd role
416461
'verify',
417462
':kbd:`sys rq`',
418-
'<p><kbd class="kbd docutils literal notranslate">sys rq</kbd></p>',
419-
'\\sphinxAtStartPar\n\\sphinxkeyboard{\\sphinxupquote{sys rq}}',
463+
(
464+
'<p>'
465+
'<span class="kbdcompound">'
466+
'<kbd class="kbd docutils literal notranslate">sys rq</kbd>'
467+
'</span>'
468+
'</p>'
469+
),
470+
(
471+
'\\sphinxAtStartPar\n'
472+
'\\DUrole{kbdcompound}{'
473+
'\\sphinxkeyboard{\\sphinxupquote{sys rq}}'
474+
'}'
475+
),
420476
),
421477
(
422478
# kbd role
423479
'verify',
424480
':kbd:`⌘+⇧+M`',
425481
(
426482
'<p>'
483+
'<span class="kbdcompound">'
427484
'<kbd class="kbd docutils literal notranslate">⌘</kbd>'
428-
'+'
485+
'<span class="kbdsep">+</span>'
429486
'<kbd class="kbd docutils literal notranslate">⇧</kbd>'
430-
'+'
487+
'<span class="kbdsep">+</span>'
431488
'<kbd class="kbd docutils literal notranslate">M</kbd>'
489+
'</span>'
432490
'</p>'
433491
),
434492
(
435493
'\\sphinxAtStartPar\n'
494+
'\\DUrole{kbdcompound}{'
436495
'\\sphinxkeyboard{\\sphinxupquote{⌘}}'
437-
'+'
496+
'\\DUrole{kbdsep}{+}'
438497
'\\sphinxkeyboard{\\sphinxupquote{⇧}}'
439-
'+'
498+
'\\DUrole{kbdsep}{+}'
440499
'\\sphinxkeyboard{\\sphinxupquote{M}}'
500+
'}'
441501
),
442502
),
443503
(

0 commit comments

Comments
 (0)