mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #2330 (Fix regexp escape sequence syntax warning)
This commit is contained in:
parent
532f8c1e1e
commit
bdec6576d0
@ -32,7 +32,7 @@ from polyglot.builtins import unicode_type
|
|||||||
|
|
||||||
|
|
||||||
class Templite:
|
class Templite:
|
||||||
auto_emit = re.compile('(^[\'\"])|(^[a-zA-Z0-9_\[\]\'\"]+$)')
|
auto_emit = re.compile(r'''(^['"])|(^[a-zA-Z0-9_\[\]'"]+$)''')
|
||||||
|
|
||||||
def __init__(self, template, start='${', end='}$'):
|
def __init__(self, template, start='${', end='}$'):
|
||||||
if len(start) != 2 or len(end) != 2:
|
if len(start) != 2 or len(end) != 2:
|
||||||
@ -44,18 +44,21 @@ class Templite:
|
|||||||
part = part.replace('\\'.join(list(start)), start)
|
part = part.replace('\\'.join(list(start)), start)
|
||||||
part = part.replace('\\'.join(list(end)), end)
|
part = part.replace('\\'.join(list(end)), end)
|
||||||
if i % 2 == 0:
|
if i % 2 == 0:
|
||||||
if not part: continue
|
if not part:
|
||||||
|
continue
|
||||||
part = part.replace('\\', '\\\\').replace('"', '\\"')
|
part = part.replace('\\', '\\\\').replace('"', '\\"')
|
||||||
part = '\t' * offset + 'emit("""%s""")' % part
|
part = '\t' * offset + 'emit("""%s""")' % part
|
||||||
else:
|
else:
|
||||||
part = part.rstrip()
|
part = part.rstrip()
|
||||||
if not part: continue
|
if not part:
|
||||||
|
continue
|
||||||
if part.lstrip().startswith(':'):
|
if part.lstrip().startswith(':'):
|
||||||
if not offset:
|
if not offset:
|
||||||
raise SyntaxError('no block statement to terminate: ${%s}$' % part)
|
raise SyntaxError('no block statement to terminate: ${%s}$' % part)
|
||||||
offset -= 1
|
offset -= 1
|
||||||
part = part.lstrip()[1:]
|
part = part.lstrip()[1:]
|
||||||
if not part.endswith(':'): continue
|
if not part.endswith(':'):
|
||||||
|
continue
|
||||||
elif self.auto_emit.match(part.lstrip()):
|
elif self.auto_emit.match(part.lstrip()):
|
||||||
part = 'emit(%s)' % part.lstrip()
|
part = 'emit(%s)' % part.lstrip()
|
||||||
lines = part.splitlines()
|
lines = part.splitlines()
|
||||||
@ -75,8 +78,10 @@ class Templite:
|
|||||||
**kw - keyword arguments which are added to the namespace
|
**kw - keyword arguments which are added to the namespace
|
||||||
"""
|
"""
|
||||||
namespace = {}
|
namespace = {}
|
||||||
if __namespace: namespace.update(__namespace)
|
if __namespace:
|
||||||
if kw: namespace.update(kw)
|
namespace.update(__namespace)
|
||||||
|
if kw:
|
||||||
|
namespace.update(kw)
|
||||||
namespace['emit'] = self.write
|
namespace['emit'] = self.write
|
||||||
|
|
||||||
__stdout = sys.stdout
|
__stdout = sys.stdout
|
||||||
|
Loading…
x
Reference in New Issue
Block a user