summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--wikimini/__init__.py53
-rw-r--r--wikimini/document.py3
-rw-r--r--wikimini/templates/__init__.py6
-rw-r--r--wikimini/templates/mainlinks.py1
-rw-r--r--wikimini/templates/quotes.py1
5 files changed, 36 insertions, 28 deletions
diff --git a/wikimini/__init__.py b/wikimini/__init__.py
index fac0c84..d6e49a1 100644
--- a/wikimini/__init__.py
+++ b/wikimini/__init__.py
@@ -1,12 +1,14 @@
import mwparserfromhell as mwp
import requests
-import re
from tabulate import tabulate
from typing import List, Union, Tuple
-from .document import *
+from .document import (
+ Plain, BlockLink, InlineLink, Verbatim, Document, Node, Block, ItemList,
+ Paragraph, Heading, insert_into, extract_plaintext,
+)
#: The default API URL, pointing to the english Wikipedia.
API_URL = "https://en.wikipedia.org/w/api.php"
@@ -79,9 +81,9 @@ class Wikimini:
return (title, mwp.parse(text))
def convert(
- self,
- obj: Union[mwp.wikicode.Wikicode, mwp.nodes.Node],
- ) -> Union[Document, List[Node], List[Block]]:
+ self,
+ obj: Union[mwp.wikicode.Wikicode, mwp.nodes.Node],
+ ) -> Union[Document, List[Node], List[Block]]:
"""Function that converts and renders a node.
This function is exposed for template implementors, for normal usage,
@@ -116,11 +118,12 @@ class Wikimini:
if current == []:
pass
- # Special case: We're starting a list, but we're already in a list
- elif (document and len(current) == 1 and
- isinstance(current[0], ItemList) and
- isinstance(document[-1], ItemList) and
- document[-1].ordered == current[0].ordered):
+ # Special case: We're starting a list, but we're already in a
+ # list
+ elif (document and len(current) == 1
+ and isinstance(current[0], ItemList)
+ and isinstance(document[-1], ItemList)
+ and document[-1].ordered == current[0].ordered):
pass
# Special case: We're starting a list!
elif len(current) == 1 and isinstance(current[0], ItemList):
@@ -135,8 +138,9 @@ class Wikimini:
elif isinstance(obj, mwp.nodes.heading.Heading):
return [Heading(obj.level, obj.title.strip_code())]
elif isinstance(obj, mwp.nodes.tag.Tag):
- # Most tags are handled just fine and can be delegated to strip_code
- # (inline text styles), however we can do a bit better for list tags.
+ # Most tags are handled just fine and can be delegated to
+ # strip_code (inline text styles), however we can do a bit better
+ # for list tags.
if str(obj.wiki_markup) == "*":
return [ItemList([], False)]
elif str(obj.wiki_markup) == "#":
@@ -164,15 +168,15 @@ class Wikimini:
rows.append(parsed)
else:
header = parsed
- return [
- Verbatim(tabulate(rows, header, tablefmt=self.table_format))
- ]
+ return [Verbatim(
+ tabulate(rows, header, tablefmt=self.table_format)
+ )]
else:
return default(obj)
elif isinstance(obj, mwp.nodes.template.Template):
- # Most templates are handled fine (and completely stripped), however,
- # some of them are useful and provide some output that we should mimic
- # (for example, the convert template).
+ # Most templates are handled fine (and completely stripped),
+ # 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)
if template is None:
@@ -180,7 +184,8 @@ class Wikimini:
else:
return template(self, obj)
elif isinstance(obj, mwp.nodes.wikilink.Wikilink):
- if str(obj.title).startswith("File:") or str(obj.text).startswith("thumb|"):
+ if (str(obj.title).startswith("File:")
+ or str(obj.text).startswith("thumb|")):
return []
elif str(obj.title).startswith("Category:"):
return []
@@ -200,9 +205,9 @@ class Wikimini:
:class:`~document.Document` representation.
Note that wikicode is much more powerful than the internal
- representation, so this is a lossy function. The returned document tries
- to mimic the content of the Wikicode as much as possible (for human
- consumption).
+ representation, so this is a lossy function. The returned document
+ tries to mimic the content of the Wikicode as much as possible (for
+ human consumption).
This function mostly mimics
:meth:`~mwparserfromhell.wikicode.Wikicode.strip_code`, with some
@@ -215,8 +220,8 @@ class Wikimini:
The converted Document.
"""
# Avoid calling str() on the whole Wikicode here
- if (isinstance(obj, mwp.wikicode.Wikicode) and
- str(mwp.wikicode.Wikicode(obj.nodes[:2])) == "#REDIRECT "):
+ if (isinstance(obj, mwp.wikicode.Wikicode)
+ and str(mwp.wikicode.Wikicode(obj.nodes[:2])) == "#REDIRECT "):
document = Document()
title = str(obj.nodes[2].title)
if "#" in title:
diff --git a/wikimini/document.py b/wikimini/document.py
index 2c901dc..be51108 100644
--- a/wikimini/document.py
+++ b/wikimini/document.py
@@ -7,6 +7,7 @@ import re
from dataclasses import dataclass, replace
from typing import List, Union
+
class Document:
"""A rendered Wikipedia article.
@@ -385,7 +386,7 @@ def insert_into(blocks: List[Block], node: Node):
if "\n\n" in node.plain():
idx = node.plain().index("\n\n")
left = node[:idx]
- right = node[idx+2:]
+ right = node[idx + 2:]
current_block.append(left)
blocks.append(Paragraph([]))
insert_into(blocks, right)
diff --git a/wikimini/templates/__init__.py b/wikimini/templates/__init__.py
index 9e983e1..58a5db8 100644
--- a/wikimini/templates/__init__.py
+++ b/wikimini/templates/__init__.py
@@ -2,9 +2,9 @@
This module contains functions that mimic Wikipedia's templates.
-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`).
+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`).
"""
from typing import Callable, Optional
diff --git a/wikimini/templates/mainlinks.py b/wikimini/templates/mainlinks.py
index 8c9abbf..3d945ed 100644
--- a/wikimini/templates/mainlinks.py
+++ b/wikimini/templates/mainlinks.py
@@ -3,6 +3,7 @@ from . import registry
from ..document import Paragraph, Plain, BlockLink
+
def tmpl_main(wikimini, obj):
"""Renders the ``{{main|...}}`` template."""
links = [
diff --git a/wikimini/templates/quotes.py b/wikimini/templates/quotes.py
index 39f6fa2..fdf00cc 100644
--- a/wikimini/templates/quotes.py
+++ b/wikimini/templates/quotes.py
@@ -3,6 +3,7 @@ from . import registry
from ..document import Blockquote
+
def tmpl_quote(wikimini, obj):
"""Renders the ``{{blockquote|...}}`` template."""
text = obj.get("text", None)