3 from docutils
import nodes
4 from docutils
.parsers
.rst
import Directive
5 from docutils
.statemachine
import StringList
8 class APIExamples(Directive
):
9 """Add directive `api-examples` to be used as follows:
15 The arguments should be proper filenames to source files.
16 This directives tries to detect the language from the file extension
17 and supports the file extensions specified in `self.exts`.
20 # Set tab title and language for syntax highlighting
22 '.cpp': {'title': 'C++', 'lang': 'c++'},
23 '.java': {'title': 'Java', 'lang': 'java'},
24 '.py': {'title': 'Python', 'lang': 'python'},
25 '.smt2': {'title': 'SMT-LIBv2', 'lang': 'smtlib'},
28 # The "arguments" are actually the content of the directive
32 # collect everything in a list of strings
33 content
= ['.. tabs::', '']
35 for file in self
.content
:
36 # detect file extension
37 _
, ext
= os
.path
.splitext(file)
39 title
= self
.exts
[ext
]['title']
40 lang
= self
.exts
[ext
]['lang']
46 content
.append(f
' .. tab:: {title}')
48 content
.append(f
' .. literalinclude:: {file}')
49 content
.append(f
' :language: {lang}')
50 content
.append(f
' :linenos:')
52 # parse the string list
53 node
= nodes
.Element()
54 self
.state
.nested_parse(StringList(content
), 0, node
)
59 app
.setup_extension('sphinx_tabs.tabs')
60 app
.add_directive("api-examples", APIExamples
)
63 'parallel_read_safe': True,
64 'parallel_write_safe': True,