From 790d716d570c93f7239844cc753233fa1cb0e820 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Sun, 13 Sep 2026 11:51:36 +0200 Subject: [PATCH] Fix ESC/POS notation parser: '#' only starts a comment as its own token Stripping from the first '#' anywhere in the line ate content like '#5 Fix thing' (an issue reference) entirely, since it starts with '#' with no preceding whitespace. Comments now only trigger on a standalone '#' token (i.e. whitespace on both sides), matching the documented examples. --- app.rb | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/app.rb b/app.rb index 5729316..29e075c 100644 --- a/app.rb +++ b/app.rb @@ -117,17 +117,23 @@ helpers do # Hello # literal text # LF # GS k 0x03 12345678 0x00 - # Each `#...` trailing comment is stripped, then each line's - # whitespace-separated tokens become either a known control mnemonic, a - # raw `0xNN` byte, or literal text (CP852-encoded) -- consecutive literal - # tokens are rejoined with a single space so plain prose still reads - # correctly, while whitespace around control/byte tokens is dropped since - # those are just for human readability, not printer output. + # A trailing comment is only a comment when "#" is its own + # whitespace-separated token -- not just stripped from the first "#" + # anywhere in the line, which would mangle literal text like "#5 Fix + # thing" (an issue reference, not a comment). Each remaining token + # becomes a known control mnemonic, a raw `0xNN` byte, or literal text + # (CP852-encoded) -- consecutive literal tokens are rejoined with a + # single space so plain prose still reads correctly, while whitespace + # around control/byte tokens is dropped since those are just for human + # readability, not printer output. def parse_escpos_notation(text) bytes = "".b text.each_line do |line| prev_was_text = false - line.sub(/#.*/, '').split.each do |token| + tokens = line.split + comment_at = tokens.index('#') + tokens = tokens[0...comment_at] if comment_at + tokens.each do |token| if ESCPOS_MNEMONICS.key?(token) bytes << ESCPOS_MNEMONICS[token] prev_was_text = false