diff --git a/py/apigen/htmlgen.py b/py/apigen/htmlgen.py index 94f381650..c9e09577c 100644 --- a/py/apigen/htmlgen.py +++ b/py/apigen/htmlgen.py @@ -153,6 +153,12 @@ def get_obj(dsa, pkg, dotted_name): full_dotted_name)) return ret +def get_rel_sourcepath(projpath, filename, default=None): + relpath = py.path.local(filename).relto(projpath) + if not relpath: + return default + return relpath + # the PageBuilder classes take care of producing the docs (using the stuff # above) class AbstractPageBuilder(object): @@ -339,7 +345,8 @@ class ApiPageBuilder(AbstractPageBuilder): sep = get_linesep(callable_source) org = callable_source.split(sep) colored = [enumerate_and_color(org, firstlineno, enc)] - text = 'source: %s' % (sourcefile,) + relpath = get_rel_sourcepath(self.projroot, sourcefile, sourcefile) + text = 'source: %s' % (relpath,) if is_in_pkg: href = self.linker.get_lazyhref(sourcefile) @@ -606,8 +613,9 @@ class ApiPageBuilder(AbstractPageBuilder): href = self.linker.get_lazyhref(id) self.write_page('call site %s for %s' % (index, dotted_name), reltargetpath, tbtag, nav) - return H.CallStackLink(call_site[0].filename, call_site[0].lineno + 1, - href) + sourcefile = call_site[0].filename + sourcepath = get_rel_sourcepath(self.projpath, sourcefile, sourcefile) + return H.CallStackLink(sourcepath, call_site[0].lineno + 1, href) _reg_source = py.std.re.compile(r'([^>]*)<(.*)>') def gen_traceback(self, dotted_name, call_site): @@ -629,7 +637,9 @@ class ApiPageBuilder(AbstractPageBuilder): l = ' %s' % (sline,) mangled.append(l) if sourcefile: - linktext = '%s - line %s' % (sourcefile, frame.lineno + 1) + relpath = get_rel_sourcepath(self.projpath, sourcefile, + sourcefile) + linktext = '%s - line %s' % (relpath, frame.lineno + 1) # skip py.code.Source objects and source files outside of the # package is_code_source = self._reg_source.match(sourcefile) diff --git a/py/apigen/testing/test_apigen_example.py b/py/apigen/testing/test_apigen_example.py index 0835231cc..6a02c39c8 100644 --- a/py/apigen/testing/test_apigen_example.py +++ b/py/apigen/testing/test_apigen_example.py @@ -144,8 +144,10 @@ class TestApiPageBuilder(AbstractBuilderTest): assert pos5 > pos4 and pos5 > pos3 pos6 = html.find('<None>', pos5) assert pos6 > pos5 - pos7 = html.find('source: %s' % (self.fs_root.join('pkg/func.py'),), - pos6) + sourcefile = self.fs_root.join('pkg/func.py') + pos7 = html.find('source: %s' % (get_rel_sourcepath(apb.projpath, + sourcefile),), + pos6) assert pos7 > pos6 _checkhtmlsnippet(html) diff --git a/py/apigen/testing/test_htmlgen.py b/py/apigen/testing/test_htmlgen.py index 46b3b0bd2..9790afea8 100644 --- a/py/apigen/testing/test_htmlgen.py +++ b/py/apigen/testing/test_htmlgen.py @@ -20,8 +20,7 @@ def assert_eq_string(string1, string2): start, end, string2[start:end], string1, string2 )) - - + def test_create_namespace_tree(): tree = htmlgen.create_namespace_tree(['foo.bar.baz']) assert tree == {'': ['foo'], @@ -127,3 +126,12 @@ def test_show_property(): assert not htmlgen.show_property('__name__') assert not htmlgen.show_property('__class__') +def test_get_rel_sourcepath(): + projpath = py.path.local('/proj') + assert (htmlgen.get_rel_sourcepath(projpath, py.path.local('/proj/foo')) == + 'foo') + assert (htmlgen.get_rel_sourcepath(projpath, py.path.local('/foo')) is + None) + assert (htmlgen.get_rel_sourcepath(projpath, py.path.local('')) is + None) +