Skip to content

Commit

Permalink
Fix 'edit on github' link for components which are submodules
Browse files Browse the repository at this point in the history
Show as 'view on github`, more user-friendly if not currently logged into github
Build file -> URL map and use that to ensure submodules (and sub-submodules) are handled correctly
  • Loading branch information
mikee47 committed Dec 8, 2021
1 parent 11c4160 commit ee4a9f9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
2 changes: 0 additions & 2 deletions docs/index.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# $2 -> Path
# $3 -> Source path relative to working root directory (SMINGDIR)
define GenIndex
:custom_pagename: $(patsubst ../%,%,$(CMP_$2_README))

$(if $(filter %.rst,$(CMP_$2_README)),
.. include:: $(call GetIncludePath,$(CMP_$2_README)),
.. mdinclude:: $(call GetMdIncludePath,$(CMP_$2_README))
Expand Down
25 changes: 4 additions & 21 deletions docs/source/_templates/breadcrumbs.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,15 @@
{% set suffix = '' %}
{% endif %}

{% if meta is defined and meta.custom_pagename is defined %}
{% set pagename = meta.custom_pagename %}
{% elif '_inc/' in pagename %}
{% set pagename = pagename|replace('_inc/','') + suffix %}
{% if pagename.startswith('_inc/') %}
{% set github_url = page_urls.get(pagename + suffix) %}
{% else %}
{% set pagename = conf_py_path + pagename + suffix %}
{% endif %}

{% if meta is defined and meta is not none %}
{% if 'github_url' in meta %}
{% set github_url = meta.github_url %}
{% set display_github = True %}
{% endif %}
{% endif %}

{% if github_url is not defined %}
{% set github_url = 'https://' + github_host|default('github.com') + '/' + github_user + '/' + github_repo + '/' + (theme_vcs_pageview_mode or 'blob') + '/' + github_version + '/' + pagename %}
{% set github_url = 'https://' + github_host|default('github.com') + '/' + github_user + '/' + github_repo + '/blob/' + github_version + '/' + conf_py_path + pagename + suffix %}
{% endif %}

<li class="wy-breadcrumbs-aside">
{% if github_url is not none %}
<a href="{{ github_url }}" class="fa fa-github"> {{ _('Edit on GitHub') }}</a>
{% elif show_source and source_url_prefix %}
<a href="{{ source_url_prefix }}{{ pagename }}{{ suffix }}">{{ _('View page source') }}</a>
{% elif show_source and has_source and sourcename %}
<a href="{{ pathto('_sources/' + sourcename, true)|e }}" rel="nofollow"> {{ _('View page source') }}</a>
<a href="{{ github_url }}" class="fa fa-github"> {{ _('View on GitHub') }}</a>
{% endif %}
</li>

Expand Down
4 changes: 4 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from sphinx.util.nodes import set_role_source_info, split_explicit_title
# So our custom extensions can be found
sys.path.insert(0, os.path.abspath('.'))
from filemap import buildFileMap


# -- Project information -----------------------------------------------------
Expand Down Expand Up @@ -117,6 +118,7 @@
"github_repo": "Sming",
"github_version": "develop",
"conf_py_path": "/docs/source/", # Path in the checkout to the docs root
"page_urls": {},
}

##
Expand All @@ -138,3 +140,5 @@

subprocess.call('make -C ../../Sming submodules SMING_ARCH=Host', shell=True)
subprocess.call('make -C .. setup api API_VERSION="' + version + '"', shell=True)

html_context['page_urls'] = buildFileMap(html_context)
70 changes: 70 additions & 0 deletions docs/source/filemap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#
# Build map of output files to source URLs so that edit links are created correctly
#

import os
import sys
import subprocess
import shutil

SMINGDIR = "../.."
GIT = shutil.which('git')


def gitcmd(args, **kwargs):
kwargs['stdout'] = subprocess.PIPE
with subprocess.Popen([GIT] + args, **kwargs) as child:
return child.stdout.read().decode()


def urljoin(elements):
res = ''
for e in elements:
if e == '':
continue
if res != '' and not res.endswith('/'):
res += '/'
res += e
return res


def getRemotePath(srcdir):
return gitcmd(['rev-parse', '--show-prefix'], cwd=srcdir).strip()


def getRemoteUrl(srcdir):
return gitcmd(['config', '--get', 'remote.origin.url'], cwd=srcdir).strip()


def getRemoteCommit(srcdir):
return gitcmd(['describe', '--always', 'HEAD'], cwd=srcdir).strip()


# Get map of directories to submodule URLs
def buildFileMap(ctx):
# index.rst files may be generated from README.md or README.rst
def getSourceFilename(srcdir, filename):
if filename != 'index.rst':
return filename
if os.path.exists(f"{srcdir}/README.md"):
return 'README.md'
if os.path.exists(f"{srcdir}/README.rst"):
return 'README.rst'
return filename

filemap = {}
for dirpath, dirnames, filenames in os.walk('_inc'):
srcpath = dirpath[5:] # skip '_inc/'
srcdir = os.path.join(SMINGDIR, srcpath)
# print(f"srcdir = {srcdir}", file=sys.stderr)
remotePath = getRemotePath(srcdir)
url = os.path.splitext(getRemoteUrl(srcdir))[0]
commit = getRemoteCommit(srcdir)
for f in filenames:
s = urljoin([url, 'blob', commit, remotePath, getSourceFilename(srcdir, f)])
filemap[f"{dirpath}/{f}"] = s

# for k, v in filemap.items():
# print(f"{k}: {v}", file=sys.stderr)

return filemap

0 comments on commit ee4a9f9

Please sign in to comment.