index.html page is now complete with a list of files and all errors found.

Other changes:
* added a stylesheet and made the html design much better.
* contains links directly to the line in the source file.
This commit is contained in:
Henrik Nilsson 2009-12-11 12:39:39 +01:00
parent b107617d46
commit 89b2118622
1 changed files with 147 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import os
import os.path import os.path
from pygments import highlight from pygments import highlight
from pygments.lexers import CppLexer from pygments.lexers import CppLexer
from pygments.lexers import guess_lexer, guess_lexer_for_filename
from pygments.formatters import HtmlFormatter from pygments.formatters import HtmlFormatter
from xml.sax import parse as xml_parse from xml.sax import parse as xml_parse
from xml.sax import SAXParseException as XmlParseException from xml.sax import SAXParseException as XmlParseException
@ -15,16 +16,139 @@ Turns a cppcheck xml file into a browsable html report along
with syntax highlighted source code. with syntax highlighted source code.
""" """
STYLE_FILE = """
body.body {
font-family: Arial;
font-size: 13px;
background-color: black;
padding: 0px;
margin: 0px;
}
#page-header {
clear: both;
width: 900px;
margin: 20px auto 0px auto;
height: 10px;
border-bottom-width: 2px;
border-bottom-style: solid;
border-bottom-color: #aaaaaa;
}
#page {
width: 860px;
margin: auto;
border-left-width: 2px;
border-left-style: solid;
border-left-color: #aaaaaa;
border-right-width: 2px;
border-right-style: solid;
border-right-color: #aaaaaa;
background-color: White;
padding: 20px;
}
#page-footer {
clear: both;
width: 900px;
margin: auto;
height: 10px;
border-top-width: 2px;
border-top-style: solid;
border-top-color: #aaaaaa;
}
#header {
width: 100%;
height: 70px;
background-image: url(logo.png);
background-repeat: no-repeat;
background-position: left top;
border-bottom-style: solid;
border-bottom-width: thin;
border-bottom-color: #aaaaaa;
}
#menu {
margin-top: 5px;
text-align: left;
float: left;
width: 100px;
height: 300px;
}
#menu > a {
margin-left: 10px;
display: block;
}
#content {
float: left;
width: 720px;
margin: 5px;
padding: 0px 10px 10px 10px;
border-left-style: solid;
border-left-width: thin;
border-left-color: #aaaaaa;
}
#footer {
padding-bottom: 5px;
padding-top: 5px;
border-top-style: solid;
border-top-width: thin;
border-top-color: #aaaaaa;
clear: both;
font-size: 10px;
}
#footer > div {
float: left;
width: 33%;
}
"""
HTML_HEAD = """ HTML_HEAD = """
<html> <html>
<head> <head>
<title>CppCheck - Html report</title> <title>CppCheck - Html report</title>
<link href="style.css" rel="stylesheet" /> <link href="style.css" rel="stylesheet" />
</head> </head>
<body> <body class="body">
<div id="page-header">
&nbsp;
</div>
<div id="page">
<div id="header">
<h1>CppCheck report</h1>
</div>
<div id="menu">
<a href="index.html">Defect list</a>
</div>
<div id="content">
""" """
HTML_FOOTER = """ HTML_FOOTER = """
</div>
<div id="footer">
<div>
CppCheck - a tool for static C/C++ code analysis
</div>
<div>
Internet: <a href="http://cppcheck.sourceforge.net">http://cppcheck.sourceforge.net</a><br/>
Forum: <a href="http://apps.sourceforge.net/phpbb/cppcheck/">http://apps.sourceforge.net/phpbb/cppcheck/</a><br/>
IRC: #cppcheck at irc.freenode.net
</div>
&nbsp;
</div>
&nbsp;
</div>
<div id="page-footer">
&nbsp;
</div>
</body> </body>
</html> </html>
""" """
@ -40,7 +164,7 @@ class CppCheckHandler(XmlContentHandler):
self.errors.append( self.errors.append(
{ {
"file" : attributes["file"], "file" : attributes["file"],
"line" : attributes["line"], "line" : int(attributes["line"]),
"id" : attributes["id"], "id" : attributes["id"],
"severity" : attributes["severity"], "severity" : attributes["severity"],
"msg" : attributes["msg"] "msg" : attributes["msg"]
@ -104,14 +228,19 @@ if __name__ == '__main__':
htmlfile = data["htmlfile"] htmlfile = data["htmlfile"]
errors = data["errors"] errors = data["errors"]
stream = file(os.path.join(source_dir, filename)) lines = []
for error in errors:
lines.append(error["line"])
source_file = os.path.join(source_dir, filename)
stream = file(source_file)
content = stream.read() content = stream.read()
stream.close() stream.close()
htmlFormatter = HtmlFormatter(linenos=True, style='colorful', full=True) htmlFormatter = HtmlFormatter(linenos=True, style='colorful', full=True, hl_lines=lines, lineanchors="line")
stream = file(os.path.join(options.report_dir, htmlfile), "w") stream = file(os.path.join(options.report_dir, htmlfile), "w")
stream.write(HTML_HEAD) stream.write(HTML_HEAD)
stream.write(highlight(content, CppLexer(), htmlFormatter)) stream.write(highlight(content, guess_lexer_for_filename(source_file, ""), htmlFormatter))
stream.write(HTML_FOOTER) stream.write(HTML_FOOTER)
stream.close() stream.close()
@ -122,9 +251,19 @@ if __name__ == '__main__':
print("Creating index.html") print("Creating index.html")
stream = file(os.path.join(options.report_dir, "index.html"), "w") stream = file(os.path.join(options.report_dir, "index.html"), "w")
stream.write(HTML_HEAD) stream.write(HTML_HEAD)
stream.write("<ul>") stream.write("<table>")
stream.write("<tr><th>Line</th><th>Id</th><th>Severity</th><th>Message</th></tr>")
for filename, data in files.iteritems(): for filename, data in files.iteritems():
stream.write("<li><a href=\"%s\">%s</a></li>" % (data["htmlfile"], filename)) stream.write("<tr><td colspan='4'><a href=\"%s\">%s</a></td></tr>" % (data["htmlfile"], filename))
stream.write("</ul>") for error in data["errors"]:
stream.write("<tr><td><a href='%s#line-%d'>%d</a></td><td>%s</td><td>%s</td><td>%s</td></tr>" %
(data["htmlfile"], error["line"], error["line"], error["id"],
error["severity"], error["msg"]))
stream.write("</table>")
stream.write(HTML_FOOTER) stream.write(HTML_FOOTER)
stream.close() stream.close()
print("Creating style.css file")
stream = file(os.path.join(options.report_dir, "style.css"), "w")
stream.write(STYLE_FILE)
stream.close()