Belle II Software  release-05-02-19
HTML.cc
1 #include <framework/utilities/HTML.h>
2 
3 #include <boost/algorithm/string/replace.hpp>
4 #include <regex>
5 
6 #include <TMatrixFBase.h>
7 #include <TMatrixD.h>
8 #include <TVector3.h>
9 
10 #include <framework/gearbox/Unit.h>
11 
12 #include <iomanip>
13 #include <sstream>
14 
15 using namespace Belle2;
16 using namespace boost::algorithm;
17 
18 std::string HTML::getString(const TMatrixFBase& matrix, int precision, bool color)
19 {
20  const int nr = matrix.GetNrows();
21  const int nc = matrix.GetNcols();
22 
23  TMatrixD matrix_double(nr, nc);
24  for (int ir = 0; ir < nr; ir++)
25  for (int ic = 0; ic < nc; ic++)
26  matrix_double(ir, ic) = matrix(ir, ic);
27  return getString(matrix_double, precision, color);
28 }
29 
30 std::string HTML::getString(const TMatrixDBase& matrix, int precision, bool color)
31 {
32  std::stringstream stream;
33  stream.precision(precision);
34 
35  stream << "<table padding=0>";
36  const double max = matrix.Max();
37  const double min = matrix.Min();
38  for (int i = 0; i < matrix.GetNrows(); i++) {
39  stream << "<tr>";
40  for (int k = 0; k < matrix.GetNcols(); k++) {
41  const double value = matrix(i, k);
42  stream << "<td align=right";
43  if (color and value != 0.0) {
44  //0 white, min cyan, max red
45  int b = 255;
46  int r = 255;
47  int g = 255;
48  if (value > 0)
49  b = g = 255 - 180 * value / ((value > 0) ? max : min);
50  else
51  r = 255 - 200 * value / ((value > 0) ? max : min);
52 
53  stream << " bgcolor=" << std::setfill('0') << std::hex;
54  stream << std::setw(2) << r;
55  stream << std::setw(2) << g;
56  stream << std::setw(2) << b;
57  stream << std::dec << std::setfill(' ');
58  }
59  stream << ">" << std::setw(11) << value;
60  stream << "</td>";
61  }
62  stream << "</tr>";
63  }
64  stream << "</table>";
65  stream << "<br>";
66 
67  return stream.str();
68 }
69 
70 std::string HTML::getString(const TVector3& vec, int precision)
71 {
72  std::stringstream stream;
73  stream.precision(precision);
74 
75  stream << std::fixed << "(" << vec.x() << ", " << vec.y() << ", " << vec.z() << ")";
76  return stream.str();
77 }
78 
79 std::string HTML::getStringConvertToUnit(const TVector3& vec, int precision, const std::string& unitType)
80 {
81  std::stringstream stream;
82  stream.precision(precision);
83  std::string displayedUnitType = unitType;
84 
85  if (unitType == "um")
86  displayedUnitType = "\x0b5m";
87 
88 
89  stream << std::fixed << "(" << Unit::convertValueToUnit(vec.x(), unitType) << ", "
90  << Unit::convertValueToUnit(vec.y(), unitType) << ", " << Unit::convertValueToUnit(vec.z(), unitType)
91  << ") " << displayedUnitType;
92 
93 
94  return stream.str();
95 }
96 std::string HTML::chooseUnitOfLength(const TVector3& vec)
97 {
98  double xyz [3];
99  std::string unitType;
100  vec.GetXYZ(xyz);
101  double max = 0;
102 
103  for (auto entry : xyz)
104  if (std::abs(entry) > max)
105  max = std::abs(entry);
106 
107  // choose specific range for that the unit is useful
108  if (max < 0.1)
109  unitType = "um";
110  else
111  unitType = "cm";
112  return unitType;
113 }
114 
115 std::string HTML::getHexDump(const int* buf, int length)
116 {
117  std::stringstream stream;
118  stream << "<small><tt>";
119  char str[10];
120  //print everything in blocks of 4 bytes, 4 blocks per line
121  for (int i = 0; i < length; i++) {
122  //byte = 2letters
123  //4byte = 8 letters
124  snprintf(str, sizeof(str), "%08x ", buf[i]);
125  stream << str;
126  }
127  stream << "</tt></small>";
128  return stream.str();
129 }
130 
131 
132 std::string HTML::htmlToPlainText(const std::string& html)
133 {
134  std::string tmp = html;
135  //conversions to plaintext
136  replace_all(tmp, "<br>", "\n");
137  replace_all(tmp, "</p>", "\n");
138  replace_all(tmp, "</tr>", "\n");
139  replace_all(tmp, "&nbsp;", " ");
140 
141  //remove non-ascii \mu
142  replace_all(tmp, "\x0b5", "u");
143 
144  //remove all unknown tags (non-greedy match)
145  const static std::regex tagRegex("<.*?>");
146  tmp = std::regex_replace(tmp, tagRegex, "");
147 
148  tmp = unescape(tmp);
149 
150  return tmp;
151 }
152 
153 std::string HTML::escape(const std::string& str)
154 {
155  std::string tmp = str;
156  replace_all(tmp, "&", "&amp;"); //must be first
157  replace_all(tmp, ">", "&gt;");
158  replace_all(tmp, "<", "&lt;");
159  replace_all(tmp, "\"", "&quot;");
160  return tmp;
161 }
162 
163 std::string HTML::unescape(const std::string& str)
164 {
165  std::string tmp = str;
166  //replace entities (important ones at least)
167  replace_all(tmp, "&quot;", "\"");
168  replace_all(tmp, "&gt;", ">");
169  replace_all(tmp, "&lt;", "<");
170  replace_all(tmp, "&amp;", "&"); //must be last
171  return tmp;
172 }
Belle2::HTML::htmlToPlainText
std::string htmlToPlainText(const std::string &html)
Reformat given HTML string into terminal-friendly plain text.
Definition: HTML.cc:132
Belle2::HTML::chooseUnitOfLength
std::string chooseUnitOfLength(const TVector3 &vec)
get a string with a unit type to convert a vector, so that it is easily readable.
Definition: HTML.cc:96
Belle2::HTML::getHexDump
std::string getHexDump(const int *buf, int length)
Create hexdump of given buffer.
Definition: HTML.cc:115
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::HTML::getString
std::string getString(const TMatrixFBase &matrix, int precision=2, bool color=true)
get HTML table representing a matrix.
Definition: HTML.cc:18
Belle2::Unit::convertValueToUnit
static double convertValueToUnit(double value, const std::string &unitString)
Converts a floating point value from the standard framework unit to the given unit.
Definition: UnitConst.cc:146
Belle2::HTML::escape
std::string escape(const std::string &str)
Convert &, <, > etc.
Definition: HTML.cc:153
Belle2::HTML::getStringConvertToUnit
std::string getStringConvertToUnit(const TVector3 &vec, int precision=2, const std::string &unitType="cm")
get a string with vector coordinates: (x, y, z).
Definition: HTML.cc:79
Belle2::HTML::unescape
std::string unescape(const std::string &str)
inverse of escape()
Definition: HTML.cc:163