extlinks support for download target="_blank"

7 views
Skip to first unread message

Andreas Maier

unread,
Jun 4, 2016, 9:02:41 AM6/4/16
to sphinx-dev
I started using the extlinks extension and like it!

For download links, the established practice seems to be to specify the following HTML attributes in the <a> link element:

    download target="_blank"

That causes the browser to download, even if the page type could be rendered by the browser.

Because these are HTML attributes, the current feature set in the extlinks config variable does not seem to be sufficient to get them set.

With additional entries in the extlinks dict value tuple, that could be achieved.

Any comments about the idea?

Andy

Komiya Takeshi

unread,
Jun 5, 2016, 5:33:51 AM6/5/16
to sphin...@googlegroups.com
Hi,

I think it is better to create your idea as a new extension.

Because sphinx.ext.extlink is a very small extension only to define
shortcut of hyperlink notations.
It could not customize the behavior of hyperlinks.

You can do it with following code (It's roughly, but works fine):
```
# -*- coding: utf-8 -*-
from docutils import nodes


class download_gist(nodes.Element, nodes.Inline):
pass


def visit_download_gist(self, node):
url = "https://gist.github.com/%s" % node['reftarget']
self.body.append('<a class="reference download external" href="%s"
download="">' % url)
self.context.append('</a>')


def depart_download_gist(self, node):
self.body.append(self.context.pop())


def setup(app):
from sphinx.roles import XRefRole
app.add_role('gist-dl', XRefRole(nodeclass=download_gist))
app.add_node(download_gist,
html=(visit_download_gist, depart_download_gist))
```


Thanks,
Takeshi KOMIYA

2016-06-04 22:02 GMT+09:00 'Andreas Maier' via sphinx-dev
<sphin...@googlegroups.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "sphinx-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sphinx-dev+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages