-
-
Notifications
You must be signed in to change notification settings - Fork 409
tools, tools.web: rework web.decode() to use modern stdlib
#2205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+37
−16
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,6 @@ | |
| applications, APIs, or websites in your plugins. | ||
|
|
||
| .. versionadded:: 7.0 | ||
|
|
||
| .. note:: | ||
| Some parts of this module will remain accessible through ``sopel.web`` as | ||
| well until its final removal in Sopel 8. This is for backward | ||
| compatibility only; please update old code as soon as possible. | ||
| """ | ||
| # Copyright © 2008, Sean B. Palmer, inamidst.com | ||
| # Copyright © 2009, Michael Yanovich <[email protected]> | ||
|
|
@@ -18,12 +13,13 @@ | |
|
|
||
| from __future__ import generator_stop | ||
|
|
||
| import html | ||
| from html.entities import name2codepoint | ||
| import re | ||
| import urllib | ||
| from urllib.parse import urlparse, urlunparse | ||
|
|
||
| from sopel import __version__ | ||
| from sopel import __version__, tools | ||
|
|
||
|
|
||
| __all__ = [ | ||
|
|
@@ -85,9 +81,19 @@ | |
|
|
||
|
|
||
| r_entity = re.compile(r'&([^;\s]+);') | ||
| """Regular expression to match HTML entities.""" | ||
| """Regular expression to match HTML entities. | ||
|
|
||
| .. deprecated:: 8.0 | ||
|
|
||
| Will be removed in Sopel 9, along with :func:`entity`. | ||
| """ | ||
|
|
||
|
|
||
| @tools.deprecated( | ||
| version='8.0', | ||
| removed_in='9.0', | ||
| reason="No longer needed now that Python 3.4+ has `html.unescape()`", | ||
| ) | ||
| def entity(match): | ||
| """Convert an entity reference to the appropriate character. | ||
|
|
||
|
|
@@ -96,6 +102,12 @@ def entity(match): | |
| :return str: the Unicode character corresponding to the given ``match`` | ||
| string, or a fallback representation if the reference cannot be | ||
| resolved to a character | ||
|
|
||
| .. deprecated:: 8.0 | ||
|
|
||
| Will be removed in Sopel 9. Use :func:`decode` directly or migrate to | ||
| Python's standard-library equivalent, :func:`html.unescape`. | ||
|
|
||
| """ | ||
| value = match.group(1).lower() | ||
| if value.startswith('#x'): | ||
|
|
@@ -107,13 +119,20 @@ def entity(match): | |
| return '[' + value + ']' | ||
|
|
||
|
|
||
| def decode(html): | ||
| def decode(text): | ||
| """Decode HTML entities into Unicode text. | ||
|
|
||
| :param str html: the HTML page or snippet to process | ||
| :return str: ``html`` with all entity references replaced | ||
| :param str text: the HTML page or snippet to process | ||
| :return str: ``text`` with all entity references replaced | ||
|
|
||
| .. versionchanged:: 8.0 | ||
|
|
||
| Renamed ``html`` parameter to ``text``. (Python gained a standard | ||
| library module named :mod:`html` in version 3.4.) | ||
|
|
||
| """ | ||
| return r_entity.sub(entity, html) | ||
| # TODO deprecated? | ||
| return html.unescape(text) | ||
|
|
||
|
|
||
| def quote(string, safe='/'): | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.