bugdown/api_code_examples: Support multiple fixtures per doc.

This commit adds support for passing in an argument to the macro
"call" to explicitly specify a fixture to render, like so:

{generate_code_example|doc_name|fixture(stream_message_with_args)}
This commit is contained in:
Eeshan Garg
2018-02-05 23:37:12 -03:30
committed by Tim Abbott
parent 7ae704d1bb
commit 09721e9ec3

View File

@@ -11,7 +11,7 @@ import markdown
import zerver.lib.api_test_helpers import zerver.lib.api_test_helpers
REGEXP = re.compile(r'\{generate_code_example\|\s*(.+?)\s*\|\s*(.+?)\s*\}') REGEXP = re.compile(r'\{generate_code_example\|\s*(.+?)\s*\|\s*(.+?)\s*(\(\s*(.+?)\s*\))?\}')
PYTHON_CLIENT_CONFIG_LINES = """ PYTHON_CLIENT_CONFIG_LINES = """
#!/usr/bin/env python3 #!/usr/bin/env python3
@@ -55,13 +55,18 @@ class APICodeExamplesPreprocessor(Preprocessor):
if match: if match:
function = match.group(1) function = match.group(1)
key = match.group(2) key = match.group(2)
argument = match.group(4)
if key == 'fixture': if key == 'fixture':
text = self.render_fixture(function) if argument:
text = self.render_fixture(function, name=argument)
else:
text = self.render_fixture(function)
elif key == 'example': elif key == 'example':
text = self.render_code_example(function) if argument == 'admin_config=True':
elif key == 'example(admin_config=True)': text = self.render_code_example(function, admin_config=True)
text = self.render_code_example(function, admin_config=True) else:
text = self.render_code_example(function)
# The line that contains the directive to include the macro # The line that contains the directive to include the macro
# may be preceded or followed by text or tags, in that case # may be preceded or followed by text or tags, in that case
@@ -77,10 +82,14 @@ class APICodeExamplesPreprocessor(Preprocessor):
done = True done = True
return lines return lines
def render_fixture(self, function: str) -> List[str]: def render_fixture(self, function: str, name: Optional[str]=None) -> List[str]:
fixture = [] fixture = []
fixture_dict = zerver.lib.api_test_helpers.FIXTURES[function] if name:
fixture_dict = zerver.lib.api_test_helpers.FIXTURES[function][name]
else:
fixture_dict = zerver.lib.api_test_helpers.FIXTURES[function]
fixture_json = ujson.dumps(fixture_dict, indent=4, sort_keys=True) fixture_json = ujson.dumps(fixture_dict, indent=4, sort_keys=True)
fixture.append('```') fixture.append('```')