[svn r37616] All code snippets are colored now, also properly fixed hiding of _* names

in the navigation.

--HG--
branch : trunk
This commit is contained in:
guido 2007-01-30 17:29:33 +01:00
parent d435873e03
commit e905f71e72
3 changed files with 38 additions and 31 deletions

View File

@ -332,6 +332,20 @@ class SourcePageBuilder(AbstractPageBuilder):
reltargetpath = outputpath.relto(self.base).replace(os.path.sep, '/') reltargetpath = outputpath.relto(self.base).replace(os.path.sep, '/')
self.write_page(title, reltargetpath, project, tag, nav) self.write_page(title, reltargetpath, project, tag, nav)
def enumerate_and_color(codelines, firstlineno, enc):
tokenizer = source_color.Tokenizer(source_color.PythonSchema)
colored = []
for i, line in enumerate(codelines):
try:
colored.append(H.span('%04s: ' % (i + firstlineno + 1)))
colored.append(source_html.prepare_line([line], tokenizer, enc))
colored.append('\n')
except py.error.ENOENT:
# error reading source code, giving up
colored = org
break
return colored
class ApiPageBuilder(AbstractPageBuilder): class ApiPageBuilder(AbstractPageBuilder):
""" builds the html for an api docs page """ """ builds the html for an api docs page """
def __init__(self, base, linker, dsa, projroot, namespace_tree): def __init__(self, base, linker, dsa, projroot, namespace_tree):
@ -359,19 +373,17 @@ class ApiPageBuilder(AbstractPageBuilder):
is_in_pkg = self.is_in_pkg(sourcefile) is_in_pkg = self.is_in_pkg(sourcefile)
if sourcefile and callable_source: if sourcefile and callable_source:
enc = source_html.get_module_encoding(sourcefile) enc = source_html.get_module_encoding(sourcefile)
tokenizer = source_color.Tokenizer(source_color.PythonSchema)
firstlineno = func.func_code.co_firstlineno
org = callable_source.split('\n')
colored = enumerate_and_color(org, firstlineno, enc)
if is_in_pkg: if is_in_pkg:
csource = H.div(H.br(), slink = H.a('source: %s' % (sourcefile,),
H.a('source: %s' % (sourcefile,), href=self.linker.get_lazyhref(sourcefile))
href=self.linker.get_lazyhref(sourcefile)), else:
H.br(), slink = H.em('source: %s' % (sourcefile,))
H.SourceDef(H.pre(unicode(callable_source, csource = H.div(H.br(), slink, H.br(),
enc)))) H.SourceDef(H.div(class_='code', *colored)))
elif not is_in_pkg:
csource = H.div(H.br(),
H.em('source: %s' % (sourcefile,)),
H.br(),
H.SourceDef(H.pre(unicode(callable_source,
enc))))
else: else:
csource = H.SourceDef('could not get source file') csource = H.SourceDef('could not get source file')
@ -465,8 +477,6 @@ class ApiPageBuilder(AbstractPageBuilder):
H.Docstring(docstring or '*no docstring available*') H.Docstring(docstring or '*no docstring available*')
) )
for dotted_name in sorted(item_dotted_names): for dotted_name in sorted(item_dotted_names):
if dotted_name.startswith('_'):
continue
itemname = dotted_name.split('.')[-1] itemname = dotted_name.split('.')[-1]
if is_private(itemname): if is_private(itemname):
continue continue
@ -583,9 +593,11 @@ class ApiPageBuilder(AbstractPageBuilder):
for dn in sorted(siblings): for dn in sorted(siblings):
selected = dn == '.'.join(path) selected = dn == '.'.join(path)
sibpath = dn.split('.') sibpath = dn.split('.')
navitems.append(build_navitem_html(self.linker, sibpath[-1], sibname = sibpath[-1]
dn, depth, if is_private(sibname):
selected)) continue
navitems.append(build_navitem_html(self.linker, sibname,
dn, depth, selected))
if selected: if selected:
lastlevel = dn.count('.') == dotted_name.count('.') lastlevel = dn.count('.') == dotted_name.count('.')
if not lastlevel: if not lastlevel:
@ -723,17 +735,7 @@ class ApiPageBuilder(AbstractPageBuilder):
else: else:
enc = 'latin-1' enc = 'latin-1'
sourcelink = H.div(linktext) sourcelink = H.div(linktext)
colored = [] colored = enumerate_and_color(mangled, frame.firstlineno, enc)
for i, line in enumerate(mangled):
try:
colored.append(H.span('%02s: ' %
(i + frame.firstlineno + 1)))
colored.append(source_html.prepare_line(
[line], tokenizer, enc))
colored.append('\n')
except py.error.ENOENT:
colored = mangled
break
else: else:
sourcelink = H.div('source unknown') sourcelink = H.div('source unknown')
tbdiv.append(sourcelink) tbdiv.append(sourcelink)

View File

@ -88,6 +88,8 @@ a:hover {
padding-left: 1em; padding-left: 1em;
white-space: pre; white-space: pre;
font-family: monospace, Monaco; font-family: monospace, Monaco;
margin-top: 0.5em;
margin-bottom: 0.5em;
} }
.comment { .comment {

View File

@ -63,6 +63,7 @@ def setup_fs_project():
'main.SomeSubClass': ('./somesubclass.py', 'SomeSubClass'), 'main.SomeSubClass': ('./somesubclass.py', 'SomeSubClass'),
'main.SomeSubClass': ('./somesubclass.py', 'SomeSubClass'), 'main.SomeSubClass': ('./somesubclass.py', 'SomeSubClass'),
'other': ('./somenamespace.py', '*'), 'other': ('./somenamespace.py', '*'),
'_test': ('./somenamespace.py', '*'),
}) })
""")) """))
return temp, 'pkg' return temp, 'pkg'
@ -114,7 +115,8 @@ class AbstractBuilderTest(object):
'main.SomeSubClass', 'main.SomeSubClass',
'main.SomeInstance', 'main.SomeInstance',
'other.foo', 'other.foo',
'other.bar']) 'other.bar',
'_test'])
self.namespace_tree = namespace_tree self.namespace_tree = namespace_tree
self.apb = ApiPageBuilder(base, linker, self.dsa, self.apb = ApiPageBuilder(base, linker, self.dsa,
self.fs_root.join(self.pkg_name), self.fs_root.join(self.pkg_name),
@ -195,6 +197,7 @@ class TestApiPageBuilder(AbstractBuilderTest):
def test_build_class_pages_nav_links(self): def test_build_class_pages_nav_links(self):
data = self.apb.prepare_class_pages(['main.SomeSubClass', data = self.apb.prepare_class_pages(['main.SomeSubClass',
'main.SomeClass']) 'main.SomeClass'])
self.apb.prepare_namespace_pages()
# fake some stuff that would be built from other methods # fake some stuff that would be built from other methods
self.linker.set_link('', 'api/index.html') self.linker.set_link('', 'api/index.html')
self.linker.set_link('main', 'api/main.html') self.linker.set_link('main', 'api/main.html')
@ -211,8 +214,8 @@ class TestApiPageBuilder(AbstractBuilderTest):
'href="main.SomeClass.html">SomeClass', 'href="main.SomeClass.html">SomeClass',
'href="main.SomeSubClass.html">SomeSubClass', 'href="main.SomeSubClass.html">SomeSubClass',
]) ])
assert not 'href="main.sub.func.html"' in html assert 'href="main.sub.func.html"' not in html
py.test.skip('WOP from here') assert 'href="_test' not in html
assert 'href="main.sub.html">sub' in html assert 'href="main.sub.html">sub' in html
_checkhtml(html) _checkhtml(html)