HTMLWriter.cs 2.2 KB

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