|
1 | 1 | from docutils import nodes |
2 | | -from sphinx.addnodes import download_reference |
| 2 | +from docutils.parsers.rst import directives |
3 | 3 | from sphinx.directives.code import LiteralInclude |
4 | 4 | import re |
5 | 5 |
|
| 6 | +from sphinx.util.typing import OptionSpec |
| 7 | + |
| 8 | + |
6 | 9 | class Sample(LiteralInclude): |
| 10 | + option_spec: OptionSpec = { |
| 11 | + 'full': directives.flag, |
| 12 | + } |
| 13 | + |
| 14 | + def run(self): |
| 15 | + self.arguments[0] = "/../samples/" + self.arguments[0] |
| 16 | + self.options['language'] = 'php' |
7 | 17 |
|
8 | | - def run(self): |
9 | | - self.arguments[0] = "/../samples/" + self.arguments[0] |
10 | | - self.options['language'] = 'php' |
| 18 | + pattern = r"[\s+]?(\<\?php.*?]\);)" |
11 | 19 |
|
12 | | - pattern = r"[\s+]?(\<\?php.*?]\);)" |
| 20 | + code_block = super(Sample, self).run()[0] |
| 21 | + if 'full' in self.options: |
| 22 | + return [code_block] |
13 | 23 |
|
14 | | - code_block = super(Sample, self).run()[0] |
15 | | - string = str(code_block[0]) |
| 24 | + string = str(code_block[0]) |
16 | 25 |
|
17 | | - match = re.match(pattern, string, re.S) |
18 | | - if match is None: |
19 | | - return [code_block] |
| 26 | + match = re.match(pattern, string, re.S) |
| 27 | + if match is None: |
| 28 | + return [code_block] |
20 | 29 |
|
21 | | - auth_str = match.group(1).strip() |
22 | | - main_str = re.sub(pattern, "", string, 0, re.S).strip() |
| 30 | + main_str = re.sub(pattern, "", string, 0, re.S).strip() |
| 31 | + if main_str == '': |
| 32 | + return [code_block] |
23 | 33 |
|
24 | | - show_hide_btn = download_reference(reftarget=self.arguments[0]) |
| 34 | + return [ |
| 35 | + nodes.literal_block(main_str, main_str, language="php") |
| 36 | + ] |
25 | 37 |
|
26 | | - return [ |
27 | | - show_hide_btn, |
28 | | - nodes.literal_block(auth_str, auth_str, language="php"), |
29 | | - nodes.literal_block(main_str, main_str, language="php")] |
30 | 38 |
|
31 | 39 | def setup(app): |
32 | | - app.add_directive('sample', Sample) |
33 | | - return {'version': '0.1'} |
| 40 | + app.add_directive('sample', Sample) |
| 41 | + return {'version': '0.1'} |
0 commit comments