Advertisement
Patasuss

Autodoc

Jun 25th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.24 KB | None | 0 0
  1. #
  2. # AUTO-DOC
  3. # v0.1
  4. # Idea and code by Patrick Wilde
  5. #
  6. # This program generates a basic structure
  7. # of a LaTeX documentation. It analyzes .java files
  8. # and automatically finds all functions.
  9. # This script may be bugged, and the corresponding output
  10. # may not be correct. Use with caution and always check
  11. # the output.
  12. #
  13. # Please don't steal and claim as your own. ;)
  14. #
  15. # Using Python 3.4
  16. #
  17.  
  18. #Trivial
  19. spaceWeight = 1
  20. tabWeight = 4
  21.  
  22. #Keywords used to identify functtion headers
  23. functionKeywords = ["public", "private", "static", "void", "int", "String", "char", "boolean", "byte", "double", "float"]
  24.  
  25. #Returns the indentation of the current line
  26. def getIndentation(line):
  27.     indentation = 0
  28.     for ch in line:
  29.         if(ch==' '):
  30.             indentation+=spaceWeight
  31.         elif(ch=='\t'):
  32.             indentation+=tabWeight
  33.         else:
  34.             break
  35.     return indentation
  36.  
  37. #Checks if the given line contains a function header
  38. def isFunctionHeader(line):
  39.     containsOneKeyword = False
  40.     for keyword in functionKeywords:
  41.         if(keyword in line):
  42.             containsOneKeyword = True
  43.             continue
  44.    
  45.     if(not containsOneKeyword):
  46.         return False
  47.    
  48.     if("(" in line and ")" in line and "{" in line):
  49.         return True
  50.    
  51.     return False
  52.  
  53. #Gets the functionname + parameters from the function header
  54. def getFunctionName(fString):
  55.     fName = ""
  56.     bracketIndex = fString.rfind(")")+1
  57.     lastChar = ''
  58.     ch = ''
  59.     openingBracketPassed = False
  60.     if(bracketIndex==-1):
  61.         return "N/A"
  62.     while(bracketIndex>0):
  63.         bracketIndex-=1
  64.         lastChar = ch
  65.         ch = fString[bracketIndex]
  66.         if( not((ch==' ' or ch=='\t') and lastChar=='(') ):
  67.             if(ch==' ' and openingBracketPassed):
  68.                 break
  69.             else:
  70.                 if(ch=='('):
  71.                     openingBracketPassed = True
  72.                 fName = ch + fName
  73.     return fName
  74.  
  75. #Extracts the classname from the filepath
  76. def getClassName(fileName):
  77.     fileName = fileName[0:-5]
  78.     slashIndex = fileName.rfind('/')
  79.    
  80.     if(slashIndex>-1):
  81.         fileName = fileName[slashIndex+1:]
  82.    
  83.     return fileName
  84.  
  85. #Generates the latex documentation code for a function (paragraph + filecode)
  86. def getLatexFunctionCode(fPath, fString, startLine, endLine):
  87.     code = ""
  88.     code += "\\paragraph{"+getFunctionName(fString)+"} Beschreibung der Funktion.\n"
  89.     code += "\\filecode{"+str(startLine)+"}{"+str(endLine)+"}{"+fPath+"}\n\n"
  90.     return code
  91.    
  92. #Generates the latex documentation code for a class
  93. def getLatexClassCode(fPath, endLine):
  94.     classname = getClassName(fPath)
  95.     code = ""
  96.     code = "%\n%"+classname.upper()+"\n%\n"
  97.     code += "\\subsection*{"+classname+"}\n Beschreibung der Klasse.\n \\\\ \\\\\n"
  98.     return code
  99.  
  100. #Generates the latex documentation code for an entire class
  101. def generateDoc(fileToOpen):
  102.     sourceFile = open(fileToOpen, "r")
  103.    
  104.     #0 - 'idle' mode
  105.     #1 - javadoc found, awaiting function line
  106.     #2 - found function, awaiting closing '}'
  107.     mode = 0
  108.     javadocLine = 0
  109.     functionIndentation = 0
  110.     functionString = ""
  111.     currLineNumber = 0
  112.     firstFunctionLineNumber = 0
  113.    
  114.     latexString = ""
  115.    
  116.     for currLine in sourceFile:
  117.         currLineNumber+=1
  118.         if(mode==0):
  119.             if("/**" in currLine):
  120.                 mode=1
  121.                 javadocLine=currLineNumber
  122.        
  123.         elif(mode==1):
  124.             if("*" in currLine):
  125.                 continue
  126.             elif("@" in currLine):
  127.                 continue
  128.             elif(isFunctionHeader(currLine)):
  129.                 mode=2
  130.                 functionIndentation = getIndentation(currLine)
  131.                 functionString = currLine
  132.             else:
  133.                 mode = 0
  134.  
  135.         elif(mode==2):
  136.                 if("}" in currLine and getIndentation(currLine)==functionIndentation):
  137.                     #print("From "+str(javadocLine) + " to " + str(currLineNumber))
  138.                    
  139.                    
  140.                     if(firstFunctionLineNumber==0):
  141.                         firstFunctionLineNumber = javadocLine
  142.                        
  143.                     latexString += getLatexFunctionCode(fileToOpen, functionString, javadocLine, currLineNumber)
  144.                     mode=0
  145.        
  146.         else:
  147.             print("%[AUTODOC] Invalid mode!")
  148.  
  149.     latexString = getLatexClassCode(fileToOpen, javadocLine-1) + latexString + "\n\n\n\n"
  150.     print(latexString)
  151.     sourceFile.close()
  152.  
  153.    
  154. files = ["Edge.java", "Graph.java", "LabyrinthFactory.java", "LadyBugTortureSimulation.java", "Node.java", "EdgeTest.java", "GraphTest.java", "LabyrinthFactoryTest.java", "LadyBugTortureSimulationTest.java", "NodeTest.java"]
  155.  
  156. for fileName in files:
  157.     generateDoc(fileName)
  158.  
  159. #generateDoc("../LabyrinthFactory.java")
  160.  
  161. #print(getClassName("../hello/my/name/is/dog/LabyrinthFactory.java"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement