Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # AUTO-DOC
- # v0.2
- # Idea and code by Patrick Wilde
- #
- # This program generates a basic structure
- # of a LaTeX documentation. It analyzes .java files
- # and automatically finds all functions.
- # This script may be bugged, and the corresponding output
- # may not be correct. Use with caution and always check
- # the output.
- #
- # Please don't steal and don't claim it as your own. ;)
- #
- # Using Python 3.4
- #
- #Trivial
- spaceWeight = 1
- tabWeight = 4
- #Keywords used to identify functtion headers
- functionKeywords = ["public", "private", "static", "void", "int", "String", "char", "boolean", "byte", "double", "float"]
- #Returns the indentation of the current line
- def getIndentation(line):
- indentation = 0
- for ch in line:
- if(ch==' '):
- indentation+=spaceWeight
- elif(ch=='\t'):
- indentation+=tabWeight
- else:
- break
- return indentation
- #Checks if the given line contains a function header
- def isFunctionHeader(line):
- containsOneKeyword = False
- for keyword in functionKeywords:
- if(keyword in line):
- containsOneKeyword = True
- continue
- if(not containsOneKeyword):
- return False
- if("(" in line and ")" in line and "{" in line):
- return True
- return False
- #Gets the functionname + parameters from the function header
- def getFunctionName(fString):
- fName = ""
- bracketIndex = fString.rfind(")")+1
- lastChar = ''
- ch = ''
- openingBracketPassed = False
- if(bracketIndex==-1):
- return "N/A"
- while(bracketIndex>0):
- bracketIndex-=1
- lastChar = ch
- ch = fString[bracketIndex]
- if( not((ch==' ' or ch=='\t') and lastChar=='(') ):
- if(ch==' ' and openingBracketPassed):
- break
- else:
- if(ch=='('):
- openingBracketPassed = True
- fName = ch + fName
- return fName
- #Extracts the classname from the filepath
- def getClassName(fileName):
- fileName = fileName[0:-5]
- slashIndex = fileName.rfind('/')
- if(slashIndex>-1):
- fileName = fileName[slashIndex+1:]
- return fileName
- #Generates the latex documentation code for a function (paragraph + filecode)
- def getLatexFunctionCode(fPath, fString, startLine, endLine):
- code = ""
- code += "\\paragraph{"+getFunctionName(fString)+"} Beschreibung der Funktion.\n"
- code += "\\filecode{"+str(startLine)+"}{"+str(endLine)+"}{"+fPath+"}\n\n"
- return code
- #Generates the latex documentation code for a class
- def getLatexClassCode(fPath, endLine):
- classname = getClassName(fPath)
- code = ""
- code = "%\n%"+classname.upper()+"\n%\n"
- code += "\\subsection*{"+classname+"}\n Beschreibung der Klasse.\n "
- #code += "\\filecode{1}{"+str(endLine)+"}{"+fPath+"} \n"
- return code
- #Generates the latex documentation code for an entire class
- def generateDoc(fileToOpen):
- sourceFile = open(fileToOpen, "r")
- #0 - 'idle' mode
- #1 - javadoc found, awaiting function line
- #2 - found function, awaiting closing '}'
- mode = 0
- javadocLine = 0
- functionIndentation = 0
- functionString = ""
- currLineNumber = 0
- firstFunctionLineNumber = 0
- latexString = ""
- for currLine in sourceFile:
- currLineNumber+=1
- if(mode==0):
- if("/**" in currLine):
- mode=1
- javadocLine=currLineNumber
- elif(mode==1):
- if("*" in currLine):
- continue
- elif("@" in currLine):
- continue
- elif(isFunctionHeader(currLine)):
- mode=2
- functionIndentation = getIndentation(currLine)
- functionString = currLine
- else:
- mode = 0
- elif(mode==2):
- if("}" in currLine and getIndentation(currLine)==functionIndentation):
- if(firstFunctionLineNumber==0):
- firstFunctionLineNumber = javadocLine
- latexString += getLatexFunctionCode(fileToOpen, functionString, javadocLine, currLineNumber)
- mode=0
- else:
- print("%[AUTODOC] Invalid mode!")
- latexString = getLatexClassCode(fileToOpen, javadocLine-1) + latexString + "\n"
- print(latexString)
- sourceFile.close()
- MainPath = "../Implementierung/uebung06/src/"
- filesWithoutPath = ["SccDotVisitor", "SystemStateChecker","SystemState", "TarjanCopyPasteProvider"]
- files = []
- for f in filesWithoutPath:
- files.append(MainPath+f+".java")
- for fileName in files:
- generateDoc(fileName)
- #generateDoc("../LabyrinthFactory.java")
- #print(getClassName("../hello/my/name/is/dog/LabyrinthFactory.java"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement