HTMLWriter.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 void WriteReportToFile(Stream outputStream)
  26. {
  27. using (StreamWriter writer = new StreamWriter(outputStream, Encoding.UTF8, 4096, true))
  28. {
  29. writer.WriteLine(ReportHeader);
  30. EmitSummary(writer);
  31. writer.Write(ReportFooter);
  32. }
  33. }
  34. protected virtual void EmitSummary(StreamWriter streamWriter)
  35. {
  36. string fileSizeStr = (TotalFileSize / 1048576D).ToString("0.0");
  37. streamWriter.WriteLine($@" <h3>NTERA Compilation report</h3>
  38. <hr />
  39. <p>NTERA v0.X</p>
  40. <p>Processed {fileSizeStr} MB in {TotalFileCount} files<br />
  41. Processed {FunctionCount} functions
  42. </p>
  43. <p>{Errors.Count} errors</p>
  44. <table style=""height: 98px;"" width=""100%"">
  45. <tbody>");
  46. foreach (var error in Errors)
  47. {
  48. EmitError(streamWriter, error.Item1, error.Item2);
  49. }
  50. streamWriter.WriteLine($@" </tbody>
  51. </table>");
  52. }
  53. protected virtual void EmitError(StreamWriter streamWriter, ParserError error, string message)
  54. {
  55. streamWriter.WriteLine($@" <tr>
  56. <td style=""width: 30.6667%;"">{message}</td>
  57. <td style=""width: 66.3333%;"">{error.SymbolMarker} : {error.ErrorMessage}</td>
  58. </tr>");
  59. }
  60. }
  61. }