|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 | # PYTHON_ARGCOMPLETE_OK
|
3 | 3 |
|
| 4 | +import itertools |
4 | 5 | import sys
|
5 | 6 | import os
|
6 | 7 | import re
|
@@ -422,23 +423,40 @@ def output(page: str, plain: bool = False) -> None:
|
422 | 423 | if plain:
|
423 | 424 | print(line)
|
424 | 425 | continue
|
| 426 | + |
425 | 427 | elif len(line) == 0:
|
426 | 428 | continue
|
| 429 | + |
| 430 | + # Handle the command name |
427 | 431 | elif line[0] == '#':
|
428 | 432 | line = ' ' * LEADING_SPACES_NUM + \
|
429 | 433 | colored(line.replace('# ', ''), *colors_of('name')) + '\n'
|
430 | 434 | sys.stdout.buffer.write(line.encode('utf-8'))
|
| 435 | + |
| 436 | + # Handle the command description |
431 | 437 | elif line[0] == '>':
|
432 | 438 | line = ' ' * (LEADING_SPACES_NUM - 1) + \
|
433 | 439 | colored(
|
434 | 440 | line.replace('>', '').replace('<', ''),
|
435 | 441 | *colors_of('description')
|
436 | 442 | )
|
437 | 443 | sys.stdout.buffer.write(line.encode('utf-8'))
|
| 444 | + |
| 445 | + # Handle an example description |
438 | 446 | elif line[0] == '-':
|
439 | 447 | line = '\n' + ' ' * LEADING_SPACES_NUM + \
|
440 | 448 | colored(line, *colors_of('example'))
|
| 449 | + # Stylize text within backticks using italics |
| 450 | + if '`' in line: |
| 451 | + # Backticks should occur in pairs, so str.split results in an odd number of elements |
| 452 | + *parts, last_part = line.split('`') |
| 453 | + # Setup an infinite cycle of italic and reset ANSI escape pairs |
| 454 | + italics_escapes = itertools.cycle(('\x1B[3m', '\x1B[0m')) |
| 455 | + # Rejoin the original string parts with the matching escape pairs |
| 456 | + line = "".join(itertools.chain.from_iterable(zip(parts, italics_escapes))) + last_part |
441 | 457 | sys.stdout.buffer.write(line.encode('utf-8'))
|
| 458 | + |
| 459 | + # Handle an example command |
442 | 460 | elif line[0] == '`':
|
443 | 461 | line = line[1:-1] # Remove backticks for parsing
|
444 | 462 |
|
|
0 commit comments