pypy / pypy / doc / tool /

import py
import pypy
pypydir = py.path.local(pypy.__file__).dirpath()
distdir = pypydir.dirpath() 
issue_url = '' 
bitbucket_url = ''

import urllib2, posixpath

def makeref(docdir):
    reffile = docdir.join('_ref.txt') 

    linkrex ='`(\S+)`_')

    name2target = {}
    def addlink(linkname, linktarget): 
        assert linkname and linkname != '/'
        if linktarget in name2target: 
            if linkname in name2target[linktarget]: 
        name2target.setdefault(linktarget, []).append(linkname)

    for textfile in sorted(docdir.listdir()):  # for subdirs, see below
        if textfile.ext != '.rst':
        content =
        found = False
        for linkname in linkrex.findall(content): 
            if '/' in linkname:
                found = True
                assert distdir.join(linkname).check(), "link %s in %s is dead" % (linkname, textfile)
                url = bitbucket_url + linkname
                if not linkname.endswith("/") and distdir.join(linkname).check(dir=1):
                    url += "/"
                addlink(linkname, url)
            elif linkname.startswith('issue'): 
                found = True
                addlink(linkname, issue_url+linkname)
        if found:
            assert ".. include:: _ref.txt" in content, "you need to include _ref.txt in %s" % (textfile, )

    items = name2target.items() 

    lines = []
    for linktarget, linknamelist in items: 
        for linkname in linknamelist[:-1]: 
            lines.append(".. _`%s`:" % linkname)
        lines.append(".. _`%s`: %s" %(linknamelist[-1], linktarget))

    print "wrote %d references to %r" %(len(lines), reffile)
    #print "last ten lines"
    #for x in lines[-10:]: print x

# We need to build a new _ref.txt for each directory that uses it, because
# they differ in the number of "../" that they need in the link targets...
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
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.