summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--wikimini/__init__.py23
-rw-r--r--wikimini/templates/__init__.py14
2 files changed, 32 insertions, 5 deletions
diff --git a/wikimini/__init__.py b/wikimini/__init__.py
index 876c7a7..00179cc 100644
--- a/wikimini/__init__.py
+++ b/wikimini/__init__.py
@@ -25,13 +25,26 @@ class Wikimini:
"""The main object for Wikipedia/Gemini access and conversion.
Attributes:
- api_url (str): The base URL of the API.
- table_format (str): The name of the table style, see
- :const:`TABLE_FORMAT`.
+ api_url: The base URL of the API.
+ table_format: The name of the table style, see :const:`TABLE_FORMAT`.
+ template_registry: The template registry to use. If :any:`None`, the
+ global registry will be used.
"""
- def __init__(self, api_url=API_URL, table_format=TABLE_FORMAT):
+ api_url: str
+ table_format: str
+ template_registry: "templates.Registry"
+
+ def __init__(
+ self,
+ api_url=API_URL,
+ table_format=TABLE_FORMAT,
+ template_registry=None,
+ ):
self.api_url = api_url
self.table_format = table_format
+ if template_registry is None:
+ template_registry = templates.registry
+ self.template_registry = template_registry
def page_url(self, title: str) -> str:
"""Returns the link for page with the given title.
@@ -180,7 +193,7 @@ class Wikimini:
# however, some of them are useful and provide some output that we
# should mimic (for example, the convert template).
name = str(obj.name)
- template = templates.registry.get(name)
+ template = self.template_registry.get(name)
if template is None:
return default(obj)
else:
diff --git a/wikimini/templates/__init__.py b/wikimini/templates/__init__.py
index 360b3fa..ebe3bbc 100644
--- a/wikimini/templates/__init__.py
+++ b/wikimini/templates/__init__.py
@@ -6,6 +6,7 @@ A template is a function that takes the :class:`~wikimini.Wikimini` instance
and the :class:`~mwparserfromhell.nodes.template.Template` node to convert, and
returns a string with the template output (see :const:`Template`).
"""
+import copy
from typing import Callable, Optional
import mwparserfromhell as mwp
@@ -49,6 +50,19 @@ class Registry:
"""
self.templates[name] = template
+ def copy(self) -> "Registry":
+ """Returns a copy of the registry.
+
+ This is useful if you want to use the global registry as a starting
+ point to add your own templates, without modifying the global instance.
+
+ Returns:
+ A copy of the registry.
+ """
+ copied = Registry()
+ copied.templates = copy.copy(self.templates)
+ return copied
+
#: The global template registry.
registry = Registry()