Search code examples
python-sphinxpandoc

Pandoc equivalent to Sphinx's :download: role


Sphinx defines a role :download: that instructs Sphinx to copy the reference file to _downloads.

Does Pandoc has a similar feature?


Solution

  • Pandoc does not have that feature built-in, but it can be added with a few lines of Lua:

    local prefix = 'media'
    local path = pandoc.path
    
    function Code (code)
      if code.attributes.role == 'download' then
        local description, filename = code.text:match '(.*) %<(.*)%>$'
        local mimetype, content = pandoc.mediabag.fetch(filename)
        local mediabag_filename = path.join{
          pandoc.utils.sha1(content),
          path.filename(filename)
        }
        if content and mimetype then
          pandoc.mediabag.insert(mediabag_filename, mimetype, content)
        end
        return pandoc.Link(description, path.join{prefix, mediabag_filename})
      end
    end
    

    Use the script by saving it to a file download-role.lua and the call pandoc with

    pandoc --lua-filter=download-role.lua --extract-media=media ...
    

    This will also work when using Markdown:

    `this example script <../example.py>`{role=download}