- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexBuilder.java
More file actions
Latest commit
170 lines (155 loc) · 8.5 KB
/
Copy pathIndexBuilder.java
File metadata and controls
170 lines (155 loc) · 8.5 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
importorg.jsoup.Jsoup;
importorg.jsoup.nodes.Document;
importorg.jsoup.nodes.Element;
importjava.io.File;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.nio.file.Files;
importjava.nio.file.Path;
importjava.nio.file.Paths;
importjava.nio.file.attribute.BasicFileAttributes;
importjava.nio.file.attribute.FileTime;
importjava.util.ArrayList;
importjava.util.Arrays;
@SuppressWarnings("SameParameterValue")
classIndexBuilder {
publicstaticvoidmain(String[] args) throwsIOException {
StringindexPath = "C:\\Projects\\krabcode\\index.html";
Filehtml = newFile(indexPath);
Documentdoc = Jsoup.parse(html, "utf-8");
Elementcontainer = doc.getElementById("autogenerated");
container.text("");
container.append(generateContentFromFolder("C:\\Projects\\krabcode\\content"));
writeAllLines(Paths.get(indexPath), doc.outerHtml().split("/n"));
println("index.html updated successfully");
// TODO upload to forpsi
}
privatestaticStringgenerateContentFromFolder(Stringpath) {
StringBuilderresult = newStringBuilder();
FilemainFolder = newFile(path);
File[] folders = mainFolder.listFiles();
sortFoldersByTimeCreated(folders);
for (Filefolder : folders) {
if (folder.isDirectory()) {
intuniqueFilenamesInFolder = uniqueFilenameCountInFolder(folder);
if (uniqueFilenamesInFolder == 0) {
continue;
}
if (uniqueFilenamesInFolder == 1) {
result.append(createSimpleModal(folder));
} else {
result.append(createCarouselModal(folder));
}
//TODO if folder contains .mp4 but not .webm generate one and add it to the html element too
// FASTER LOADING is paramount!
}
}
returnresult.toString();
}
privatestaticvoidsortFoldersByTimeCreated(File[] folders) {
assertfolders != null;
Arrays.sort(folders, (f1, f2) -> {
try {
returntimeCreated(f2).compareTo(timeCreated(f1));
} catch (IOExceptione) {
e.printStackTrace();
}
return0;
});
}
staticFileTimetimeCreated(Filefile) throwsIOException {
BasicFileAttributesattrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
returnattrs.creationTime();
}
privatestaticStringcreateSimpleModal(Filefolder) {
ArrayList<String> uniqueFilenames = uniqueFilenamesInFolder(folder);
StringvideoExtension = getVideoExtension(folder);
return"<a href=\"#" + folder.getName() + "Modal\" role=\"button\" data-toggle=\"modal\"> <img class=\"thumb\" src=\"content/" + folder.getName() + "/" + uniqueFilenames.get(0) + ".jpg\" alt=\"" + folder.getName() + "\"/> </a>\n" +
" <div id=\"" + folder.getName() + "Modal\" class=\"modal fade\" role=\"dialog\">\n" +
" <div class=\"modal-dialog modal-dialog-centered modal-lg\">\n" +
" <div class=\"modal-content\">\n" +
" <div class=\"modal-body\">\n" +
" <video autoplay controls loop muted poster=\"content/" + folder.getName() + "/" + uniqueFilenames.get(0) + ".jpg\" class=\"embed-responsive embed-responsive-1by1 main-video\">\n" +
" <source src=\"content/" + folder.getName() + "/" + uniqueFilenames.get(0) + "." + videoExtension + "\" type=\"video/" + videoExtension + "\">\n" +
" </video>\n" +
" </div>\n" +
" </div>\n" +
" </div>\n" +
" </div>";
}
@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
privatestaticStringcreateCarouselModal(Filefolder) {
ArrayList<String> uniqueFilenames = uniqueFilenamesInFolder(folder);
StringvideoExtension = getVideoExtension(folder);
StringBuildercarouselModal = newStringBuilder();
carouselModal.append("<a href=\"#" + folder.getName() + "Modal\" role=\"button\" data-toggle=\"modal\"> <img class=\"thumb\" src=\"content/" + folder.getName() + "/" + uniqueFilenames.get(0) + ".jpg\" alt=\"" + folder.getName() + "\"/> </a>\n" +
" <div id=\"" + folder.getName() + "Modal\" class=\"modal fade\" role=\"dialog\">\n" +
" <div class=\"modal-dialog modal-dialog-centered modal-lg\" role=\"document\">\n" +
" <div class=\"modal-content\">\n" +
" <div class=\"modal-body\">\n" +
" <div id=\"" + folder.getName() + "Carousel\" class=\"carousel carousel-fade\" data-ride=\"carousel\" data-interval=\"false\" data-keyboard=\"false\">\n" +
" <div class=\"carousel-inner\" role=\"listbox\">\n");
carouselModal.append("<div class=\"carousel-item active\">\n" +
" <video autoplay controls loop muted poster=\"content/" + folder.getName() + "/" + uniqueFilenames.get(0) + ".jpg\" class=\"embed-responsive embed-responsive-1by1 main-video\">\n" +
" <source src=\"content/" + folder.getName() + "/" + uniqueFilenames.get(0) + "." + videoExtension + "\" type=\"video/" + videoExtension + "\">\n" +
" </video>\n" +
" </div>\n");
for (inti = 1; i < uniqueFilenames.size(); i++) {
carouselModal.append("<div class=\"carousel-item\">\n" +
" <video autoplay controls loop muted poster=\"content/" + folder.getName() + "/" + uniqueFilenames.get(0) + ".jpg\" class=\"embed-responsive embed-responsive-1by1 main-video\">\n" +
" <source src=\"content/" + folder.getName() + "/" + uniqueFilenames.get(i) + "." + videoExtension + "\" type=\"video/" + videoExtension + "\">\n" +
" </video>\n" +
"</div>\n");
}
carouselModal.append("</div>" +
" <a class=\"carousel-control-prev\" href=\"#" + folder.getName() + "Carousel\" role=\"button\" data-slide=\"prev\">\n" +
" <span class=\"carousel-control-prev-icon\" aria-hidden=\"true\"></span>\n" +
" <span class=\"sr-only\">Previous</span>\n" +
" </a>\n" +
" <a class=\"carousel-control-next\" href=\"#" + folder.getName() + "Carousel\" role=\"button\" data-slide=\"next\">\n" +
" <span class=\"carousel-control-next-icon\" aria-hidden=\"true\"></span>\n" +
" <span class=\"sr-only\">Next</span>\n" +
" </a>\n" +
"\n" +
" </div>\n" +
" </div>\n" +
" </div>\n" +
" </div>\n" +
" </div>");
returncarouselModal.toString();
}
privatestaticStringgetVideoExtension(Filefolder) {
for (Filef : folder.listFiles()) {
if (f.getName().contains("mp4")) {
return"mp4";
}
if (f.getName().contains("webm")) {
return"webm";
}
}
returnnull;
}
privatestaticintuniqueFilenameCountInFolder(Filefolder) {
returnuniqueFilenamesInFolder(folder).size();
}
privatestaticArrayList<String> uniqueFilenamesInFolder(Filefolder) {
ArrayList<String> knownFilenames = newArrayList<String>();
for (Filef : folder.listFiles()) {
StringfilenameWithoutExtension = f.getName().split("\\.")[0];
if (!knownFilenames.contains(filenameWithoutExtension)) {
knownFilenames.add(filenameWithoutExtension);
}
}
returnknownFilenames;
}
privatestaticvoidwriteAllLines(Pathpath, String[] gradleFile) throwsIOException {
FileWriterfw = newFileWriter(path.toFile());
for (Strings : gradleFile) {
fw.write(s + System.lineSeparator());
}
fw.close();
}
staticvoidprintln(Stringline) {
System.out.println(line);
}
}