Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def ProcessImports(fileAsString, fancyVerbatim, verbose):
- """
- Checks if the file includes import statements and acts upon them
- Parameters
- ----------------
- fileAsString : str
- The file displayed as a string for search&replace
- Returns
- ----------------
- processedString : str
- fileAsString's %@import statements fully processed
- Example
- ---------------
- >>>ProcessImports('%@import\nthis should be displayed\n%@')
- PrepareImportString("this should be displayed")
- """
- processedString = fileAsString
- if verbose:
- print "Processing %@import raw text-blocks"
- """
- handles %@imports that don't actually import anything
- (e.g. raw text into code-block format)
- """
- solitary = re.search(r"^%@import\n(.|\n)*?%@$",
- processedString, re.MULTILINE)
- if solitary:
- stringToPrepare = solitary.group(0)[9:-3]
- while solitary:
- latexPreparedString = PrepareImportString(stringToPrepare,
- fancyVerbatim)
- processedString = processedString.replace(solitary.group(0),
- latexPreparedString)
- solitary = re.search(r"^%@import\n(.|\n)*?%@$",
- processedString, re.MULTILINE)
- if solitary:
- stringToPrepare = solitary.group(0)[9:-3]
- foundStatements = re.findall(r"^%@import.*",
- processedString, re.MULTILINE)
- if verbose:
- if len(foundStatements) == 0:
- print "No %@import hits"
- else:
- print "Processing %@imports of external scripts"
- #textblocksToImport = []
- #loopvariables
- iterator = 0
- for x in foundStatements:
- """
- splits %@import, the file parameter and the regex statement
- into separate strings. Makes sure to remove parentheses if
- it's going to import from a file.
- """
- splittedStatement = re.split(r"\s", x, 2)
- splittedStatement[2] = splittedStatement[2][1:-1]
- fileToImportFrom = open(splittedStatement[1], "r")
- fileToImportFromAsString = fileToImportFrom.read()
- fileToImportFrom.close()
- textToImport = re.search(splittedStatement[2],
- fileToImportFromAsString)
- latexPreparedString = " "
- if textToImport:
- latexPreparedString = PrepareImportString(
- textToImport.group(0), fancyVerbatim)
- processedString = processedString.replace(
- foundStatements[iterator], latexPreparedString)
- iterator=iterator+1
- return processedString
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement