mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 18:24:30 -04:00
Rename function so can detect if its available
This commit is contained in:
parent
6524665e5b
commit
289192f595
@ -449,8 +449,15 @@ def my_unichr(num):
|
|||||||
except (ValueError, OverflowError):
|
except (ValueError, OverflowError):
|
||||||
return '?'
|
return '?'
|
||||||
|
|
||||||
|
XML_ENTITIES = {
|
||||||
|
'"' : '"',
|
||||||
|
"'" : ''',
|
||||||
|
'<' : '<',
|
||||||
|
'>' : '>',
|
||||||
|
'&' : '&'
|
||||||
|
}
|
||||||
|
|
||||||
def entity_to_unicode(match, exceptions=[], encoding='cp1252',
|
def entity_to_unicode(match, exceptions=(), encoding='cp1252',
|
||||||
result_exceptions={}):
|
result_exceptions={}):
|
||||||
'''
|
'''
|
||||||
:param match: A match object such that '&'+match.group(1)';' is the entity.
|
: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+?);')
|
_ent_pat = re.compile(r'&(\S+?);')
|
||||||
xml_entity_to_unicode = partial(entity_to_unicode, result_exceptions={
|
xml_entity_to_unicode = partial(entity_to_unicode, result_exceptions=XML_ENTITIES)
|
||||||
'"' : '"',
|
|
||||||
"'" : ''',
|
|
||||||
'<' : '<',
|
|
||||||
'>' : '>',
|
|
||||||
'&' : '&'})
|
|
||||||
|
|
||||||
|
|
||||||
def replace_entities(raw, encoding='cp1252'):
|
def replace_entities(raw, encoding='cp1252'):
|
||||||
|
@ -142,7 +142,7 @@ replace(const char *input, size_t input_sz, char *output, int keep_xml_entities)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static PyObject*
|
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; }
|
if (nargs < 1) { PyErr_SetString(PyExc_TypeError, "Must specify string tp process"); return NULL; }
|
||||||
const char *input = NULL; Py_ssize_t input_sz = 0;
|
const char *input = NULL; Py_ssize_t input_sz = 0;
|
||||||
int keep_xml_entities = false;
|
int keep_xml_entities = false;
|
||||||
@ -168,8 +168,8 @@ replace_entities(PyObject *self, PyObject *const *args, Py_ssize_t nargs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static PyMethodDef methods[] = {
|
static PyMethodDef methods[] = {
|
||||||
{"replace_entities", (PyCFunction)replace_entities, METH_FASTCALL,
|
{"replace_all_entities", (PyCFunction)replace_all_entities, METH_FASTCALL,
|
||||||
"Replace entities in the specified string"
|
"Replace all entities in the specified string"
|
||||||
},
|
},
|
||||||
{NULL, NULL, 0, NULL}
|
{NULL, NULL, 0, NULL}
|
||||||
};
|
};
|
||||||
|
@ -2139,11 +2139,11 @@ def find_tests():
|
|||||||
import unittest
|
import unittest
|
||||||
class TestHTMLEntityReplacement(unittest.TestCase):
|
class TestHTMLEntityReplacement(unittest.TestCase):
|
||||||
def test_html_entity_replacement(self):
|
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):
|
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):
|
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Ӓb', 'aӒb')
|
t('aӒb', 'aӒb')
|
||||||
t('', '')
|
t('', '')
|
||||||
t('a', 'a')
|
t('a', 'a')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user