44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
# Copyright (c) PARRRATE T&V 2021. All rights reserved.
|
||
|
||
from io import BytesIO
|
||
|
||
from bu4.parsing.codes import *
|
||
|
||
__all__ = ('transply',)
|
||
|
||
|
||
def transply(source: str) -> bytes:
|
||
transplied = BytesIO()
|
||
for c in source:
|
||
if c.isspace():
|
||
pass
|
||
elif c in '/':
|
||
transplied.write(bytes([CODE_CALL]))
|
||
elif c in '{‹':
|
||
transplied.write(bytes([CODE_CALL, CODE_QOPN]))
|
||
elif c in '(':
|
||
transplied.write(bytes([CODE_MAKE]))
|
||
elif c in '|':
|
||
transplied.write(bytes([CODE_QCLS, CODE_MAKE]))
|
||
elif c in '})':
|
||
transplied.write(bytes([CODE_NULL]))
|
||
elif c in '#':
|
||
transplied.write(bytes([CODE_SKIP]))
|
||
elif c in '?':
|
||
transplied.write(bytes([CODE_QOPN, CODE_NULL, CODE_QCLS]))
|
||
elif c in ']»':
|
||
transplied.write(bytes([CODE_NULL, CODE_QCLS]))
|
||
elif c in '[':
|
||
transplied.write(bytes([CODE_QOPN, CODE_NAME]))
|
||
elif c in '"':
|
||
transplied.write(bytes([CODE_QUOT]))
|
||
elif c in '<':
|
||
transplied.write(bytes([CODE_QOPN]))
|
||
elif c in '>›':
|
||
transplied.write(bytes([CODE_QCLS]))
|
||
elif c in '«':
|
||
transplied.write(bytes([CODE_QOPN, CODE_XCPT]))
|
||
else:
|
||
transplied.write(c.encode())
|
||
return transplied.getvalue()
|