Skip to content
Permalink
Browse files

fix githublink role

  • Loading branch information...
sdpython committed Nov 20, 2016
1 parent 29afb23 commit 6b0d9f3d3cb20c23eaa18cb26e6ab5aa52edca92
@@ -60,10 +60,16 @@ def test_githublink(self):
before
:githublink:`j`
:githublink:`j|py`
after
:githublink:`j`
again
:githublink:`j|py|84`
this code shoud appear
""".replace(" ", "")
if sys.version_info[0] >= 3:
@@ -72,7 +78,7 @@ def test_githublink(self):
html = rst2html(content, fLOG=fLOG,
writer="custom", keep_warnings=True,
directives=None,
github_options=dict(user="sdpython", project="pyquickhelper"))
githublink_options=dict(user="sdpython", project="pyquickhelper"))

t1 = "this code shoud not appear"
if t1 in html:
@@ -82,6 +88,18 @@ def test_githublink(self):
if t1 not in html:
raise Exception(html)

t1 = "https://github.com/sdpython/pyquickhelper/blob/master/src/string.py#L7"
if t1 not in html:
raise Exception(html)

t1 = "https://github.com/sdpython/pyquickhelper/blob/master/src/string#L11"
if t1 not in html:
raise Exception(html)

t1 = "https://github.com/sdpython/pyquickhelper/blob/master/src/string.py#L84"
if t1 not in html:
raise Exception(html)

temp = get_temp_folder(__file__, "temp_githublink")
with open(os.path.join(temp, "out_githublink.html"), "w", encoding="utf8") as f:
f.write(html)
@@ -399,15 +399,27 @@ def set_sphinx_variables(fileconf, module_name, author, year, theme, theme_path,
\\newcommand{\\R}{\\mathbb{R}}
"""

if github_user:
githublink_options = dict(user=github_user)
else:
githublink_options = None
if github_repo:
githublink_options['project'] = github_repo.strip("/").split("/")[-1]

if extlinks is None:
extlinks = dict()
githublink_options = None
elif 'issue' in extlinks:
issue = extlinks['issue'].split('/')
len = len(issue)
user = issue[-4]
project = issue[-3]
githublink_options = dict(user=user, project=project)
issue = extlinks['issue'][0].split('/')
le = len(issue)
if le > 0:
user = issue[-4]
project = issue[-3]
if githublink_options is None:
githublink_options = {}
if 'user' not in githublink_options:
githublink_options["user"] = user
if 'project' not in githublink_options:
githublink_options["project"] = project

# collect local variables
loc = locals()
@@ -802,7 +802,7 @@ def produces_indexes(
under = "=" * len(title)

content = "\n".join([".. contents::", " :local:",
" :maxdepth: 1", "", "", "Summary", "+++++++"])
" :depth: 1", "", "", "Summary", "+++++++"])

if os.environ.get("USERNAME", os.environ.get("USER", "````````````")) in title:
raise HelpGenException(
@@ -19,7 +19,7 @@ class githublink_node(nodes.Element):
pass


def make_link_node(rawtext, app, path, anchor, lineno, options):
def make_link_node(rawtext, app, path, anchor, lineno, options, settings):
"""
Create a link to a github file.
@@ -29,18 +29,40 @@ def make_link_node(rawtext, app, path, anchor, lineno, options):
:param lineno: line number
:param anchor: anchor
:param options: Options dictionary passed to role func.
:param settings: settings
"""
try:
opt = app.config.github_options
exc = []
try:
config = app.config
except Exception as e:
exc.append(e)
config = None
if config is not None:
try:
opt = config.githublink_options
except Exception as ee:
exc.append(ee)
opt = None
else:
opt = None
if not opt:
raise AttributeError
try:
opt = settings.githublink_options
except Exception as eee:
exc.append(eee)
opt = None
if not opt:
lines = "\n".join("## {0} ##".format(str(e)) for e in exc)
raise AttributeError(
"settings does not have a key githublink_options, app does not have a member config.\n{0}".format(lines))
except AttributeError as err:
raise ValueError(
'github_options configuration value is not set (%s)' % str(err))
'githublink_options configuration value is not set (%s)' % str(err))
user = opt["user"]
project = opt["project"]
ref = "https://github.com/{0}/{1}/blob/master/{2}#{3}".format(
user, project, path.replace("\\", "/"), lineno)
ref = "https://github.com/{0}/{1}/blob/master/{2}#L{3}".format(
user, project, path, lineno)
set_classes(options)
node = nodes.reference(rawtext, anchor, refuri=ref, **options)
return node
@@ -52,7 +74,7 @@ def githublink_role(role, rawtext, text, lineno, inliner,
Defines custom role *githublink*. The following instruction add
a link to the documentation on github.
:githublink:
:githublink:`source on GitHub|py`
:param name: The role name used in the document.
:param rawtext: The entire markup snippet, with role.
@@ -62,6 +84,10 @@ def githublink_role(role, rawtext, text, lineno, inliner,
:param options: Directive options for customization.
:param content: The directive content for customization.
:return: ``[node], []``
The pipe ``|`` indicates that an extension must be added to
*docname* to get the true url.
"""
if not rawtext or len(rawtext) == 0:
rawtext = "github"
@@ -72,12 +98,30 @@ def githublink_role(role, rawtext, text, lineno, inliner,
while len(folder) > 0 and not os.path.exists(git):
folder = os.path.split(folder)[0]
git = os.path.join(folder, ".git")
path = docname.replace(git, "").strip("\\/")
if len(folder) > 0:
path = docname[len(folder):]
else:
path = os.path.join('src', docname)
path = path.replace("\\", "/").strip("/")

if "|" in text:
# no extension to the url, we add one
spl = text.split("|")
if len(spl) == 3:
text, ext, no = spl
path += "." + ext
lineno = int(no)
elif len(spl) != 2:
raise ValueError("unable to interpret '{0}'".format(text))
else:
text, ext = spl
path += "." + ext

try:
node = make_link_node(rawtext, app, path, lineno, rawtext, options)
node = make_link_node(rawtext=rawtext, app=app, path=path, lineno=lineno,
options=options, anchor=text, settings=inliner.document.settings)
except (ValueError, AttributeError) as e:
msg = inliner.reporter.error('github_options must be set to a dictionary with keys (user, project)\n%s' % str(e),
msg = inliner.reporter.error('githublink_options must be set to a dictionary with keys (user, project)\n%s' % str(e),
line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
@@ -89,5 +133,5 @@ def setup(app):
setup for ``githublink`` (sphinx)
"""
app.add_role('githublink', githublink_role)
app.add_config_value('github_options', None, 'env')
app.add_config_value('githublink_options', None, 'env')
return {'version': sphinx.__display_version__, 'parallel_read_safe': True}

0 comments on commit 6b0d9f3

Please sign in to comment.
You can’t perform that action at this time.