|
| 1 | +""" |
| 2 | +Replaces the edit_url for documentation included in other submodules. |
| 3 | +
|
| 4 | +For example, the file robot_parameters.md [1] is included using the submodule located at repos/stretch_body |
| 5 | +
|
| 6 | +When edit_uri is defined in the mkdocs.yml, the edit link for that page is |
| 7 | +https://github.com/hello-robot/hello-robot.github.io/blob/0.3/repos/stretch_body/docs/robot_parameters.md |
| 8 | +which does not link properly. |
| 9 | +
|
| 10 | +The code contained in this module is inspired by the submodule-edit-uri plugin [2] |
| 11 | +and uses the mkdocs hooks [3] functionality. |
| 12 | +
|
| 13 | +It reads the submodules from `.gitmodules` using GitPython during the configuration step, |
| 14 | +and then substitutes the proper url in the full output. |
| 15 | +
|
| 16 | +The resulting link is now correct, linking to the full github blob url [1] |
| 17 | +
|
| 18 | +[1] https://github.com/hello-robot/stretch_body/blob/master/docs/robot_parameters.md |
| 19 | +[2] https://github.com/sondregronas/mkdocs-submodule-edit-uri |
| 20 | +[3] https://www.mkdocs.org/user-guide/configuration/#hooks |
| 21 | +
|
| 22 | +
|
| 23 | +""" |
| 24 | +from git import Repo |
| 25 | + |
| 26 | + |
| 27 | +def urljoin(*a): |
| 28 | + s = '' |
| 29 | + for sub in a: |
| 30 | + if s and not s.endswith('/'): |
| 31 | + s += '/' |
| 32 | + s += sub |
| 33 | + return s |
| 34 | + |
| 35 | + |
| 36 | +def on_config(config): |
| 37 | + submod_d = {} |
| 38 | + config['submodules'] = submod_d |
| 39 | + repo = Repo('.') |
| 40 | + for submod in repo.submodules: |
| 41 | + url = submod.url |
| 42 | + if url.endswith('.git'): |
| 43 | + url = url[:-4] |
| 44 | + submod_d[submod.name] = { |
| 45 | + 'path': submod.path, |
| 46 | + 'url': url, |
| 47 | + 'branch': submod.branch.name, |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +def on_post_page(output, page, config): |
| 52 | + # Assume that the edit uri is (blob|edit)/(branch_name)/docs/ |
| 53 | + edit_pieces = config['edit_uri'].split('/') |
| 54 | + assert len(edit_pieces) == 4, config['edit_uri'] |
| 55 | + verb = edit_pieces[0] |
| 56 | + branch = edit_pieces[1] |
| 57 | + |
| 58 | + base = urljoin(config['repo_url'], verb, branch) |
| 59 | + |
| 60 | + for name, submod in config['submodules'].items(): |
| 61 | + old_path = urljoin(base, submod['path']) |
| 62 | + new_path = urljoin(submod['url'], verb, submod['branch']) |
| 63 | + output = output.replace(old_path, new_path) |
| 64 | + return output |
0 commit comments