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