Source code for sphinx_toolbox.wikipedia

#!/usr/bin/env python3
#
#  wikipedia.py
"""
Sphinx extension to create links to Wikipedia articles.

.. versionadded:: 0.2.0
.. extensions:: sphinx_toolbox.wikipedia


Configuration
--------------

.. latex:vspace:: -5px

.. confval:: wikipedia_lang
	:type: :class:`str`
	:required: False
	:default: ``'en'``

	The Wikipedia language to use for :rst:role:`wikipedia` roles.

	.. versionadded:: 0.2.0


Usage
------

.. latex:vspace:: -5px

.. rst:role:: wikipedia

	Role which shows a link to the given article on Wikipedia.

	The title and language can be customised.


	:bold-title:`Example`

	.. rest-example::

		:wikipedia:`Sphinx`

		:wikipedia:`mythical creature <Sphinx>`

		:wikipedia:`Answer to the Ultimate Question of Life, the Universe, and Everything <:de:42 (Antwort)>`

	.. only:: html

		.. rest-example::

			:wikipedia:`:zh:斯芬克斯`


API Reference
----------------
"""
#
#  Copyright © 2020-2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in all
#  copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
#  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
#  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
#  OR OTHER DEALINGS IN THE SOFTWARE.
#
#  Based on https://github.com/quiver/sphinx-ext-wikipedia
#  BSD Licensed
#
#  Parts of the docstrings based on https://docutils.sourceforge.io/docs/howto/rst-roles.html
#

# stdlib
import re
from typing import Dict, List, Tuple
from urllib.parse import quote

# 3rd party
from apeye.url import URL
from docutils import nodes
from docutils.nodes import system_message
from docutils.parsers.rst.states import Inliner
from sphinx.application import Sphinx
from sphinx.util.nodes import split_explicit_title

# this package
from sphinx_toolbox.utils import SphinxExtMetadata, metadata_add_version

__all__ = ("make_wikipedia_link", "setup")

base_url = "https://%s.wikipedia.org/wiki"
_wiki_lang_re = re.compile(":(.*?):(.*)")


def _get_wikipedia_lang(inliner: Inliner) -> str:  # pragma: no cover
	return inliner.document.settings.env.config.wikipedia_lang





[docs]@metadata_add_version def setup(app: Sphinx) -> SphinxExtMetadata: """ Setup :mod:`sphinx_toolbox.wikipedia`. .. versionadded:: 1.0.0 :param app: The Sphinx application. """ app.add_role("wikipedia", make_wikipedia_link) app.add_config_value("wikipedia_lang", "en", "env", [str]) return {"parallel_read_safe": True}