Dont escape - when generating CFI

Also recognize both escaped and unescaped hyphens when parsing CFI
This commit is contained in:
Kovid Goyal 2021-11-19 12:43:22 +05:30
parent d0c5241e97
commit 6a5126fe36
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 8 additions and 4 deletions

View File

@ -17,9 +17,11 @@ class Parser:
def __init__(self):
# All allowed unicode characters + escaped special characters
special_char = r'[\[\](),;=^-]'
unescaped_char = '[[\t\n\r -\ud7ff\ue000-\ufffd\U00010000-\U0010ffff]--%s]' % special_char
escaped_char = r'\^' + special_char
special_char = r'[\[\](),;=^]'
unescaped_char = f'[[\t\n\r -\ud7ff\ue000-\ufffd\U00010000-\U0010ffff]--{special_char}]'
# calibre used to escape hyphens as well, so recognize them even though
# not strictly spec compliant
escaped_char = r'\^' + special_char[:-1] + '-]'
chars = r'(?:%s|(?:%s))+' % (unescaped_char, escaped_char)
chars_no_space = chars.replace('0020', '0021')
# No leading zeros allowed for integers

View File

@ -86,7 +86,9 @@ class Tests(unittest.TestCase):
# Test parsing of text assertions
('/1:3[aa^,b]', a('aa,b'), ''),
('/1:3[aa-b]', a('aa-b'), ''),
('/1:3[aa^-b]', a('aa-b'), ''),
('/1:3[aa-^--b]', a('aa---b'), ''),
('/1:3[aa^,b,c1]', a('aa,b', 'c1'), ''),
('/1:3[,aa^,b]', a(after='aa,b'), ''),
('/1:3[;s=a]', a(s='a'), ''),

View File

@ -24,7 +24,7 @@ from __python__ import hash_literals
from read_book.viewport import scroll_viewport, rem_size
# CFI escaping {{{
escape_pat = /[\[\],^();~@!-]/g
escape_pat = /[\[\],^();~@!]/g
unescape_pat = /[\^](.)/g
def escape_for_cfi(raw):