HTMLWriter.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using NTERA.Engine.Compiler;
  6. namespace NTERA.Compiler
  7. {
  8. public class HTMLWriter
  9. {
  10. protected virtual string ReportFooter { get; } = " </body>\r\n</html>";
  11. public long TotalFileSize { get; set; }
  12. public int TotalFileCount { get; set; }
  13. public int FunctionCount { get; set; }
  14. public IList<Tuple<ParserError, string>> Errors { get; set; }
  15. public IList<Tuple<ParserError, string>> Warnings { get; set; }
  16. public void WriteReportToFile(Stream outputStream)
  17. {
  18. string ReportHeader = $@"<!DOCTYPE html>
  19. <html lang=""en"" xmlns=""http://www.w3.org/1999/xhtml"">
  20. <head>
  21. <meta charset=""utf-8""/>
  22. <title>NTERA Compilation Report</title>
  23. <style>
  24. {GetCSSStyle()}
  25. </style>
  26. </head>
  27. <body>";
  28. using (StreamWriter writer = new StreamWriter(outputStream, Encoding.UTF8, 4096, true))
  29. {
  30. writer.WriteLine(ReportHeader);
  31. EmitSummary(writer);
  32. writer.Write(ReportFooter);
  33. }
  34. }
  35. protected virtual string GetCSSStyle()
  36. {
  37. return @" body { font-family: sans-serif; }
  38. table { height: 98px; width: 100% }
  39. .message { width: 33.33% }
  40. .symbol { width: 66.67% }";
  41. }
  42. protected virtual void EmitSummary(StreamWriter streamWriter)
  43. {
  44. string fileSizeStr = (TotalFileSize / 1048576D).ToString("0.0");
  45. streamWriter.WriteLine($@" <h3>NTERA Compilation report</h3>
  46. <hr />
  47. <p>NTERA v0.X</p>
  48. <p>Processed {fileSizeStr} MB in {TotalFileCount} files<br />
  49. Processed {FunctionCount} functions
  50. </p>
  51. <p>{Errors.Count} errors, {Warnings.Count} warnings</p>");
  52. streamWriter.WriteLine(@" <h4>Warnings</h4>
  53. <table>
  54. <tbody>");
  55. foreach (var warning in Warnings)
  56. {
  57. EmitError(streamWriter, warning.Item1, warning.Item2);
  58. }
  59. streamWriter.WriteLine(@" </tbody>
  60. </table>
  61. <br />");
  62. streamWriter.WriteLine(@" <h4>Errors</h4>
  63. <table>
  64. <tbody>");
  65. foreach (var error in Errors)
  66. {
  67. EmitError(streamWriter, error.Item1, error.Item2);
  68. }
  69. streamWriter.WriteLine(" </tbody>\r\n </table>");
  70. }
  71. protected virtual void EmitError(StreamWriter streamWriter, ParserError error, string message)
  72. {
  73. streamWriter.WriteLine($@"<tr>
  74. <td class=""message"">{message}</td>
  75. <td class=""symbol"">{error.SymbolMarker} : {error.ErrorMessage}</td>
  76. </tr>");
  77. }
  78. }
  79. }