From 89204f8002ee49caa408e669df699841ea7db0d5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 20 Nov 2014 09:32:17 +0530 Subject: [PATCH] Handle HTML entities in the example replace function --- manual/function_mode.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/manual/function_mode.rst b/manual/function_mode.rst index 6600ae584f..37ca42a07c 100644 --- a/manual/function_mode.rst +++ b/manual/function_mode.rst @@ -79,6 +79,8 @@ write a simple function to automatically find and fix such words. .. code-block:: python import regex + from calibre import replace_entities + from calibre import prepare_string_for_xml def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): @@ -91,7 +93,9 @@ write a simple function to automatically find and fix such words. return wmatch.group() # Search for words split by a hyphen - return regex.sub(r'(\w+)\s*-\s*(\w+)', replace_word, match.group(), flags=regex.VERSION1 | regex.UNICODE) + text = replace_entities(match.group()[1:-1]) # Handle HTML entities like & + corrected = regex.sub(r'(\w+)\s*-\s*(\w+)', replace_word, text, flags=regex.VERSION1 | regex.UNICODE) + return '>%s<' % prepare_string_for_xml(corrected) # Put back required entities Use this function with the same find expression as before, namely::