def main(): usage = """usage: %prog [options] source out_dir Converts a source code file to HTML in the output directory.""" print('Cmd: %s' % ' '.join(sys.argv)) optParser = OptionParser(usage, version='%prog ' + __version__) optParser.add_option( "-l", "--loglevel", type="int", dest="loglevel", default=30, help= "Log Level (debug=10, info=20, warning=30, error=40, critical=50) [default: %default]" ) opts, args = optParser.parse_args() clkStart = time.clock() # Initialise logging etc. logging.basicConfig(level=opts.loglevel, format='%(asctime)s %(levelname)-8s %(message)s', stream=sys.stdout) if len(args) != 2: optParser.print_help() optParser.error("No arguments!") return 1 TokenCss.writeCssToDir(args[1]) ItuToHtml(args[0], args[1]) clkExec = time.clock() - clkStart print('CPU time = %8.3f (S)' % clkExec) print('Bye, bye!') return 0
def _writeDirectoryIndexHTML(theInDir, theOutDir, titlePathTupleS, theJobSpec): """Writes a super index.html when a directory has been processed. titlePathTuples is a list of: PpProcessResult(ituPath, indexPath, tuIndexFileName(ituPath)).""" indexPath = os.path.join(theOutDir, 'index.html') TokenCss.writeCssToDir(theOutDir) _prefixOut, titlePathTupleS = _removeCommonPrefixFromResults( titlePathTupleS) # Write the HTML with XmlWrite.XhtmlStream(indexPath, mustIndent=INDENT_ML) as myS: with XmlWrite.Element(myS, 'head'): with XmlWrite.Element( myS, 'link', { 'href': TokenCss.TT_CSS_FILE, 'type': "text/css", 'rel': "stylesheet", }): pass with XmlWrite.Element(myS, 'title'): myS.characters('CPIP Processing') with XmlWrite.Element(myS, 'body'): with XmlWrite.Element(myS, 'h1'): myS.characters('CPIP Directory Processing in output location: %s' \ % theOutDir) # List of links to TU index pages with XmlWrite.Element(myS, 'h2'): myS.characters('Files Processed as Translation Units:') with XmlWrite.Element(myS, 'p'): myS.characters('Input: ') with XmlWrite.Element(myS, 'tt'): myS.characters(theInDir) with XmlWrite.Element(myS, 'ul'): for title, indexHTMLPath, fileIndexHTMLPath in titlePathTupleS: if indexHTMLPath is not None \ and fileIndexHTMLPath is not None: indexHTMLPath = os.path.relpath( indexHTMLPath, theOutDir, ) # Redirect to page that describes actual file indexHTMLPath = os.path.join( os.path.dirname(indexHTMLPath), fileIndexHTMLPath) with XmlWrite.Element(myS, 'li'): with XmlWrite.Element(myS, 'tt'): if indexHTMLPath is not None: with XmlWrite.Element(myS, 'a', {'href': indexHTMLPath}): myS.characters(title) else: myS.characters('%s [FAILED]' % title) _writeCommandLineInvocationToHTML(myS, theJobSpec)
def processTuToHtml(theLex, theHtmlPath, theTitle, theCondLevel, theIdxPath, incItuAnchors=True): """Processes the PpLexer and writes the tokens to the HTML file. *theHtmlPath* The path to the HTML file to write. *theTitle* A string to go into the <title> element. *theCondLevel* The Conditional level to pass to theLex.ppTokens() *theIdxPath* Path to link back to the index page. *incItuAnchors* boolean, if True will write anchors for lines in the ITU that are in this TU. If True then setItuLineNumbers returned is likely to be non-empty. Returns a pair of (PpTokenCount.PpTokenCount(), set(int)) The latter is a set of integer line numbers in the ITU that are in the TU, these line numbers with have anchors in this HTML file of the form: <a name="%d" />.""" if not os.path.exists(os.path.dirname(theHtmlPath)): os.makedirs(os.path.dirname(theHtmlPath)) LINE_FIELD_WIDTH = 8 LINE_BREAK_LENGTH = 100 # Make a global token counter (this could be got from the file include graph # but this is simpler. myTokCntr = PpTokenCount.PpTokenCount() # Write CSS TokenCss.writeCssToDir(os.path.dirname(theHtmlPath)) # Set of active lines of the ITU (only) that made it into the TU setItuLineNumbers = set() # Process the TU with XmlWrite.XhtmlStream(theHtmlPath, mustIndent=cpip.INDENT_ML) as myS: with XmlWrite.Element(myS, 'head'): with XmlWrite.Element( myS, 'link', { 'href' : TokenCss.TT_CSS_FILE, 'type' : "text/css", 'rel' : "stylesheet", } ): pass with XmlWrite.Element(myS, 'title'): myS.characters(theTitle) myIntId = 0 with XmlWrite.Element(myS, 'body'): with XmlWrite.Element(myS, 'h1'): myS.characters('Translation Unit: %s' % theLex.tuFileId) with XmlWrite.Element(myS, 'p'): myS.characters("""An annotated version of the translation unit with minimal whitespace. Indentation is according to the depth of the #include stack. Line numbers are linked to the original source code. """) with XmlWrite.Element(myS, 'p'): myS.characters("""Highlighted filenames take you forward to the next occasion in the include graph of the file being pre-processed, in this case: %s""" % theLex.tuFileId) linkToIndex(myS, theIdxPath) with XmlWrite.Element(myS, 'pre'): # My copy of the file stack for annotating the output myFileStack = [] indentStr = '' colNum = 1 for t in theLex.ppTokens(incWs=True, minWs=True, condLevel=theCondLevel): #print t logging.debug('Token: %s', str(t)) myTokCntr.inc(t, isUnCond=t.isUnCond, num=1) if t.isUnCond: # Adjust the prefix depending on how deep we are in the file stack myIntId = _adjustFileStack(myS, theLex.fileStack, myFileStack, myIntId) indentStr = '.' * len(myFileStack) # Write the token if t.tt == 'whitespace': if t.t != '\n' and colNum > LINE_BREAK_LENGTH: myS.characters(' \\\n') myS.characters(indentStr) myS.characters(' ' * (LINE_FIELD_WIDTH + 8)) colNum = 1 else: # Line break myS.characters(t.t) ## NOTE: This is removed as the cost to the ## browser is enormous. ## Set a marker #with XmlWrite.Element(myS, # 'a', # {'name' : myTuI.add(theLex.tuIndex)}): # pass else: if colNum > LINE_BREAK_LENGTH: # Force a break myS.characters('\\\n') myS.characters(indentStr) myS.characters(' ' * (LINE_FIELD_WIDTH + 8)) colNum = 1 with XmlWrite.Element(myS, 'span', {'class' : TokenCss.retClass(t.tt)}): myS.characters(t.t) colNum += len(t.t) if t.t == '\n' and len(myFileStack) != 0: # Write an ID for the ITU only if incItuAnchors and len(myFileStack) == 1: with XmlWrite.Element(myS, 'a', {'name' : '%d' % theLex.lineNum}): setItuLineNumbers.add(theLex.lineNum) # Write the line prefix myS.characters(indentStr) myS.characters('[') myS.characters(' ' * \ (LINE_FIELD_WIDTH - len('%d' % theLex.lineNum))) HtmlUtils.writeHtmlFileLink( myS, theLex.fileName, theLex.lineNum, '%d' % theLex.lineNum, theClass=None, ) myS.characters(']: ') colNum = 1 linkToIndex(myS, theIdxPath) return myTokCntr, setItuLineNumbers
def processMacroHistoryToHtml(theLex, theHtmlPath, theItu, theIndexPath): """Write out the macro history from the PpLexer as HTML. Returns a map of: {identifier : [(fileId, lineNum, href_name), ...], ...} which can be used by src->html generator for providing links to macro pages.""" TokenCss.writeCssToDir(os.path.dirname(theHtmlPath)) # Grab the environment myEnv = theLex.macroEnvironment # Write the index page that links to the referenced and non-referenced pages with XmlWrite.XhtmlStream( os.path.join(theHtmlPath, _macroHistoryIndexName(theItu))) as myS: with XmlWrite.Element(myS, 'head'): with XmlWrite.Element( myS, 'link', { 'href': TokenCss.TT_CSS_FILE, 'type': "text/css", 'rel': "stylesheet", }): pass with XmlWrite.Element(myS, 'title'): myS.characters('Macro Environment for: %s' % theItu) with XmlWrite.Element(myS, 'body'): with XmlWrite.Element(myS, 'h1'): myS.characters('Macro Environment for: %s' % theItu) with XmlWrite.Element(myS, 'p'): myS.characters( """A page describing the macros encountered during pre-processing, their definition, where defined, where used and their dependencies. All linked to the source code.""") _linkToIndex(myS, theIndexPath) # Write the TOC and get the sorted list all the macros in alphabetical order _writeTocMacros(myS, myEnv, isReferenced=True, filePrefix=_macroHistoryRefName(theItu)) _writeTocMacros(myS, myEnv, isReferenced=False, filePrefix=_macroHistoryNorefName(theItu)) # Write back link _linkToIndex(myS, theIndexPath) # Write the page for referenced macros with XmlWrite.XhtmlStream( os.path.join(theHtmlPath, _macroHistoryRefName(theItu))) as myS: with XmlWrite.Element(myS, 'head'): with XmlWrite.Element( myS, 'link', { 'href': TokenCss.TT_CSS_FILE, 'type': "text/css", 'rel': "stylesheet", }): pass with XmlWrite.Element(myS, 'title'): myS.characters('Referenced Macros for: %s' % theItu) with XmlWrite.Element(myS, 'body'): with XmlWrite.Element(myS, 'h1'): myS.characters('Referenced Macros for: %s' % theItu) _linkToIndex(myS, theIndexPath) _writeTocMacros(myS, myEnv, isReferenced=True, filePrefix=None) _writeSectionOnMacroHistory(myS, myEnv, [ PpLexer.UNNAMED_FILE_NAME, ], theHtmlPath, theItu, isReferenced=True) _linkToIndex(myS, theIndexPath) # Write the page for non-referenced macros with XmlWrite.XhtmlStream( os.path.join(theHtmlPath, _macroHistoryNorefName(theItu))) as myS: with XmlWrite.Element(myS, 'head'): with XmlWrite.Element( myS, 'link', { 'href': TokenCss.TT_CSS_FILE, 'type': "text/css", 'rel': "stylesheet", }): pass with XmlWrite.Element(myS, 'title'): myS.characters('Non-Referenced Macros for: %s' % theItu) with XmlWrite.Element(myS, 'body'): with XmlWrite.Element(myS, 'h1'): myS.characters('Non-Referenced Macros for: %s' % theItu) _linkToIndex(myS, theIndexPath) _writeTocMacros(myS, myEnv, isReferenced=False, filePrefix=None) _writeSectionOnMacroHistory(myS, myEnv, [ PpLexer.UNNAMED_FILE_NAME, ], theHtmlPath, theItu, isReferenced=False) _linkToIndex(myS, theIndexPath) retVal = _retMacroIdHrefNames(myEnv, theItu) # print('retVal', retVal) # print('indexPath', indexPath) return retVal, _macroHistoryIndexName(theItu)