3 from docutils
import nodes
4 from docutils
.parsers
.rst
import Directive
5 from docutils
.statemachine
import StringList
6 from sphinx
.util
.docutils
import SphinxDirective
7 from sphinx
.util
.nodes
import nested_parse_with_titles
9 class IncludeBuildFile(SphinxDirective
):
10 """Add directive `include-build-file` to be used as follows:
12 .. include-build-file:: <filename>
14 The argument should be a filename of an rst files within one of the
15 folders given by the `ibf_folders` config option.
18 # The "arguments" are actually the content of the directive
22 filename
= ''.join(self
.content
)
23 for folder
in self
.env
.config
.ibf_folders
:
24 candidate
= os
.path
.join(folder
, filename
)
25 if os
.path
.isfile(candidate
):
28 content
= open(filename
).readlines()
29 content
= [line
.rstrip('\n') for line
in content
]
30 # parse the string list
31 node
= nodes
.Element()
32 nested_parse_with_titles(self
.state
, StringList(content
), node
)
33 self
.state
.document
.settings
.env
.note_dependency(filename
)
38 app
.add_config_value('ibf_folders', [], 'env')
39 app
.add_directive('include-build-file', IncludeBuildFile
)
42 'parallel_read_safe': True,
43 'parallel_write_safe': True,