CbmRoot
Loading...
Searching...
No Matches
CbmQaReportHtmlEngine.cxx
Go to the documentation of this file.
1/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
11
12#include "CbmQaReportFigure.h"
13#include "CbmQaReportHeader.h"
14#include "CbmQaReportSection.h"
15#include "CbmQaReportTable.h"
16#include "CbmQaReportTail.h"
17
24
25#include <algorithm>
26#include <chrono>
27#include <ctime>
28#include <iomanip>
29#include <sstream>
30//#include <regex>
31
32#include <fmt/format.h>
33
34// ---------------------------------------------------------------------------------------------------------------------
35//
36std::string HtmlEngine::FigureBody(const Figure& figure) const
37{
38 std::stringstream out;
39 out << "<figure>\n";
40 for (const auto& plot : figure.GetPlots()) {
41 out << " <embed src=\"" << plot.fsPath << "\" style=\"width:" << kFigureWidth << "\">\n";
42 }
43 if (!figure.GetCaption().empty()) {
44 out << " <figcaption>Fig.[" << figure.GetLabel() << "]: " << figure.GetCaption() << "</figcaption>\n";
45 }
46 out << "</figure>\n";
47
48 return out.str();
49}
50
51// ---------------------------------------------------------------------------------------------------------------------
52//
53std::string HtmlEngine::HeaderBody(const Header& header) const
54{
55 std::stringstream out;
56
57 // TODO: Move these definitions to text-file (?)
58 // Settings
59 out << "<!DOCTYPE html>\n";
60 out << "<html>\n";
61 out << "<style>\n";
62 out << "table {\n boarder-collapse: collapse;\n width: 100%;\n"
63 << "}\n";
64 out << "th, td {\n text-align: " << kTableTextAlign << ";\n padding: " << kTablePadding << "px;\n"
65 << "}\n";
66 out << "tr:nth-child(even) {\n background-color: #EEEEEE;\n}\n";
67 out << "</style>\n";
68 out << "<head>\n";
69 out << " <title>" << header.GetTitle() << "</title>\n";
70 out << "</head>\n\n";
71 out << "<body>\n";
72 out << "<h1>" << header.GetTitle() << "</h1>\n\n";
73 if (!header.GetSubtitle().empty()) {
74 out << "<h2>" << header.GetSubtitle() << "</h2>\n\n";
75 }
76 auto timeNow = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
77 out << "<table style=\"width:1\">\n";
78 out << " <tr>\n";
79 out << " <td>Generated on:</td>\n";
80 out << " <td>" << std::put_time(std::localtime(&timeNow), "%a %d.%m.%Y, %X") << "</td>\n";
81 out << " </tr>\n";
82 out << " <tr>\n";
83 out << " <td>Author:</td>\n";
84 out << " <td>" << header.GetAuthor() << "</td>\n";
85 out << " </tr>\n";
86 if (!header.GetSetup().empty()) {
87 out << " <tr>\n";
88 out << " <td>Setup:</td>\n";
89 out << " <td>" << header.GetSetup() << "</td>\n";
90 out << " </tr>\n";
91 }
92 out << "</table>\n\n";
93
94 return out.str();
95}
96
97// ---------------------------------------------------------------------------------------------------------------------
98//
99std::string HtmlEngine::SectionBody(const Section& section) const
100{
101 std::stringstream out;
102 std::string sSectionTag = fmt::format("h{}", std::min((section.GetLevel() + 2), 6));
103
104 out << '<' << sSectionTag << '>' << section.GetTitle() << "</" << sSectionTag << ">\n\n";
105 for (const auto& pElement : section.GetDaughterElements()) {
106 out << pElement->GetBody(*this) << '\n';
107 }
108 out << '\n';
109 return out.str();
110}
111
112
113// ---------------------------------------------------------------------------------------------------------------------
114//
115std::string HtmlEngine::TableBody(const Table& table) const
116{
117 std::stringstream out;
118 int nRows = table.GetNofRows();
119 int nCols = table.GetNofCols();
120
121 out << "<table>\n";
122 // tabular header
123 if (!table.GetCaption().empty()) {
124 out << " <tablecaption>Table: " << table.GetCaption() << "</tablecaption>\n";
125 }
126 // table header
127 out << " <tr>\n";
128 for (int iCol = 0; iCol < nCols; ++iCol) {
129 out << " <th>" << table.GetColumnTitle(iCol) << "</th>\n";
130 }
131 out << " </tr>\n";
132 // table body
133 for (int iRow = 0; iRow < nRows; ++iRow) {
134 out << " <tr>\n";
135 for (int iCol = 0; iCol < nCols; ++iCol) {
136 out << " <td>" << table(iRow, iCol) << "</td>\n";
137 }
138 out << " </tr>\n";
139 }
140 out << "</table>\n";
141 return out.str();
142}
143
144
145// ---------------------------------------------------------------------------------------------------------------------
146//
147std::string HtmlEngine::TailBody(const Tail&) const
148{
149 std::stringstream out;
150 out << '\n';
151 out << "</body>\n";
152 out << "</html>\n";
153 return out.str();
154}
Base class for the report figure (header)
Base class for the report header (header)
HTML document engine for the cbm::qa::report (source)
Base class for the report section (header)
Base class for the report table (header)
const std::vector< std::shared_ptr< Element > > GetDaughterElements() const
Get daughter elements.
const std::string & GetTitle() const
Gets name.
const std::string & GetLabel() const
Gets label.
Figure in the report.
const std::string & GetCaption() const
Gets caption.
const std::vector< Plot > & GetPlots() const
Gets plot vector.
Header of the report.
const std::string & GetTitle() const
Gets title.
const std::string & GetSubtitle() const
Gets subtitle.
const std::string & GetSetup() const
Gets setup.
const std::string & GetAuthor() const
Gets author.
static constexpr std::string_view kTableTextAlign
Table: align.
static constexpr double kFigureWidth
Figure width [in page width].
std::string TableBody(const Table &table) const override
Creates a body for table.
std::string TailBody(const Tail &tail) const override
Creates a body for tail.
std::string FigureBody(const Figure &figure) const override
Creates a body for figure.
std::string HeaderBody(const Header &header) const override
Creates a body for header.
static constexpr int kTablePadding
Table: rows padding [px].
std::string SectionBody(const Section &section) const override
Creates a body for section.
Section of the report.
int GetLevel() const
Gets level of the section.
Table element in the report.
const std::string & GetColumnTitle(int iCol) const
Gets column title.
int GetNofRows() const
Gets number of rows.
int GetNofCols() const
Gets number of columns.
const std::string & GetCaption() const
Gets caption.
Tail of the report.