summaryrefslogtreecommitdiff
path: root/beautifulsoup/dammit.py
diff options
context:
space:
mode:
authorLeonard Richardson <leonard.richardson@canonical.com>2011-02-26 23:47:06 -0500
committerLeonard Richardson <leonard.richardson@canonical.com>2011-02-26 23:47:06 -0500
commitdc5682014c7360e723d4861d32ee933eea8fcd5d (patch)
tree021886f2b0e2158d87d33417b8b687dfb1cee923 /beautifulsoup/dammit.py
parentace32031ac6c9787ee46c5ab19e6f71b99cd26d3 (diff)
parentd9f49a66e4a7dfd93823f2396796ed6c55f69648 (diff)
Minor late-night tweaks.
Diffstat (limited to 'beautifulsoup/dammit.py')
-rw-r--r--beautifulsoup/dammit.py26
1 files changed, 16 insertions, 10 deletions
diff --git a/beautifulsoup/dammit.py b/beautifulsoup/dammit.py
index 31dfa95..4483118 100644
--- a/beautifulsoup/dammit.py
+++ b/beautifulsoup/dammit.py
@@ -31,6 +31,8 @@ except ImportError:
class EntitySubstitution(object):
+ """Substitute XML or HTML entities for the corresponding characters."""
+
def _populate_class_variables():
lookup = {}
characters = []
@@ -61,17 +63,20 @@ class EntitySubstitution(object):
"&(?!#\d+;|#x[0-9a-fA-F]+;|\w+;)"
")")
- def _substitute_html_entity(self, matchobj):
- entity = self.CHARACTER_TO_HTML_ENTITY.get(matchobj.group(0))
+ @classmethod
+ def _substitute_html_entity(cls, matchobj):
+ entity = cls.CHARACTER_TO_HTML_ENTITY.get(matchobj.group(0))
return "&%s;" % entity
- def _substitute_xml_entity(self, matchobj):
+ @classmethod
+ def _substitute_xml_entity(cls, matchobj):
"""Used with a regular expression to substitute the
appropriate XML entity for an XML special character."""
- entity = self.CHARACTER_TO_XML_ENTITY[matchobj.group(0)]
+ entity = cls.CHARACTER_TO_XML_ENTITY[matchobj.group(0)]
return "&%s;" % entity
- def substitute_xml(self, value, make_quoted_attribute=False):
+ @classmethod
+ def substitute_xml(cls, value, make_quoted_attribute=False):
"""Substitute XML entities for special XML characters.
:param value: A string to be substituted. The less-than sign will
@@ -117,14 +122,15 @@ class EntitySubstitution(object):
# Escape angle brackets, and ampersands that aren't part of
# entities.
- value = self.BARE_AMPERSAND_OR_BRACKET.sub(
- self._substitute_xml_entity, value)
+ value = cls.BARE_AMPERSAND_OR_BRACKET.sub(
+ cls._substitute_xml_entity, value)
if make_quoted_attribute:
return quote_with + value + quote_with
else:
return value
- def substitute_html(self, s):
+ @classmethod
+ def substitute_html(cls, s):
"""Replace certain Unicode characters with named HTML entities.
This differs from data.encode(encoding, 'xmlcharrefreplace')
@@ -135,8 +141,8 @@ class EntitySubstitution(object):
character with "&eacute;" will make it more readable to some
people.
"""
- return self.CHARACTER_TO_HTML_ENTITY_RE.sub(
- self._substitute_html_entity, s)
+ return cls.CHARACTER_TO_HTML_ENTITY_RE.sub(
+ cls._substitute_html_entity, s)
class UnicodeDammit: