[py-svn] r35307 - in py/dist/py/apigen/source: . testing

guido at codespeak.net guido at codespeak.net
Tue Dec 5 16:59:29 CET 2006


Author: guido
Date: Tue Dec  5 16:59:27 2006
New Revision: 35307

Modified:
   py/dist/py/apigen/source/color.py
   py/dist/py/apigen/source/html.py
   py/dist/py/apigen/source/testing/test_color.py
   py/dist/py/apigen/source/testing/test_html.py
Log:
Integrated syntax coloring. Note that, after some discussion with cfbolz, I
may replace the tokenizer with the one in the Python core.


Modified: py/dist/py/apigen/source/color.py
==============================================================================
--- py/dist/py/apigen/source/color.py	(original)
+++ py/dist/py/apigen/source/color.py	Tue Dec  5 16:59:27 2006
@@ -11,7 +11,7 @@
     keyword = ['for', 'if', 'not', 'then', 'else', 'while', 'from', 'import',
                'try', 'except', 'finally', 'raise', 'print', 'exec', 'eval',
                'break', 'in', 'assert', 'None']
-    alt_keyword = ['def', 'class', 'return']
+    alt_keyword = ['def', 'class', 'return', 'pass', 'yield']
 
 class Token(object):
     data = None
@@ -52,8 +52,10 @@
         
         self._re_strings_full = []
         self._re_strings_multiline = []
+        self._re_strings_empty = []
         for d in schema.string + schema.multiline_string:
-            self._re_strings_full.append(re.compile('%s.*?%s' % (d, d)))
+            self._re_strings_full.append(re.compile('%s[^%s]+%s' % (d, d, d)))
+            self._re_strings_empty.append(re.compile('%s%s' % (d, d)))
         for d in schema.multiline_string:
             self._re_strings_multiline.append((re.compile('%s.*' % (d,), re.S),
                                                re.compile('.*?%s' % (d,))))
@@ -74,9 +76,10 @@
                 self._inside_multiline = False
                 yield Token(s, 'string')
         while data:
-            for f in [self._check_multiline_strings, self._check_full_strings,
-                      self._check_comments, self._check_number,
-                      self._check_space, self._check_word, self._check_rest]:
+            for f in [self._check_full_strings, self._check_multiline_strings,
+                      self._check_empty_strings, self._check_comments,
+                      self._check_number, self._check_space, self._check_word,
+                      self._check_rest]:
                 data, t = f(data)
                 if t:
                     yield t
@@ -108,6 +111,18 @@
                 break
         return data, token
 
+    def _check_empty_strings(self, data):
+        token = None
+        for r in self._re_strings_empty:
+            m = r.match(data)
+            if m:
+                s = m.group(0)
+                data = data[len(s):]
+                token = Token(s, type='string')
+                break
+        return data, token
+
+
     def _check_comments(self, data):
         # fortunately we don't have to deal with multi-line comments
         token = None

Modified: py/dist/py/apigen/source/html.py
==============================================================================
--- py/dist/py/apigen/source/html.py	(original)
+++ py/dist/py/apigen/source/html.py	Tue Dec  5 16:59:27 2006
@@ -82,6 +82,11 @@
                     padding: 0px;
                     border-width: 0px;
                 }
+
+                a {
+                    color: blue;
+                    text-decoration: none;
+                }
                 
                 .lineno {
                     text-align: right;
@@ -96,6 +101,23 @@
                     padding-left: 1em;
                     white-space: pre;
                 }
+                
+                .comment {
+                    color: purple;
+                }
+
+                .string {
+                    color: red;
+                }
+
+                .keyword {
+                    color: blue;
+                }
+
+                .alt_keyword {
+                    color: green;
+                }
+                
             """, type='text/css'),
         )
 
@@ -118,7 +140,6 @@
             if type(item) in [str, unicode]:
                 tokens = self.tokenizer.tokenize(item)
                 for t in tokens:
-                    print t.type
                     if t.type in ['keyword', 'alt_keyword', 'number',
                                   'string']:
                         ret.append(html.span(t.data, class_=t.type))
@@ -131,6 +152,8 @@
     def add_row(self, lineno, text):
         if text == ['']:
             text = [raw(' ')]
+        else:
+            text = self.prepare_line(text)
         self.tbody.append(html.tr(html.td(str(lineno), class_='lineno'),
                                   html.td(class_='code', *text)))
 

Modified: py/dist/py/apigen/source/testing/test_color.py
==============================================================================
--- py/dist/py/apigen/source/testing/test_color.py	(original)
+++ py/dist/py/apigen/source/testing/test_color.py	Tue Dec  5 16:59:27 2006
@@ -67,6 +67,8 @@
                        Token('\n', type='whitespace')]
         # tricky problem: the following line must not put the tokenizer in
         # 'multiline state'...
-        assert self.tokens('"""foo"""') == [Token('"""foo"""', type='string')]
-        assert self.tokens('bar') == [Token('bar', type='word')]
+        res = list(t.tokenize('"""foo"""'))
+        assert res == [Token('"""foo"""', type='string')]
+        res = list(t.tokenize('bar'))
+        assert res == [Token('bar', type='word')]
 

Modified: py/dist/py/apigen/source/testing/test_html.py
==============================================================================
--- py/dist/py/apigen/source/testing/test_html.py	(original)
+++ py/dist/py/apigen/source/testing/test_html.py	Tue Dec  5 16:59:27 2006
@@ -124,12 +124,20 @@
         tbody = doc.tbody
         assert len(tbody) == 4
         assert unicode(tbody[0][0]) == '<td class="lineno">1</td>'
-        assert unicode(tbody[0][1]) == ('<td class="code">&quot;&quot;&quot; '
+        assert unicode(tbody[0][1]) == ('<td class="code">'
+                                        '<span class="string">'
+                                        '&quot;&quot;&quot; '
                                         'this is a foo implementation '
-                                        '&quot;&quot;&quot;</td>')
+                                        '&quot;&quot;&quot;'
+                                        '</span></td>')
         assert unicode(tbody[1][1]) == '<td class="code">&#xa0;</td>'
-        assert unicode(tbody[2][1]) == ('<td class="code">class '
+        assert unicode(tbody[2][1]) == ('<td class="code">'
+                                        '<span class="alt_keyword">class'
+                                        '</span> '
                                         '<a name="Foo">Foo</a>:</td>')
+        assert unicode(tbody[3][1]) == ('<td class="code">    '
+                                        '<span class="alt_keyword">pass'
+                                        '</span></td>')
 
     def test_unicode(self):
         doc = HTMLDocument()


More information about the py-svn mailing list