1 /**
2 Copyright: Copyright (c) 2021, Joakim Brännström. All rights reserved.
3 License: MPL-2
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 
6 This Source Code Form is subject to the terms of the Mozilla Public License,
7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
8 one at http://mozilla.org/MPL/2.0/.
9 */
10 module dextool.plugin.mutate.backend.report.html.utility;
11 
12 import std.datetime : SysTime, Duration, dur;
13 
14 import dextool.plugin.mutate.backend.report.html.constants;
15 import dextool.plugin.mutate.backend.type : TestCase;
16 import dextool.type : Path;
17 import arsd.dom : Element;
18 
19 @safe:
20 
21 /// Convert a file path to a html represented path.
22 string pathToHtml(string p) {
23     import std.algorithm : joiner;
24     import std.path : pathSplitter, buildPath;
25     import std.utf : toUTF8;
26 
27     return p.pathSplitter.joiner("__").toUTF8;
28 }
29 
30 Path pathToHtmlLink(string p) {
31     return Path(pathToHtml(p) ~ Html.ext);
32 }
33 
34 string removeAllNonAlphaNum(string s) {
35     import std.array : appender;
36     import std.ascii : isASCII;
37     import std.conv : text;
38     import std.uni : byCodePoint, isAlphaNum;
39 
40     auto result = appender!(char[])();
41     foreach (c; s.byCodePoint) {
42         if (isAlphaNum(c) && isASCII(c)) {
43             result.put(c.text);
44         } else {
45             result.put("_");
46         }
47     }
48     return result.data.idup;
49 }
50 
51 Path testCaseToHtmlLink(TestCase tc) {
52     return Path(tc.name.removeAllNonAlphaNum.pathToHtmlLink);
53 }
54 
55 string toShortDate(SysTime ts) {
56     import std.format : format;
57 
58     return format("%04s-%02s-%02s", ts.year, cast(ushort) ts.month, ts.day);
59 }
60 
61 string toShortTime(Duration d) {
62     import std.conv : to;
63     import std.format : format;
64 
65     immutable Units = ["days", "hours", "minutes", "seconds"];
66 
67     static foreach (UnitIdx; 0 .. Units.length - 1) {
68         {
69             if (d.total!(Units[UnitIdx]) > 0) {
70                 enum unit0 = Units[UnitIdx];
71                 enum unit1 = Units[UnitIdx + 1];
72                 return format("%s%s %s%s", d.total!unit0, unit0[0], (d - d.total!unit0
73                         .dur!unit0).total!unit1, unit1[0]);
74             }
75         }
76     }
77 
78     return format!"%ss"(d.total!"seconds");
79 }
80 
81 void generatePopupHelp(Element e, string infoText) @trusted {
82     e.addChild("div", "i").addClass("popup-help").addChild("span", infoText)
83         .addClass("popup-help-content");
84 }