Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. def ProcessImports(fileAsString, fancyVerbatim, verbose):
  2. """
  3. Checks if the file includes import statements and acts upon them
  4.  
  5. Parameters
  6. ----------------
  7. fileAsString : str
  8. The file displayed as a string for search&replace
  9.  
  10. Returns
  11. ----------------
  12. processedString : str
  13. fileAsString's %@import statements fully processed
  14.  
  15. Example
  16. ---------------
  17. >>>ProcessImports('%@import\nthis should be displayed\n%@')
  18. PrepareImportString("this should be displayed")
  19.  
  20. """
  21.  
  22. processedString = fileAsString
  23.  
  24. if verbose:
  25. print "Processing %@import raw text-blocks"
  26. """
  27. handles %@imports that don't actually import anything
  28. (e.g. raw text into code-block format)
  29. """
  30. solitary = re.search(r"^%@import\n(.|\n)*?%@$",
  31. processedString, re.MULTILINE)
  32. if solitary:
  33. stringToPrepare = solitary.group(0)[9:-3]
  34. while solitary:
  35. latexPreparedString = PrepareImportString(stringToPrepare,
  36. fancyVerbatim)
  37. processedString = processedString.replace(solitary.group(0),
  38. latexPreparedString)
  39. solitary = re.search(r"^%@import\n(.|\n)*?%@$",
  40. processedString, re.MULTILINE)
  41. if solitary:
  42. stringToPrepare = solitary.group(0)[9:-3]
  43.  
  44. foundStatements = re.findall(r"^%@import.*",
  45. processedString, re.MULTILINE)
  46.  
  47. if verbose:
  48. if len(foundStatements) == 0:
  49. print "No %@import hits"
  50. else:
  51. print "Processing %@imports of external scripts"
  52.  
  53. #textblocksToImport = []
  54. #loopvariables
  55. iterator = 0
  56.  
  57. for x in foundStatements:
  58. """
  59. splits %@import, the file parameter and the regex statement
  60. into separate strings. Makes sure to remove parentheses if
  61. it's going to import from a file.
  62. """
  63. splittedStatement = re.split(r"\s", x, 2)
  64. splittedStatement[2] = splittedStatement[2][1:-1]
  65.  
  66. fileToImportFrom = open(splittedStatement[1], "r")
  67. fileToImportFromAsString = fileToImportFrom.read()
  68. fileToImportFrom.close()
  69.  
  70. textToImport = re.search(splittedStatement[2],
  71. fileToImportFromAsString)
  72. latexPreparedString = " "
  73. if textToImport:
  74. latexPreparedString = PrepareImportString(
  75. textToImport.group(0), fancyVerbatim)
  76.  
  77. processedString = processedString.replace(
  78. foundStatements[iterator], latexPreparedString)
  79.  
  80. iterator=iterator+1
  81.  
  82. return processedString
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement