cppcheck-htmlreport now takes a parameter for source code encoding to support non ascii characters.
Also present a nicer error message if highlighting fail for a file.
This commit is contained in:
parent
f6c24f9070
commit
628f4ad6e7
|
@ -199,6 +199,7 @@ if __name__ == '__main__':
|
||||||
parser.add_option("--file", dest="file", help="The cppcheck xml output file to read defects from. Default is reading from stdin.")
|
parser.add_option("--file", dest="file", help="The cppcheck xml output file to read defects from. Default is reading from stdin.")
|
||||||
parser.add_option("--report-dir", dest="report_dir", help="The directory where the html report content is written.")
|
parser.add_option("--report-dir", dest="report_dir", help="The directory where the html report content is written.")
|
||||||
parser.add_option("--source-dir", dest="source_dir", help="Base directory where source code files can be found.")
|
parser.add_option("--source-dir", dest="source_dir", help="Base directory where source code files can be found.")
|
||||||
|
parser.add_option("--source-encoding", dest="source_encoding", help="Encoding of source code.", default=None)
|
||||||
|
|
||||||
# Parse options and make sure that we have an output directory set.
|
# Parse options and make sure that we have an output directory set.
|
||||||
options, args = parser.parse_args()
|
options, args = parser.parse_args()
|
||||||
|
@ -248,30 +249,33 @@ if __name__ == '__main__':
|
||||||
# file that contains one or more errors.
|
# file that contains one or more errors.
|
||||||
print("Processing errors")
|
print("Processing errors")
|
||||||
for filename, data in files.iteritems():
|
for filename, data in files.iteritems():
|
||||||
htmlfile = data["htmlfile"]
|
try:
|
||||||
errors = data["errors"]
|
htmlfile = data["htmlfile"]
|
||||||
|
errors = data["errors"]
|
||||||
|
|
||||||
lines = []
|
lines = []
|
||||||
for error in errors:
|
for error in errors:
|
||||||
lines.append(error["line"])
|
lines.append(error["line"])
|
||||||
|
|
||||||
source_file = os.path.join(source_dir, filename)
|
source_file = os.path.join(source_dir, filename)
|
||||||
if not os.path.isfile(source_file):
|
if not os.path.isfile(source_file):
|
||||||
sys.stderr.write("ERROR: Source file '%s' not found.\n" % source_file)
|
sys.stderr.write("ERROR: Source file '%s' not found.\n" % source_file)
|
||||||
continue
|
continue
|
||||||
stream = file(source_file)
|
stream = file(source_file)
|
||||||
content = stream.read()
|
content = stream.read()
|
||||||
stream.close()
|
stream.close()
|
||||||
|
|
||||||
htmlFormatter = AnnotateCodeFormatter(linenos=True, style='colorful', hl_lines=lines, lineanchors="line")
|
htmlFormatter = AnnotateCodeFormatter(linenos=True, style='colorful', hl_lines=lines, lineanchors="line", encoding=options.source_encoding)
|
||||||
htmlFormatter.errors = errors
|
htmlFormatter.errors = errors
|
||||||
stream = file(os.path.join(options.report_dir, htmlfile), "w")
|
stream = file(os.path.join(options.report_dir, htmlfile), "w")
|
||||||
stream.write(HTML_HEAD % (options.title, htmlFormatter.get_style_defs(".highlight"), options.title))
|
stream.write(HTML_HEAD % (options.title, htmlFormatter.get_style_defs(".highlight"), options.title))
|
||||||
stream.write(highlight(content, guess_lexer_for_filename(source_file, ""), htmlFormatter))
|
stream.write(highlight(content, guess_lexer_for_filename(source_file, ""), htmlFormatter))
|
||||||
stream.write(HTML_FOOTER)
|
stream.write(HTML_FOOTER)
|
||||||
stream.close()
|
stream.close()
|
||||||
|
|
||||||
print(" " + filename)
|
print(" " + filename)
|
||||||
|
except Exception, message:
|
||||||
|
print("ERROR: Filename: %s, %s" % (filename, message))
|
||||||
|
|
||||||
# Generate a master index.html file that will contain a list of
|
# Generate a master index.html file that will contain a list of
|
||||||
# all the errors created.
|
# all the errors created.
|
||||||
|
|
Loading…
Reference in New Issue