From 3fc1bc2c2cd5bbb224c66514e683413f5132afda Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Tue, 8 Jul 2025 12:34:10 +0100 Subject: [PATCH] GPM Templates: allow comments to start in any column, not just the first. --- manual/template_lang.rst | 2 +- src/calibre/gui2/dialogs/template_dialog.py | 4 +++- src/calibre/utils/formatter.py | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/manual/template_lang.rst b/manual/template_lang.rst index 5a5302a492..5417cfc839 100644 --- a/manual/template_lang.rst +++ b/manual/template_lang.rst @@ -293,7 +293,7 @@ Notes: * In a logical context, any non-empty value is ``True`` * In a logical context, the empty value is ``False`` * Strings and numbers can be used interchangeably. For example, ``10`` and ``'10'`` are the same thing. -* Comments are lines starting with a '#' character. Comments beginning later in a line are not supported. +* Comments are lines starting with blanks or tabs then a '#' character. **Operator precedence** diff --git a/src/calibre/gui2/dialogs/template_dialog.py b/src/calibre/gui2/dialogs/template_dialog.py index 27dee21d11..4e3f0a37b6 100644 --- a/src/calibre/gui2/dialogs/template_dialog.py +++ b/src/calibre/gui2/dialogs/template_dialog.py @@ -348,7 +348,9 @@ class TemplateHighlighter(QSyntaxHighlighter): if not text: pass - elif text[0] == '#': + elif re.match(r'[ \t]*#', text): + # Line with only a comment possibly preceded with spaces or tabs + # This works in both GPM and python self.setFormat(0, textLength, self.Formats['comment']) return diff --git a/src/calibre/utils/formatter.py b/src/calibre/utils/formatter.py index 2f911aef38..042f2369e9 100644 --- a/src/calibre/utils/formatter.py +++ b/src/calibre/utils/formatter.py @@ -1715,7 +1715,7 @@ class TemplateFormatter(string.Formatter): (r'\w+', lambda x,t: (_Parser.LEX_ID, t)), (r'".*?((?