Skip to content

Add source link to classes and method #94

Closed
@shimizukawa

Description

@shimizukawa

I start hacking autodoc in Sphinx to add a //source// link after each class/method/function name. This link will link to web page showing the code of the current module, like the browse view in Trac.

To attemp to do this I hack the //desc_directive()// function like this (from line 420:

#!python

    for i, sig in enumerate(signatures):
        # add a signature node for each signature in the current unit
        # and add a reference target for it
        sig = sig.strip()
        signode = addnodes.desc_signature(sig, '')
        signode['first'] = False

        # MY CODE START HERE
        # Create a reference
        newnode = nodes.reference('', '')
        innernode = nodes.emphasis('Source', 'Source')
        newnode['refuri'] = 'https://www.example.com/trac/project/browser/trunk/py/' + options['module'].replace('.', '/') + '.py'
        newnode.append(innernode)
        signode.append(newnode)
        # MY CODE FINISH HERE

        node.append(signode)

This is really preliminary code with everything hard coded. In the final version the URL will be taken from the conf.py file and a directive option (like :source:) will be added to the automodule directive.

Side note: I saw your proposal //Easily extendable autodoc// (issue #3), it looks a very very good idea. Any idea of a timeframe for when you will add this ? Hacking //desc_directive()// is not a good way to go.

In my attemp to do this I hit 2 problems:

The first one is the //Source// link was in front of the method name instead of after (HTML output):

#!html

<dt id="ui.cli.cli.CliController">
<!--[ui.cli.cli.CliController]-->
class <a class="reference external" href="www.example.com/trac/project/browser/trunk/py/ui/cli/cli.py"><em>Source</em></a>
<tt class="descname">CliController</tt>
<big>(</big><em>subject</em><big>)</big>
<a class="headerlink" href="#ui.cli.cli.CliController" title="Permalink to this definition">¶</a>
</dt>

I have no idea why because the nodes I add are childrens of the signode ?

My second problem is that I wish to add the line number to the URL of the class/method/function so we can jump directly to the good place in the code. The URL will be of this form:
www.example.com/trac/project/browser/trunk/py/ui/cli/cli.py#L234

There's a //lineno// variable in the //desc_directive()// function, but the number it give don't correspond to the real line number. But after I dig a little more into the Sphinx/docutils code, I was with the impression that //lineno// is supposed to be the real line number ?

Is there's a way to get the real line number (line number in the script file) in the //desc_directive()// function ?


Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions