Repository navigation
Expand file tree
/
Copy pathissues.cpp
More file actions
310 lines (277 loc) · 9.31 KB
/
Copy pathissues.cpp
File metadata and controls
310 lines (277 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#ifdef _MSC_VER
# define _CRT_SECURE_NO_WARNINGS
#endif
#include "issues.h"
#include "metadata.h"
#include "status.h"
#include <algorithm>
#include <cassert>
#include <istream>
#include <ostream>
#include <string>
#include <stdexcept>
#include <iterator>
#include <fstream>
#include <sstream>
#include <format>
#include <iostream> // eases debugging
#include <ctime>
#include <chrono>
#include <filesystem>
namespace fs = std::filesystem;
namespace {
auto parse_date(std::istream & temp) -> std::chrono::year_month_day {
#if __cpp_lib_chrono >= 201803L
std::chrono::year_month_day date{};
if (temp >> std::chrono::parse(" %d %b %Y", date))
return date;
throw std::runtime_error{"date format error"};
#else
int d;
temp >> d;
if (temp.fail()) {
throw std::runtime_error{"date format error"};
}
std::string month;
temp >> month;
std::map<std::string_view, int> months{
{"Jan", 1}, {"Feb", 2}, {"Mar", 3}, {"Apr", 4}, {"May", 5}, {"Jun", 6},
{"Jul", 7}, {"Aug", 8}, {"Sep", 9}, {"Oct",10}, {"Nov",11}, {"Dec",12}
};
int y{ 0 };
temp >> y;
return std::chrono::year{y}/months[month]/d;
#endif
}
auto report_date_file_last_modified(std::filesystem::path const & filename, lwg::metadata const& meta) -> std::chrono::year_month_day {
using namespace std::chrono;
system_clock::time_point t;
// NB: Cannot use `native()` instead of `string()`, because on Windows that
// would result in std::wstring:
int id = lwg::stoi(filename.filename().stem().string().substr(5));
// Use the Git commit date of the file if available.
if (auto it = meta.git_commit_times.find(id); it != meta.git_commit_times.end())
t = system_clock::from_time_t(it->second);
else {
// Otherwise use the modification time of the file.
auto mtime = fs::last_write_time(filename);
#if __cpp_lib_chrono >= 201803L
t = clock_cast<system_clock>(mtime);
#else
// clock_cast isn't supported, so convert to sys_time manually.
static const auto snow = system_clock::now();
static const auto fnow = fs::file_time_type::clock::now();
t = snow - round<seconds>(fnow - mtime);
#endif
}
return year_month_day(floor<days>(t));
}
// Replace '<' and '>' and '&' with HTML character references.
// This is used to turn backtick-quoted inline code into valid XML/HTML.
std::string escape_special_chars(std::string s) {
static const std::vector<std::pair<char, std::string_view>> subs {
{ '&', "&" }, // this has to come first!
{ '<', "<" },
{ '>', ">" },
};
for (auto [c, r] : subs)
for (auto p = s.find(c); p != s.npos; p = s.find(c, p+r.size()))
s.replace(p, 1, r);
return s;
}
} // close unnamed namespace
auto lwg::parse_issue_from_file(std::string tx, std::string const & filename,
lwg::metadata & meta) -> issue {
auto& section_db = meta.section_db;
struct bad_issue_file : std::runtime_error {
bad_issue_file(std::string const & filename, std::string error_message)
: runtime_error{"Error parsing issue file " + filename + ": " + error_message}
{ }
};
// Replace ```code block``` with valid XML.
for (size_t p = tx.find("\n```\n"); p != tx.npos; p = tx.find("\n```\n", p))
{
size_t p2 = tx.find("\n```\n", p + 5);
if (p2 == tx.npos)
throw bad_issue_file{filename, "Unmatched ``` code block: " + tx.substr(p, 10)};
auto code = "\n<pre><code>" + escape_special_chars(tx.substr(p + 5, p2 - p - 5)) + "\n</code></pre>\n";
tx.replace(p, p2 - p + 5, code);
p += code.size();
}
// Replace inline `code` with valid XML.
for (size_t p = tx.find('`'); p != tx.npos; p = tx.find('`', p))
{
if (tx[p+1] == '`')
{
// Some issues use double backtick for ``quotes like this''.
// We don't want to do anything here.
p += 2;
continue;
}
size_t p2 = tx.find('`', p + 1);
if (p2 > tx.find('\n', p + 1))
{
// Do not treat "`foo\nbar`" as inline code.
// Move to the next backtick and check that one.
p = p2;
continue;
}
// This class attribute is used by the CSS in src/report_generator.cpp
// so that the backticks are still displayed if this occurs inside a
// <pre> element (because that always displays in code font anyway).
auto code = "<code class='backtick'>"
+ escape_special_chars(tx.substr(p + 1, p2 - p - 1))
+ "</code>";
tx.replace(p, p2 - p + 1, code);
p += code.size();
}
// <tt> is obsolete in HTML5, replace with <code>:
for (auto p = tx.find("<tt"); p != tx.npos; p = tx.find("<tt", p+5))
if (tx.at(p+3) == '>' || tx.at(p+3) == ' ')
tx.replace(p, 3, "<code");
for (auto p = tx.find("</tt>"); p != tx.npos; p = tx.find("</tt>", p+7))
tx.replace(p, 5, "</code>");
issue is;
// Get issue number
std::string_view match = "<issue num=\"";
auto k = tx.find(match);
if (k == std::string::npos) {
throw bad_issue_file{filename, "Unable to find issue number"};
}
k += match.size();
auto l = tx.find('\"', k);
auto num = tx.substr(k, l-k);
if (!filename.ends_with(std::format("issue{:0>4}.xml", num)))
std::cerr << "warning: issue number " << num << " in " << filename << " does not match issue number\n";
is.num = lwg::stoi(num);
// Get issue status
match = "status=\"";
k = tx.find(match, l);
if (k == std::string::npos) {
throw bad_issue_file{filename, "Unable to find issue status"};
}
k += match.size();
l = tx.find('\"', k);
is.stat = tx.substr(k, l-k);
// Get issue title
match = "<title>";
k = tx.find(match, l);
if (k == std::string::npos) {
throw bad_issue_file{filename, "Unable to find issue title"};
}
k += match.size();
l = tx.find("</title>", k);
is.title = tx.substr(k, l-k);
// Extract doc_prefix from title
if (is.title[0] == '['
&& is.title.find("[CD]") == std::string::npos) // special case for a few titles
{
std::string::size_type pos = is.title.find(']');
if (pos != std::string::npos)
is.doc_prefix = is.title.substr(1, pos - 1);
// std::cout << is.doc_prefix << '\n';
}
// Get issue sections
match = "<section>";
k = tx.find(match, l);
if (k == std::string::npos) {
throw bad_issue_file{filename, "Unable to find issue section"};
}
k += match.size();
l = tx.find("</section>", k);
while (k < l) {
k = tx.find('\"', k);
if (k >= l) {
break;
}
auto k2 = tx.find('\"', k+1);
if (k2 >= l) {
throw bad_issue_file{filename, "Unable to find issue section"};
}
++k;
section_tag tag;
tag.prefix = is.doc_prefix;
tag.name = tx.substr(k+1, k2 - k - 2);
//std::cout << "lookup tag=\"" << tag.prefix << "\", \"" << tag.name << "\"\n";
is.tags.emplace_back(tag);
if (section_db.find(is.tags.back()) == section_db.end()) {
section_num num{};
// num.num.push_back(100 + 'X' - 'A');
num.prefix = tag.prefix;
num.num.push_back(99);
section_db[is.tags.back()] = num;
}
k = k2;
++k;
}
if (is.tags.empty()) {
throw bad_issue_file{filename, "Unable to find issue section"};
}
// Get submitter
match = "<submitter>";
k = tx.find(match, l);
if (k == std::string::npos) {
throw bad_issue_file{filename, "Unable to find issue submitter"};
}
k += match.size();
l = tx.find("</submitter>", k);
is.submitter = tx.substr(k, l-k);
// Get date
match = "<date>";
k = tx.find(match, l);
if (k == std::string::npos) {
throw bad_issue_file{filename, "Unable to find issue date"};
}
k += match.size();
l = tx.find("</date>", k);
try {
std::istringstream temp{tx.substr(k, l-k)};
is.date = parse_date(temp);
}
catch(std::exception const & ex) {
throw bad_issue_file{filename, "date format error"};
}
// Get modification date
is.mod_date = report_date_file_last_modified(filename, meta);
// Get priority - this element is optional
match = "<priority>";
k = tx.find(match, l);
if (k != std::string::npos) {
k += match.size();
l = tx.find("</priority>", k);
if (l == std::string::npos) {
throw bad_issue_file{filename, "Corrupt 'priority' element: no closing tag"};
}
is.priority = lwg::stoi(tx.substr(k, l-k));
}
// Trim text to <discussion>
k = tx.find("<discussion>", l);
if (k == std::string::npos) {
throw bad_issue_file{filename, "Unable to find issue discussion"};
}
tx.erase(0, k);
// Find out if issue has a proposed resolution
if (is_active(is.stat) or "Pending WP" == is.stat) {
match = "<resolution>";
auto k2 = tx.find(match, 0);
if (k2 == std::string::npos) {
is.has_resolution = false;
}
else {
k2 += match.size();
auto l2 = tx.find("</resolution>", k2);
is.resolution = tx.substr(k2, l2 - k2);
if (is.resolution.length() < 15) {
// Filter small amounts of whitespace between tags, with no actual resolution
is.resolution.clear();
}
// is.has_resolution = l2 - k2 > 15;
is.has_resolution = !is.resolution.empty();
}
}
else {
is.has_resolution = true;
}
is.text = std::move(tx);
return is;
}