Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
better factorization of how examples are displayed in RST
[simgrid.git] / docs / source / _ext / showfile.py
1 # -*- coding: utf-8 -*-
2
3 import os
4 from docutils.parsers.rst import Directive, directives
5 from docutils import nodes
6 from docutils.statemachine import StringList
7 from sphinx.util.osutil import copyfile
8 from sphinx.util import logging
9
10 class ShowFileDirective(Directive):
11     """
12     Show a file or propose it to download.
13     """
14
15     has_content = False
16     optional_arguments = 1
17     option_spec = {
18       'language': directives.unchanged
19     }
20
21     def run(self):
22 #        self.assert_has_content()
23
24         filename = self.arguments[0]
25         language = "python"
26         if 'language' in self.options:
27             language = self.options['language']
28
29         logger = logging.getLogger(__name__)
30 #        logger.info('showfile {} in {}'.format(filename, language))
31
32         new_content = [
33           '.. toggle-header::',
34           '   :header: View {}'.format(filename),
35           '',
36           '   `Download {} <https://framagit.org/simgrid/simgrid/tree/{}>`_'.format(os.path.basename(filename), filename),
37           '',
38           '   .. literalinclude:: ../../{}'.format(filename),
39           '      :language: {}'.format(language),
40           ''
41         ]
42
43         for idx, line in enumerate(new_content):
44 #            logger.info('{} {}'.format(idx,line))
45             self.content.data.insert(idx, line)
46             self.content.items.insert(idx, (None, idx))
47
48         node = nodes.container()
49         self.state.nested_parse(self.content, self.content_offset, node)
50         return node.children
51
52 def setup(app):
53     app.add_directive('showfile', ShowFileDirective)