Implement %d filter

This commit is contained in:
RunasSudo 2025-05-14 18:28:29 +10:00
parent 4e42eeb9fd
commit 259f6f3c29
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 12 additions and 0 deletions

View File

@ -45,6 +45,11 @@ class Emitter:
raise NotImplementedError() raise NotImplementedError()
def output_variable_formatted(self, format_string: str, variable: str) -> None:
"""Emit code to output a variable, according to the format string"""
raise NotImplementedError()
def output_variable_urlencoded(self, variable: str) -> None: def output_variable_urlencoded(self, variable: str) -> None:
"""Emit code to output a variable, as encoded URL component""" """Emit code to output a variable, as encoded URL component"""

View File

@ -35,6 +35,9 @@ class CgitEmitter(Emitter):
def output_variable_as_text(self, variable: str) -> None: def output_variable_as_text(self, variable: str) -> None:
self.emit(f'html_txt({variable});') self.emit(f'html_txt({variable});')
def output_variable_formatted(self, format_string: str, variable: str) -> None:
self.emit(f'htmlf({self.escape_cstr(format_string)}, {variable});')
def output_variable_urlencoded(self, variable: str) -> None: def output_variable_urlencoded(self, variable: str) -> None:
self.emit(f'html_url_arg({variable});') self.emit(f'html_url_arg({variable});')

View File

@ -133,6 +133,10 @@ class Parser:
# Output as URL component # Output as URL component
variable = variable[:-len('urlencode')].rstrip()[:-1].rstrip() variable = variable[:-len('urlencode')].rstrip()[:-1].rstrip()
self.emitter.output_variable_urlencoded(variable) self.emitter.output_variable_urlencoded(variable)
elif variable.endswith('%d') and variable[:-len('%d')].rstrip().endswith('|'):
# Output as %d
variable = variable[:-len('%d')].rstrip()[:-1].rstrip()
self.emitter.output_variable_formatted('%d', variable)
else: else:
# No filter - output as text # No filter - output as text
self.emitter.output_variable_as_text(variable) self.emitter.output_variable_as_text(variable)