def inject_source_code(file_content, pretty=False, base_path=''): r""" Replace all "%@ import..." statements in the latex file with the source code it refers to. Parameters ---------- file_content : str The file_content to search for and replace placeholders in. pretty : bool Whether or not to use fancy formatting. base_path : str Base path that the input paths are relative to Returns ------- file_content : str Augmented file content Example ------- Test that the input string is returned when there's no match >>> inject_source_code('a few words') 'a few words' Test that a valid import statement is parsed and injected >>> inject_source_code('foobar\n%@import ./code_import.py (def get_regex(.*))+\nstuff') 'foobar\n\\begin{verbatim}\ndef get_regex_match_in_file(file, regex):\\end{verbatim}\n\nstuff' Test that a string containing two import statements is handled correctly >>> inject_source_code('foobar\n%@import ./code_import.py (def get_regex(.*))+\nstuff\n%@import ./code_import.py (def get_regex(.*))+\nmore stuff') 'foobar\n\\begin{verbatim}\ndef get_regex_match_in_file(file, regex):\\end{verbatim}\n\nstuff\n\\begin{verbatim}\ndef get_regex_match_in_file(file, regex):\\end{verbatim}\n\nmore stuff' """ # Find all lines matching the import statement format source_imports = re.findall(r'\n(%@import ([^\ ]+) (.*))\n', file_content); # For each import statement for source_import in source_imports: import_statement = source_import[0]; import_regex = source_import[2]; file_path = source_import[1]; file_base = os.path.split(file_path)[0]; if base_path != '': file_path = os.path.join(base_path, file_path); file_base = os.path.split(file_path)[0]; # Replace import statement with the matched portion # of the referenced file file_content = file_content.replace( import_statement, verbatim.verbatim_code( get_regex_match_in_file(file_path, import_regex), pretty ) ); return file_content;
def process_blocks(file_content, pretty=False): r""" Identify all inline code and exec result blocks and format them in the same fashion as the exec result blocks pulled from referenced files. Parameters ---------- file_content : str The file_content to search for and replace placeholders in. pretty : bool Whether to use fancy formatting or not Test that code import blocks are picked up and formatted >>> process_blocks('foobar\n%@import\n<?php\nvar_dump($_POST);\n%@\ntest\n%@import\ndef test(a, b):\n print \'foobar\';\n%@\nstuff') "foobar\n\\begin{verbatim}\n<?php\nvar_dump($_POST);\n\\end{verbatim}\n\ntest\n\\begin{verbatim}\ndef test(a, b):\n print 'foobar';\n\\end{verbatim}\n\nstuff" Test that exec result blocks are picked up and formatted >>> process_blocks('foobar\n%@exec\n$ echo "Just another Perl hacker"\n%@\nstuff') 'foobar\n\\begin{verbatim}\n$ echo "Just another Perl hacker"\n\\end{verbatim}\n\nstuff' """ # Find all lines matching the exec or import statement format blocks = re.findall(r'(%@(import|exec)\n((.*\n)+?)%@)', file_content, re.MULTILINE); # For each import statement for block in blocks: outer_block = block[0]; block_type = block[1]; inner_block = block[2]; if (block_type == "import"): formatted_block = verbatim.verbatim_code(inner_block, pretty); else: formatted_block = verbatim.verbatim_exec(inner_block, pretty); file_content = file_content.replace( outer_block, formatted_block ); return file_content;