Rename function so can detect if its available

This commit is contained in:
Kovid Goyal 2024-09-13 12:11:03 +05:30
parent 6524665e5b
commit 289192f595
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 15 additions and 13 deletions

View File

@ -449,8 +449,15 @@ def my_unichr(num):
except (ValueError, OverflowError):
return '?'
XML_ENTITIES = {
'"' : '"',
"'" : ''',
'<' : '&lt;',
'>' : '&gt;',
'&' : '&amp;'
}
def entity_to_unicode(match, exceptions=[], encoding='cp1252',
def entity_to_unicode(match, exceptions=(), encoding='cp1252',
result_exceptions={}):
'''
:param match: A match object such that '&'+match.group(1)';' is the entity.
@ -502,12 +509,7 @@ def entity_to_unicode(match, exceptions=[], encoding='cp1252',
_ent_pat = re.compile(r'&(\S+?);')
xml_entity_to_unicode = partial(entity_to_unicode, result_exceptions={
'"' : '&quot;',
"'" : '&apos;',
'<' : '&lt;',
'>' : '&gt;',
'&' : '&amp;'})
xml_entity_to_unicode = partial(entity_to_unicode, result_exceptions=XML_ENTITIES)
def replace_entities(raw, encoding='cp1252'):

View File

@ -142,7 +142,7 @@ replace(const char *input, size_t input_sz, char *output, int keep_xml_entities)
}
static PyObject*
replace_entities(PyObject *self, PyObject *const *args, Py_ssize_t nargs) {
replace_all_entities(PyObject *self, PyObject *const *args, Py_ssize_t nargs) {
if (nargs < 1) { PyErr_SetString(PyExc_TypeError, "Must specify string tp process"); return NULL; }
const char *input = NULL; Py_ssize_t input_sz = 0;
int keep_xml_entities = false;
@ -168,8 +168,8 @@ replace_entities(PyObject *self, PyObject *const *args, Py_ssize_t nargs) {
}
static PyMethodDef methods[] = {
{"replace_entities", (PyCFunction)replace_entities, METH_FASTCALL,
"Replace entities in the specified string"
{"replace_all_entities", (PyCFunction)replace_all_entities, METH_FASTCALL,
"Replace all entities in the specified string"
},
{NULL, NULL, 0, NULL}
};

View File

@ -2139,11 +2139,11 @@ def find_tests():
import unittest
class TestHTMLEntityReplacement(unittest.TestCase):
def test_html_entity_replacement(self):
from calibre_extensions.fast_html_entities import replace_entities
from calibre_extensions.fast_html_entities import replace_all_entities
def t(inp, exp):
self.assertEqual(exp, replace_entities(inp), f'Failed for input: {inp!r}')
self.assertEqual(exp, replace_all_entities(inp), f'Failed for input: {inp!r}')
def x(inp, exp):
self.assertEqual(exp, replace_entities(inp, True), f'Failed for input: {inp!r}')
self.assertEqual(exp, replace_all_entities(inp, True), f'Failed for input: {inp!r}')
t('a&#1234;b', 'aӒb')
t('', '')
t('a', 'a')