diff --git a/htmlcc/emitter/__init__.py b/htmlcc/emitter/__init__.py index 7fb1e4d..6db4364 100644 --- a/htmlcc/emitter/__init__.py +++ b/htmlcc/emitter/__init__.py @@ -45,6 +45,11 @@ class Emitter: 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: """Emit code to output a variable, as encoded URL component""" diff --git a/htmlcc/emitter/cgit.py b/htmlcc/emitter/cgit.py index 84cd5ab..64fbaf3 100644 --- a/htmlcc/emitter/cgit.py +++ b/htmlcc/emitter/cgit.py @@ -35,6 +35,9 @@ class CgitEmitter(Emitter): def output_variable_as_text(self, variable: str) -> None: 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: self.emit(f'html_url_arg({variable});') diff --git a/htmlcc/parser.py b/htmlcc/parser.py index b0b80ea..2dd9ad8 100644 --- a/htmlcc/parser.py +++ b/htmlcc/parser.py @@ -133,6 +133,10 @@ class Parser: # Output as URL component variable = variable[:-len('urlencode')].rstrip()[:-1].rstrip() 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: # No filter - output as text self.emitter.output_variable_as_text(variable)