Skip to content

Commit 5d458c8

Browse files
committed
fix: "/%" early stop
1 parent 30b3371 commit 5d458c8

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

internal/libs/tex/latexpand.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@ import (
88
"paperdebugger/internal/libs/shared"
99
)
1010

11+
// commentRegex matches a LaTeX comment: an unescaped % and everything after it
12+
// until end of line. The leading group captures either start-of-line or a
13+
// non-backslash character so that \% (an escaped percent) is preserved. Pairs
14+
// of backslashes (\\) before % are treated as a line-break followed by a real
15+
// comment, matching LaTeX semantics.
16+
var commentRegex = regexp.MustCompile(`(^|[^\\])((?:\\\\)*)%.*$`)
17+
1118
func removeComments(text string) string {
1219
// Split into lines, trim each line and filter empty ones
1320
lines := strings.Split(text, "\n")
1421
var result []string
1522
for _, line := range lines {
1623
trimmed := strings.TrimSpace(line)
17-
commentRegex := regexp.MustCompile(`%.*$`)
18-
cleaned := commentRegex.ReplaceAllString(trimmed, "")
24+
cleaned := commentRegex.ReplaceAllString(trimmed, "$1$2")
1925
cleaned = strings.TrimSpace(cleaned)
2026
if len(cleaned) == 0 {
2127
continue

internal/libs/tex/latexpand_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ Hello, world!
2020
\end{document}`, removeComments(input))
2121
}
2222

23+
func TestRemoveCommentsPreservesEscapedPercent(t *testing.T) {
24+
const input = `accuracy improved by 12\% over baseline % TODO: recheck`
25+
assert.Equal(t, `accuracy improved by 12\% over baseline`, removeComments(input))
26+
}
27+
28+
func TestRemoveCommentsDoubleBackslashBeforePercent(t *testing.T) {
29+
const input = `line one \\% this is a real comment after a line break
30+
next line`
31+
assert.Equal(t, `line one \\
32+
next line`, removeComments(input))
33+
}
34+
2335
func TestLatexpand(t *testing.T) {
2436
input := map[string]string{
2537
"main.tex": `

0 commit comments

Comments
 (0)