Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further convert the example documentation to the tabbed 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
23         filename = self.arguments[0]
24         language = "python"
25         if 'language' in self.options:
26             language = self.options['language']
27
28         logger = logging.getLogger(__name__)
29 #        logger.info('showfile {} in {}'.format(filename, language))
30
31         new_content = [
32           '.. toggle-header::',
33           '   :header: View {}'.format(filename),
34           '',
35           '   `Download {} <https://framagit.org/simgrid/simgrid/tree/{}>`_'.format(os.path.basename(filename), filename),
36           '',
37           '   .. literalinclude:: ../../{}'.format(filename),
38           '      :language: {}'.format(language),
39           ''
40         ]
41
42         for idx, line in enumerate(new_content):
43 #            logger.info('{} {}'.format(idx,line))
44             self.content.data.insert(idx, line)
45             self.content.items.insert(idx, (None, idx))
46
47         node = nodes.container()
48         self.state.nested_parse(self.content, self.content_offset, node)
49         return node.children
50
51 class ExampleTabDirective(Directive):
52     """
53     A group-tab for a given language, in the presentation of the examples.
54     """
55     has_content = True
56     optional_arguments = 0
57     mandatory_argument = 0
58
59     def run(self):
60         self.assert_has_content()
61
62         filename = self.content[0].strip()
63         self.content.trim_start(1)
64
65         (language, langcode) = (None, None)
66         if filename[-3:] == '.py':
67             language = 'Python'
68             langcode = 'py'
69         elif filename[-4:] == '.cpp':
70             language = 'C++'
71             langcode = 'cpp'
72         elif filename[-4:] == '.xml':
73             language = 'XML'
74             langcode = 'xml'
75         else:
76             raise Exception("Unknown language '{}'. Please choose '.cpp', '.py' or '.xml'".format(language))
77
78         for idx, line in enumerate(self.content.data):
79             self.content.data[idx] = '   ' + line
80
81         for idx, line in enumerate([
82             '.. group-tab:: {}'.format(language),
83             '   ']):
84             self.content.data.insert(idx, line)
85             self.content.items.insert(idx, (None, idx))
86
87         for line in [
88             '',
89             '   .. showfile:: {}'.format(filename),
90             '      :language: {}'.format(langcode),
91             '']:
92             idx = len(self.content.data)
93             self.content.data.insert(idx, line)
94             self.content.items.insert(idx, (None, idx))
95
96 #        logger = logging.getLogger(__name__)
97 #        logger.info('------------------')
98 #        for line in self.content.data:
99 #            logger.info('{}'.format(line))
100
101         node = nodes.container()
102         self.state.nested_parse(self.content, self.content_offset, node)
103         return node.children
104
105 def setup(app):
106     app.add_directive('showfile', ShowFileDirective)
107     app.add_directive('example-tab', ExampleTabDirective)
108