Custom download list directive

79 views
Skip to first unread message

Christian Sigge Steiniche

unread,
Mar 11, 2021, 2:41:08 AM3/11/21
to sphinx-users

Hi all

I am trying to write a custom plugin for sphinx to list files to download from a directory (with filters etc.).

The output should be formatted as a list of ":downloads:" would be:

* :download:`File1</assets/_tmp/file1.txt>`
* :download:`File2</assets/_tmp/file2.txt>`
* :download:`File3</assets/_tmp/file3.txt>`

The html output of a list of download becomes:

download_list_1.PNG
Trying to do the same from my "SphinxDirective" class plugin, I get the following result

download_list_2.PNG
The files are automatically copied and referenced correctly, but I do not get the downloads icon and the text formatting as in the top example.

The html output using ":download:" becomes:

<ul class="simple">
<li><p><a class="reference download internal" download="" href="../_downloads/73e25db1add1c37f55d8f773b290e27c/file1.txt"><code class="xref download docutils literal notranslate"><span class="pre">File1</span></code></a></p></li>
...
</ul>

The output using my custom plugin becomes:

<ul>
<li><a class="reference download internal" download="" href="../_downloads/f95e443b8a4da2a3b622a298a3df8ed7/file1.txt">file1.txt</a></li>
...
</ul>

Note that the first includes an additional element: code class="xref download docutils literal notranslate". I assume this is the one adding the icon and the download formatting. 

My (slightly simplified) implementation:

class DownloadLister(SphinxDirective):

has_content = False

def run(self):

  lst = nodes.bullet_list()
  for file_path in glob.glob('source/assets/_tmp/*.txt'):

    file_path_abs = os.path.abspath(file_path)

    item = nodes.list_item()
    item += download_reference(text=os.path.basename(file_path_abs), reftarget=file_path_abs)
    lst += item

  return [lst]

In rst:

.. downloadlister::

I assume that I am missing a node type, but I am not able to find it in the source code, sphinx documentation or docutils documentation.

In general, I find it very confusing to create sphinx extensions as the documentation is sparse at best. 

Any help with the above is highly appreciated.

Br
Christian




Reply all
Reply to author
Forward
0 new messages