Repository: google-research/arxiv-latex-cleaner

Issue:
[BUG] Inline comment containing a url is not removed
Hi 👋 
When there is an inline comment followed by a \url{} command, the inline comment is not removed as expected. This issue seems to have been introduced while addressing #82.


## Example
### Input:
```latex
Hello! % (\url{topsecret.com})
```
### Output:
```latex
Hello! %
\url{topsecret.com})
```
### Expected Output:
```latex
Hello! %
```

Discussion:
@jponttuset: Thanks @merajhashemi!

Solution patch:
diff --git a/arxiv_latex_cleaner/arxiv_latex_cleaner.py b/arxiv_latex_cleaner/arxiv_latex_cleaner.py
index aa402aa..4eb341d 100644
--- a/arxiv_latex_cleaner/arxiv_latex_cleaner.py
+++ b/arxiv_latex_cleaner/arxiv_latex_cleaner.py
@@ -221,14 +221,14 @@ def _remove_comments_inline(text):
   url_pattern = r'\\url\{(?>[^{}]|(?R))*\}'
 
   def remove_comments(segment):
-    """Remove comments from a segment of text."""
+    """Check if a segment of text contains a comment and remove it."""
     if segment.lstrip().startswith('%'):
-      return ''
+      return '', True
     match = regex.search(r'(?<!\\)%', segment)
     if match:
-      return segment[: match.end()] + '\n'
+      return segment[: match.end()] + '\n', True
     else:
-      return segment
+      return segment, False
 
   # split the text into segments based on \url{} tags
   segments = regex.split(f'({url_pattern})', text)
@@ -236,7 +236,11 @@ def _remove_comments_inline(text):
   for i in range(len(segments)):
     # only process segments that are not part of a \url{} tag
     if not regex.match(url_pattern, segments[i]):
-      segments[i] = remove_comments(segments[i])
+      segments[i], match = remove_comments(segments[i])
+      if match:
+        # remove all segments after the first inline comment
+        segments = segments[: i + 1]
+        break
 
   final_text = ''.join(segments)
   return (


Test patch:
diff --git a/arxiv_latex_cleaner/tests/arxiv_latex_cleaner_test.py b/arxiv_latex_cleaner/tests/arxiv_latex_cleaner_test.py
index c9fba92..8540e75 100644
--- a/arxiv_latex_cleaner/tests/arxiv_latex_cleaner_test.py
+++ b/arxiv_latex_cleaner/tests/arxiv_latex_cleaner_test.py
@@ -298,6 +298,11 @@ class UnitTests(parameterized.TestCase):
           'line_in': '\\url{https://www.example.com/hello%20world}\n',
           'true_output': '\\url{https://www.example.com/hello%20world}\n',
       },
+      {
+          'testcase_name': 'comment_with_url',
+          'line_in': 'Foo %\\url{https://www.example.com/hello%20world}\n',
+          'true_output': 'Foo %\n',
+      },
   )
   def test_remove_comments_inline(self, line_in, true_output):
     self.assertEqual(