File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+
1118func 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
Original file line number Diff line number Diff 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+
2335func TestLatexpand (t * testing.T ) {
2436 input := map [string ]string {
2537 "main.tex" : `
You can’t perform that action at this time.
0 commit comments