1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
# ==================================================================================================================== #
# _____ _ _ ____ _ _ #
# _ __ _ |_ _|__ ___ | (_)_ __ __ _ / ___| _ __ | |__ (_)_ __ __ __ #
# | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` | \___ \| '_ \| '_ \| | '_ \\ \/ / #
# | |_) | |_| || | (_) | (_) | | | | | | (_| |_ ___) | |_) | | | | | | | |> < #
# | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)____/| .__/|_| |_|_|_| |_/_/\_\ #
# |_| |___/ |___/ |_| #
# ==================================================================================================================== #
# Authors: #
# Patrick Lehmann #
# #
# License: #
# ==================================================================================================================== #
# Copyright 2026-2026 Patrick Lehmann - Bötzingen, Germany #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
# SPDX-License-Identifier: Apache-2.0 #
# ==================================================================================================================== #
#
"""
A converter of GitHub-flavoured Markdown into docutils nodes.
The Markdown is e.g. the notes of a GitHub release. :class:`GitHubMarkdown` parses it with markdown-it-py and the
GFM plugin of mdit-py-plugins - CommonMark plus tables, strikethrough, task lists, alerts, autolinks and footnotes -,
and :class:`~pyTooling.Sphinx.Markdown.MarkdownTranslator` translates the tokens into the nodes a directive
returns:
.. code-block:: Python
converter = GitHubMarkdown("pyTooling", "pyTooling")
return converter.Convert(markdown, self.state.document, location=self.get_location())
.. rubric:: GitHub's references
GitHub links what Markdown leaves as text: ``#123``, ``GH-123`` and ``owner/name#123`` to an issue or a pull request,
``@user`` to a user, a commit's hash (7 to 40 hexadecimal digits) to the commit, and ``:tada:`` is an emoji. The
converter does the same in text outside of code and links (see :meth:`GitHubMarkdown.Parse`). GitHub links a
hash only if the commit exists; the converter can't ask, so it links a word of hexadecimal digits holding a digit and a
letter.
.. rubric:: Emoji
The emoji, their characters and shortcodes, are GitHub's own list: the table :data:`EMOJI_FILE` is generated from
github/gemoji's :file:`db/emoji.json`, which GitHub, Inc. distributes under the MIT license; the table's header names
the version and holds the license text. A custom GitHub emoji, e.g. ``:octocat:``, is an image without characters, and
isn't in the list.
.. rubric:: Headings and raw HTML
A heading becomes a topic or a rubric, never a section, and raw HTML is translated element by element, as
:mod:`~pyTooling.Sphinx.Markdown` describes.
.. rubric:: LaTeX
An emoji is a node of its own (:class:`~pyTooling.Sphinx.Node.Emoji`), whose characters LaTeX writes as they are, for
LuaLaTeX and an emoji font.
LaTeX can't include an SVG image, e.g. a badge. :class:`RasterizeShieldsBadges` fetches a shields.io badge as PNG;
every other SVG image is converted by Sphinx' ImageMagick converter, :mod:`sphinx.ext.imgconverter`, which
pyTooling.Sphinx loads. Without ImageMagick, Sphinx warns, and LaTeX stops at the image with "Unknown graphics
extension: .svg".
.. attention::
Packages ``markdown_it`` and ``mdit_py_plugins`` are imported when a :class:`GitHubMarkdown` is created, not when
this module is imported. They are installed with the extra ``markdown``.
"""
from __future__ import annotations
from re import compile as re_compile, escape as re_escape
from typing import TYPE_CHECKING, Any, ClassVar, Optional as Nullable, Pattern
from urllib.parse import urlsplit
from docutils import nodes
from sphinx.transforms.post_transforms import SphinxPostTransform
from pyTooling.Common import getFullyQualifiedName, readResourceFile
from pyTooling.Decorators import export, readonly
from pyTooling.Exceptions import MissingDependencyError
from pyTooling.MetaClasses import ExtendedType
from pyTooling.Sphinx import Resources as SphinxResources
from pyTooling.Sphinx.Markdown import IMAGE_CLASS, MarkdownTranslator
from pyTooling.Sphinx.Node import Emoji
from pyTooling.Sphinx.Shields import SHIELDS_SERVICE_PNG, SHIELDS_SERVICE_SVG
if TYPE_CHECKING: # pragma: no cover
from markdown_it import MarkdownIt
__all__ = ["EMOJI_FILE", "OWNER_PATTERN", "REPOSITORY_NAME_PATTERN", "REPOSITORY_PATTERN", "REFERENCE_PATTERN"]
#: Name of the emoji table in :mod:`pyTooling.Sphinx.Resources`: a line per emoji, its code points and its shortcodes,
#: generated from github/gemoji (MIT license, Copyright (c) 2019 GitHub, Inc.).
EMOJI_FILE = "GitHubEmoji.txt"
#: A GitHub owner - a user or an organization: ASCII letters, digits and single hyphens, not starting with a hyphen.
OWNER_PATTERN = re_compile(r"[A-Za-z0-9](?:-?[A-Za-z0-9])*")
#: A GitHub repository's name: ASCII letters, digits, ``.``, ``_`` and ``-``.
REPOSITORY_NAME_PATTERN = re_compile(r"[A-Za-z0-9._-]+")
#: A GitHub repository with its owner, as a reference writes it: ``<owner>/<name>``.
REPOSITORY_PATTERN = re_compile(rf"{OWNER_PATTERN.pattern}/{REPOSITORY_NAME_PATTERN.pattern}")
_BEFORE = r"(?<![\w/@#.-])"
"""What can't precede a reference: a word character, or a character continuing a path, a reference or a URL."""
#: An emoji's shortcode, or one of GitHub's references: an issue or pull request, a commit, or a user.
REFERENCE_PATTERN = re_compile(
r":(?P<shortcode>[a-z0-9_+-]+):"
rf"|{_BEFORE}(?:(?P<issueRepository>{REPOSITORY_PATTERN.pattern})#|GH-|#)(?P<issue>[0-9]+)(?![\w-])"
rf"|{_BEFORE}(?:(?P<commitRepository>{REPOSITORY_PATTERN.pattern})@)?(?P<sha>[0-9a-f]{{7,40}})(?![\w-])"
rf"|{_BEFORE}@(?P<user>{OWNER_PATTERN.pattern})(?![\w/-])"
)
@export
class GitHubMarkdown(metaclass=ExtendedType, slots=True):
"""
A converter of GitHub-flavoured Markdown into docutils nodes, which links GitHub's references in a repository's
context.
The owner and the repository are the ones ``#123`` refers to, and the server URL the one every link starts with.
:meth:`Convert` converts a Markdown document into docutils nodes, :meth:`Parse` parses a text without Markdown for
GitHub's references and emoji.
GitHub writes a line break of an issue, a comment or a release's notes as a line break, not as a space:
:attr:`LineBreaks` selects which.
"""
_emojis: ClassVar[Nullable[dict[str, str]]] = None #: The emoji's characters by shortcode, once read.
_shortcodes: ClassVar[Nullable[dict[str, str]]] = None #: A shortcode per emoji, by its characters without U+FE0F.
_emojiPattern: ClassVar[Nullable[Pattern[str]]] = None #: The pattern finding emoji written as characters.
_owner: str #: The owner of the repository references refer to, a user or an organization.
_repository: str #: The name of the repository references refer to.
_serverURL: str #: The URL of the GitHub server, without a trailing ``/``.
_lineBreaks: bool #: Whether a line break in a paragraph is written as a line break.
_parser: MarkdownIt #: The Markdown parser.
def __init__(
self,
owner: str,
repository: str,
serverURL: str = "https://github.com",
lineBreaks: bool = True
) -> None:
"""
Initializes a converter for the Markdown of a repository.
:param owner: The owner of the repository references refer to, a user or an organization.
:param repository: The name of the repository references refer to.
:param serverURL: Optional, URL of the GitHub server. Default: ``"https://github.com"``.
:param lineBreaks: Optional, whether a line break in a paragraph is written as a line break, as
GitHub does in a release's notes. Default: ``True``.
:raises ValueError: If parameter ``owner`` is ``None``.
:raises TypeError: If parameter ``owner`` isn't of type :class:`str`.
:raises ValueError: If parameter ``owner`` isn't a GitHub user's or organization's name.
:raises ValueError: If parameter ``repository`` is ``None``.
:raises TypeError: If parameter ``repository`` isn't of type :class:`str`.
:raises ValueError: If parameter ``repository`` isn't a GitHub repository's name.
:raises ValueError: If parameter ``serverURL`` is ``None``.
:raises TypeError: If parameter ``serverURL`` isn't of type :class:`str`.
:raises ValueError: If parameter ``serverURL`` isn't an HTTP or HTTPS URL.
:raises ValueError: If parameter ``lineBreaks`` is ``None``.
:raises TypeError: If parameter ``lineBreaks`` isn't of type :class:`bool`.
:raises MissingDependencyError: If the extra ``markdown`` isn't installed.
"""
if owner is None:
raise ValueError("Parameter 'owner' is None.")
elif not isinstance(owner, str):
ex = TypeError("Parameter 'owner' is not of type 'str'.")
ex.add_note(f"Got type '{getFullyQualifiedName(owner)}'.")
raise ex
elif OWNER_PATTERN.fullmatch(owner) is None:
ex = ValueError("Parameter 'owner' is not a GitHub user's or organization's name.")
ex.add_note(f"Got value '{owner}'.")
raise ex
if repository is None:
raise ValueError("Parameter 'repository' is None.")
elif not isinstance(repository, str):
ex = TypeError("Parameter 'repository' is not of type 'str'.")
ex.add_note(f"Got type '{getFullyQualifiedName(repository)}'.")
raise ex
elif REPOSITORY_NAME_PATTERN.fullmatch(repository) is None:
ex = ValueError("Parameter 'repository' is not a GitHub repository's name.")
ex.add_note(f"Got value '{repository}'.")
raise ex
if serverURL is None:
raise ValueError("Parameter 'serverURL' is None.")
elif not isinstance(serverURL, str):
ex = TypeError("Parameter 'serverURL' is not of type 'str'.")
ex.add_note(f"Got type '{getFullyQualifiedName(serverURL)}'.")
raise ex
elif urlsplit(serverURL).scheme not in ("https", "http") or urlsplit(serverURL).netloc == "":
ex = ValueError("Parameter 'serverURL' is not an HTTP or HTTPS URL.")
ex.add_note(f"Got value '{serverURL}'.")
raise ex
if lineBreaks is None:
raise ValueError("Parameter 'lineBreaks' is None.")
elif not isinstance(lineBreaks, bool):
ex = TypeError("Parameter 'lineBreaks' is not of type 'bool'.")
ex.add_note(f"Got type '{getFullyQualifiedName(lineBreaks)}'.")
raise ex
try:
from markdown_it import MarkdownIt
except ImportError as ex:
raise MissingDependencyError(dependency="markdown-it-py", extra="markdown") from ex
try:
from mdit_py_plugins.gfm import gfm_plugin
except ImportError as ex:
raise MissingDependencyError(dependency="mdit-py-plugins", extra="markdown") from ex
self._owner = owner
self._repository = repository
self._serverURL = serverURL.rstrip("/")
self._lineBreaks = lineBreaks
# 'text_join' would merge an escaped character into the text around it, e.g. '\#12' into a reference
self._parser = MarkdownIt("commonmark", {"html": True}).use(gfm_plugin).disable("text_join")
if GitHubMarkdown._emojis is None:
emojis: dict[str, str] = {}
shortcodes: dict[str, str] = {}
for line in readResourceFile(SphinxResources, EMOJI_FILE).splitlines():
if line.startswith("#") or line.strip() == "":
continue
codePoints, *names = line.split()
characters = "".join(chr(int(codePoint, 16)) for codePoint in codePoints.split("-"))
shortcodes[characters.replace("\ufe0f", "")] = names[0]
for name in names:
emojis[name] = characters
GitHubMarkdown._emojiPattern = self._EmojiPattern(shortcodes)
GitHubMarkdown._shortcodes = shortcodes
GitHubMarkdown._emojis = emojis
@staticmethod
def _EmojiPattern(shortcodes: dict[str, str]) -> Pattern[str]:
"""
Build the pattern finding the emoji written as characters in a text: a trie of their characters, each optionally
followed by variation selector 16 (U+FE0F).
A single character below U+2190 - e.g. the copyright sign - is an emoji only if followed by U+FE0F; else it is
a character of the text font.
:param shortcodes: The emoji's characters without U+FE0F.
:returns: The pattern.
"""
trie: dict[str, Any] = {}
for characters in shortcodes:
level = trie
for character in characters:
level = level.setdefault(character, {})
level[""] = {}
def alternatives(level: dict[str, Any], depth: int) -> str:
"""
Nested function for recursion.
:param level: A level of the trie, by character; key ``""`` marks the end of an emoji.
:param depth: The level's depth, ``0`` for the first character.
:returns: The level's pattern.
"""
branches = []
for character, nextLevel in sorted(level.items()):
if character == "":
continue
selector = "\ufe0f" if depth == 0 and ord(character) < 0x2190 and list(nextLevel) == [""] else "\ufe0f?"
branches.append(f"{re_escape(character)}{selector}{alternatives(nextLevel, depth + 1)}")
if len(branches) == 0:
return ""
optional = "?" if "" in level else ""
return f"(?:{'|'.join(branches)}){optional}"
return re_compile(alternatives(trie, 0))
@readonly
def Owner(self) -> str:
"""
Read-only property to access the owner of the repository references refer to (:attr:`_owner`).
:returns: The owner, a user or an organization.
"""
return self._owner
@readonly
def Repository(self) -> str:
"""
Read-only property to access the name of the repository references refer to (:attr:`_repository`).
:returns: The repository's name.
"""
return self._repository
@readonly
def ServerURL(self) -> str:
"""
Read-only property to access the URL of the GitHub server (:attr:`_serverURL`).
:returns: The URL, without a trailing ``/``.
"""
return self._serverURL
@readonly
def LineBreaks(self) -> bool:
"""
Read-only property to access whether a line break in a paragraph is written as a line break (:attr:`_lineBreaks`).
:returns: ``True``, if a line break is written as a line break; ``False``, if as a space.
"""
return self._lineBreaks
def Convert(
self,
markdown: str,
document: nodes.document,
repeatedTitle: str = "",
location: Nullable[str] = None
) -> list[nodes.Node]:
"""
Convert a Markdown document into docutils nodes.
The nodes are body elements a directive returns; headings become topics and rubrics (see the module's
description). A footnote's ID is unique in the document.
The first heading of the Markdown can repeat the title, which the nodes are put under, e.g. ``# GHDL 5.0.1`` in
the notes of a release titled so. Given as ``repeatedTitle``, such a heading is skipped, so the headings after it
become the topics.
:param markdown: The Markdown.
:param document: The document the nodes are put into.
:param repeatedTitle: Optional, the title the first heading is skipped for; compared without surrounding and
repeated whitespace. Default: ``""``, no heading is skipped.
:param location: Optional, location the warnings are reported at, e.g. ``index.rst:12``. Default: ``None``.
:returns: The nodes.
:raises ValueError: If parameter ``markdown`` is ``None``.
:raises TypeError: If parameter ``markdown`` isn't of type :class:`str`.
:raises ValueError: If parameter ``document`` is ``None``.
:raises TypeError: If parameter ``document`` isn't of type :class:`~docutils.nodes.document`.
:raises ValueError: If parameter ``repeatedTitle`` is ``None``.
:raises TypeError: If parameter ``repeatedTitle`` isn't of type :class:`str`.
:raises TypeError: If parameter ``location`` isn't of type :class:`str`.
"""
if markdown is None:
raise ValueError("Parameter 'markdown' is None.")
elif not isinstance(markdown, str):
ex = TypeError("Parameter 'markdown' is not of type 'str'.")
ex.add_note(f"Got type '{getFullyQualifiedName(markdown)}'.")
raise ex
if document is None:
raise ValueError("Parameter 'document' is None.")
elif not isinstance(document, nodes.document):
ex = TypeError("Parameter 'document' is not of type 'docutils.nodes.document'.")
ex.add_note(f"Got type '{getFullyQualifiedName(document)}'.")
raise ex
if repeatedTitle is None:
raise ValueError("Parameter 'repeatedTitle' is None.")
elif not isinstance(repeatedTitle, str):
ex = TypeError("Parameter 'repeatedTitle' is not of type 'str'.")
ex.add_note(f"Got type '{getFullyQualifiedName(repeatedTitle)}'.")
raise ex
if location is not None and not isinstance(location, str):
ex = TypeError("Parameter 'location' is not of type 'str'.")
ex.add_note(f"Got type '{getFullyQualifiedName(location)}'.")
raise ex
tokens = self._parser.parse(markdown)
if repeatedTitle != "":
for index, token in enumerate(tokens):
if token.type == "heading_open" and token.level == 0:
if " ".join(tokens[index + 1].content.split()) == " ".join(repeatedTitle.split()):
del tokens[index:index + 3]
break
return MarkdownTranslator(self, document, location).Translate(tokens)
def Parse(self, text: str, references: bool = True) -> list[nodes.Node]:
"""
Parse a text for GitHub's references and emoji into docutils nodes, linking the references and writing emoji as
:class:`~pyTooling.Sphinx.Node.Emoji` nodes.
An emoji is written as its shortcode, e.g. ``:tada:``, or as its characters. A shortcode GitHub doesn't know
stays text, as does a custom GitHub emoji, which has no characters, e.g. ``:octocat:``.
:param text: The text, without Markdown.
:param references: Optional, whether GitHub's references are linked; ``False`` e.g. for a link's text. Default:
``True``.
:returns: The text, references and emoji.
:raises ValueError: If parameter ``text`` is ``None``.
:raises TypeError: If parameter ``text`` isn't of type :class:`str`.
"""
if text is None:
raise ValueError("Parameter 'text' is None.")
elif not isinstance(text, str):
ex = TypeError("Parameter 'text' is not of type 'str'.")
ex.add_note(f"Got type '{getFullyQualifiedName(text)}'.")
raise ex
result: list[nodes.Node] = []
ownRepository = f"{self._owner}/{self._repository}"
position = 0
for match in REFERENCE_PATTERN.finditer(text):
written = match[0]
if (shortcode := match["shortcode"]) is not None:
if (characters := self._emojis.get(shortcode)) is None:
continue
node: nodes.Node = Emoji(characters, characters, shortcode=shortcode)
elif not references:
continue
elif (issue := match["issue"]) is not None:
repository = ownRepository if match["issueRepository"] is None else match["issueRepository"]
node = nodes.reference(
written, written, refuri=f"{self._serverURL}/{repository}/issues/{issue}", classes=["github-issue"]
)
elif (sha := match["sha"]) is not None:
if sha.isdigit() or not any(character.isdigit() for character in sha):
continue
repository = ownRepository if match["commitRepository"] is None else match["commitRepository"]
node = nodes.reference(
written, "", refuri=f"{self._serverURL}/{repository}/commit/{sha}", classes=["github-commit"]
)
if match["commitRepository"] is not None:
node += nodes.Text(f"{repository}@")
node += nodes.literal(sha[:7], sha[:7])
else:
node = nodes.reference(
written, written, refuri=f"{self._serverURL}/{match['user']}", classes=["github-user"]
)
result.extend(self._ConvertEmojiCharacters(text[position:match.start()]))
result.append(node)
position = match.end()
result.extend(self._ConvertEmojiCharacters(text[position:]))
return result
def _ConvertEmojiCharacters(self, text: str) -> list[nodes.Node]:
"""
Convert a text into text and :class:`~pyTooling.Sphinx.Node.Emoji` nodes for the emoji written as characters.
:param text: The text.
:returns: The text and the emoji; nothing for an empty text.
"""
result: list[nodes.Node] = []
position = 0
for match in self._emojiPattern.finditer(text):
if match.end() == match.start():
continue
if match.start() > position:
result.append(nodes.Text(text[position:match.start()]))
characters = match[0]
result.append(Emoji(characters, characters, shortcode=self._shortcodes[characters.replace("\ufe0f", "")]))
position = match.end()
if position < len(text):
result.append(nodes.Text(text[position:]))
return result
@export
class RasterizeShieldsBadges(SphinxPostTransform):
"""
A post-transform fetching the shields.io badges of converted Markdown as PNG in LaTeX, which can't include SVG.
A badge's URI is moved to :data:`~pyTooling.Sphinx.Shields.SHIELDS_SERVICE_PNG`, without a suffix ``.svg``. Every
other SVG image is left to :mod:`sphinx.ext.imgconverter`, which pyTooling.Sphinx loads and which converts it with
ImageMagick.
"""
default_priority = 50 #: Priority among the post-transforms: before Sphinx downloads remote images.
formats = ("latex", ) #: Output formats the transform runs for.
def run(self, **kwargs: Any) -> None:
"""
Move the shields.io badges of converted Markdown to the service rasterizing them.
The images of converted Markdown have class :data:`~pyTooling.Sphinx.Markdown.IMAGE_CLASS`.
:param kwargs: Keyword arguments Sphinx passes to a post-transform; none is used.
"""
for image in self.document.findall(nodes.image):
if IMAGE_CLASS not in image["classes"]:
continue
uri = urlsplit(image["uri"])
if f"{uri.scheme}://{uri.netloc}" == SHIELDS_SERVICE_SVG:
raster = urlsplit(SHIELDS_SERVICE_PNG)._replace(path=uri.path.removesuffix(".svg"), query=uri.query)
image["uri"] = raster.geturl()
image["candidates"] = {"?": image["uri"]}
|