mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	openapi/markdown_extension: Refactor extract_code_example.
This refactors `extract_code_example` to return a nested list
of code snippets between '{code_example|start/end}' instead of
returing a list of all the lines between '{code_example|start/end}'
markers in the code examples.
Appropriate changes have been made to render_python_code_example.
			
			
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							302906211d
						
					
				
				
					commit
					642d1a20d0
				
			@@ -61,8 +61,8 @@ def parse_language_and_options(input_str: Optional[str]) -> Tuple[str, Dict[str,
 | 
				
			|||||||
        return (language, options)
 | 
					        return (language, options)
 | 
				
			||||||
    return (language, {})
 | 
					    return (language, {})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def extract_code_example(source: List[str], snippet: List[str],
 | 
					def extract_code_example(source: List[str], snippet: List[Any],
 | 
				
			||||||
                         example_regex: Pattern[str]) -> List[str]:
 | 
					                         example_regex: Pattern[str]) -> List[Any]:
 | 
				
			||||||
    start = -1
 | 
					    start = -1
 | 
				
			||||||
    end = -1
 | 
					    end = -1
 | 
				
			||||||
    for line in source:
 | 
					    for line in source:
 | 
				
			||||||
@@ -77,7 +77,7 @@ def extract_code_example(source: List[str], snippet: List[str],
 | 
				
			|||||||
    if (start == -1 and end == -1):
 | 
					    if (start == -1 and end == -1):
 | 
				
			||||||
        return snippet
 | 
					        return snippet
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    snippet.extend(source[start + 1: end])
 | 
					    snippet.append(source[start + 1: end])
 | 
				
			||||||
    source = source[end + 1:]
 | 
					    source = source[end + 1:]
 | 
				
			||||||
    return extract_code_example(source, snippet, example_regex)
 | 
					    return extract_code_example(source, snippet, example_regex)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -91,15 +91,16 @@ def render_python_code_example(function: str, admin_config: Optional[bool]=False
 | 
				
			|||||||
    else:
 | 
					    else:
 | 
				
			||||||
        config = PYTHON_CLIENT_CONFIG.splitlines()
 | 
					        config = PYTHON_CLIENT_CONFIG.splitlines()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    snippet = extract_code_example(function_source_lines, [], PYTHON_EXAMPLE_REGEX)
 | 
					    snippets = extract_code_example(function_source_lines, [], PYTHON_EXAMPLE_REGEX)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    code_example = []
 | 
					    code_example = []
 | 
				
			||||||
    code_example.append('```python')
 | 
					    code_example.append('```python')
 | 
				
			||||||
    code_example.extend(config)
 | 
					    code_example.extend(config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for line in snippet:
 | 
					    for snippet in snippets:
 | 
				
			||||||
        # Remove one level of indentation and strip newlines
 | 
					        for line in snippet:
 | 
				
			||||||
        code_example.append(line[4:].rstrip())
 | 
					            # Remove one level of indentation and strip newlines
 | 
				
			||||||
 | 
					            code_example.append(line[4:].rstrip())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    code_example.append('print(result)')
 | 
					    code_example.append('print(result)')
 | 
				
			||||||
    code_example.append('\n')
 | 
					    code_example.append('\n')
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user