MCPcopy Index your code
hub / github.com/clips/pattern / decode_entities

Function decode_entities

pattern/web/__init__.py:822–838  ·  view source on GitHub ↗

Decodes HTML entities in the given string ("&lt;" => "<").

(string)

Source from the content-addressed store, hash-verified

820 return string
821
822def decode_entities(string):
823 """ Decodes HTML entities in the given string ("&lt;" => "<").
824 """
825 # http://snippets.dzone.com/posts/show/4569
826 def replace_entity(match):
827 hash, hex, name = match.group(1), match.group(2), match.group(3)
828 if hash == "#" or name.isdigit():
829 if hex == '' :
830 return unichr(int(name)) # "&#38;" => "&"
831 if hex in ("x","X"):
832 return unichr(int('0x'+name, 16)) # "&#x0026;" = > "&"
833 else:
834 cp = htmlentitydefs.name2codepoint.get(name) # "&amp;" => "&"
835 return cp and unichr(cp) or match.group() # "&foo;" => "&foo;"
836 if isinstance(string, (str, unicode)):
837 return RE_UNICODE.subn(replace_entity, string)[0]
838 return string
839
840def encode_url(string):
841 return urllib.quote_plus(bytestring(string)) # "black/white" => "black%2Fwhite".

Callers 2

plaintextFunction · 0.70
translateMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…