Anonymous avatar Anonymous committed 28551fb

Add proper tests, some documentation and use double quotes for HTML attributes in local_replace.py.

Comments (0)

Files changed (1)

extensions/local_replace.py

 import re
 
 substitutions = [ (re.compile(r'\#(?P<ws>\s*)(?P<id>\d+)'),
-                   r"<a href='issue\g<id>'>#\g<ws>\g<id></a>" ),
+                   r'<a href="issue\g<id>">#\g<ws>\g<id></a>'),
                   (re.compile(r'\brevision(?P<ws>\s*)(?P<revision>\d+)'),
-                   r"<a href='http://svn.python.org/view?rev=\g<revision>&view=rev'>revision\g<ws>\g<revision></a>"),
+                   r'<a href="http://svn.python.org/view?rev=\g<revision>&view=rev">revision\g<ws>\g<revision></a>'),
                   (re.compile(r'\brev(?P<ws>\s*)(?P<revision>\d+)'),
-                   r"<a href='http://svn.python.org/view?rev=\g<revision>&view=rev'>rev\g<ws>\g<revision></a>"),
+                   r'<a href="http://svn.python.org/view?rev=\g<revision>&view=rev">rev\g<ws>\g<revision></a>'),
                   (re.compile(r'\b(?P<revstr>r|r\s+)(?P<revision>\d+)'),
-                   r"<a href='http://svn.python.org/view?rev=\g<revision>&view=rev'>\g<revstr>\g<revision></a>"),
+                   r'<a href="http://svn.python.org/view?rev=\g<revision>&view=rev">\g<revstr>\g<revision></a>'),
                   (re.compile(r'\b(?P<path>(?:Demo|Doc|Grammar|Include|Lib|Mac|Misc|Modules|Parser|PC|PCbuild|Python|RISCOS|Tools|Objects)/[-.a-zA-Z0-9_/]+[a-zA-Z0-9]/?)'),
                    r'<a href="http://svn.python.org/view/python/trunk/\g<path>">\g<path></a>'),
                    ]
 
 def localReplace(message):
-
+    """
+    Turn the following strings in HTML links:
+    #1234, # 1234  ->  <a href="issue1234">#1234</a>
+    r12345, r 12345, rev12345, rev 12345, revision12345, revision 12345
+        ->  <a href="http://svn.python.org/view?rev=12345&view=rev">r12345</a>
+    Lib/somefile.py, Modules/somemodule.c, Doc/somedocfile.rst, ...
+        ->  <a href="http://svn.python.org/view/python/trunk/Dir/filename.ext">Dir/filename.ext</a>
+    """
     for cre, replacement in substitutions:
         message = cre.sub(replacement, message)
 
     instance.registerUtil('clean_count', clean_count)
 
 if "__main__" == __name__:
-    print " revision 222", localReplace(" revision 222")
-    print " wordthatendswithr 222", localReplace(" wordthatendswithr 222")
-    print " r222", localReplace(" r222")
-    print " r 222", localReplace(" r 222")
-    print " #555", localReplace(" #555")
+    import unittest
 
+    class TestLocalReplace(unittest.TestCase):
+        def test_localReplace(self):
+            test_strings = [
+                ## r12345, r 12345, rev12345, rev 12345, revision12345, revision 12345
+                (' r222 ',
+                 ' <a href="http://svn.python.org/view?rev=222&view=rev">r222</a> '),
+                (' r 222 ',
+                 ' <a href="http://svn.python.org/view?rev=222&view=rev">r 222</a> '),
+                (' rev222 ',
+                 ' <a href="http://svn.python.org/view?rev=222&view=rev">rev222</a> '),
+                (' rev  222 ',
+                 ' <a href="http://svn.python.org/view?rev=222&view=rev">rev  222</a> '),
+                (' revision222 ',
+                 ' <a href="http://svn.python.org/view?rev=222&view=rev">revision222</a> '),
+                (' revision 222 ',
+                 ' <a href="http://svn.python.org/view?rev=222&view=rev">revision 222</a> '),
+                ('wordthatendswithr 222',
+                 'wordthatendswithr 222'),
+                #('http://svn.python.org/view/python/tags/r265 ',
+                 #'http://svn.python.org/view/python/tags/r265 '),
+
+                ##  #1234, # 1234
+                ## the lowest issue id is #1000, the highest is #1779871
+                #(' #1 ', ' #1 '),
+                #(' #10 ', ' #10 '),
+                #(' #999 ', ' #999 '),
+                #(' # 999 ', ' # 999 '),
+                (' #5555 ', ' <a href="issue5555">#5555</a> '),
+                (' # 5555 ', ' <a href="issue5555"># 5555</a> '),
+                (' #555555 ', ' <a href="issue555555">#555555</a> '),
+                (' # 555555 ', ' <a href="issue555555"># 555555</a> '),
+                #(' #2000000 ', ' #2000000 '),
+                #(' # 2000000 ', ' # 2000000 '),
+
+                ## Lib/somefile.py, Modules/somemodule.c, Doc/somedocfile.rst, ...
+                ('Lib/cgi.py',
+                 '<a href="http://svn.python.org/view/python/trunk/Lib/cgi.py">Lib/cgi.py</a>'),
+                #('/Lib/cgi.py',
+                 #'<a href="http://svn.python.org/view/python/trunk/Lib/cgi.py">/Lib/cgi.py</a>'),
+                ('the bug is in Lib/cgi.py.',
+                 'the bug is in <a href="http://svn.python.org/view/python/trunk/Lib/cgi.py">Lib/cgi.py</a>.'),
+                #('the bug is in /Lib/cgi.py.',
+                 #'the bug is in <a href="http://svn.python.org/view/python/trunk/Lib/cgi.py">/Lib/cgi.py</a>.'),
+                #('http://svn.python.org/view/python/tags/r265/Lib/cgi.py',
+                 #'http://svn.python.org/view/python/tags/r265/Lib/cgi.py'),
+                #('http://svn.python.org/view/python/tags/r265/Lib/cgi.py?view=markup',
+                 #'http://svn.python.org/view/python/tags/r265/Lib/cgi.py?view=markup'),
+            ]
+            for text, expected_result in test_strings:
+                self.assertEqual(expected_result, localReplace(text))
+
+    # run the tests when executed directly
+    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.