scripts: Added <^> to punescape

Mainly for ^ for centering:

  "%{hi!%}^8s"        -> "  hi!   "

Note we can right bias with the nested modifier!

  "%{%{hi!%}^7s%}>8s" -> "   hi!  "

Also note this does _not_ support fill characters like python's format.
I'm not sure it's tractable with %-modifiers, python's format must get
up to some funky parsing to make this work ("%s<s"?).

I think this also fixed the behavior of left-aligning numbers? Seems we
weren't handling that before.
This commit is contained in:
Christopher Haster
2026-02-19 11:44:13 -06:00
parent d23edafcfe
commit d4a2ce4a4e
10 changed files with 90 additions and 50 deletions
+8 -4
View File
@@ -1718,10 +1718,10 @@ def punescape(s, attrs=None, start=None, end=None, submatch=None):
'|' 'u....'
'|' 'U........'
'|' '\((?P<field>[^)]*)\)'
'(?P<format>[+\- #0-9\.]*[siIdboxXfFeEgG])'
'(?P<format>[<^>+\- #0-9\.]*[siIdboxXfFeEgG])'
'|' '\{'
'|' '\}'
'(?P<subformat>[+\- #0-9\.]*[siIdboxXfFeEgG])' ')')
'(?P<subformat>[<^>+\- #0-9\.]*[siIdboxXfFeEgG])' ')')
def format(f, v):
if f[-1] in 'dboxX':
@@ -1737,10 +1737,14 @@ def punescape(s, attrs=None, start=None, end=None, submatch=None):
f = f.replace('i', 's').replace('I', 's')
if '+' in f and not v.startswith('-'):
v = '+'+v
f = f.replace('+', '').replace('-', '')
f = f.replace('+', '')
else:
f = ('<' if '-' in f else '>') + f.replace('-', '')
v = str(v)
if '-' in f:
f = '<' + f.replace('-', '')
elif not any(d in f for d in '<^>'):
f = '>' + f
# note we need Python's new format syntax for binary
return ('{:%s}' % f).format(v)