Skip to content

Commit

Permalink
add resource manager usage
Browse files Browse the repository at this point in the history
  • Loading branch information
efekos committed Jan 30, 2024
1 parent b16a295 commit 392b94c
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 80 deletions.
3 changes: 0 additions & 3 deletions src/main/java/dev/efekos/pg/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public class Main {
*/
private static String MAIN_PATH;

public static String FOOTER_ELEMENT;

public static final Logger LOGGER = new Logger();
public static final DebugLogger DEBUG_LOGGER = new DebugLogger();

Expand Down Expand Up @@ -72,7 +70,6 @@ public static void main(String[] args) throws Exception {
List<String> list = Arrays.asList(args);
isDebug = list.contains("--debug");
DEBUG_LOGGER.setEnabled(isDebug);
FOOTER_ELEMENT = readStringResource("/site/html/template/footer.html");
MAIN_PATH = System.getProperty("user.dir");
System.out.println("Hello World!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import dev.efekos.pg.Main;
import dev.efekos.pg.data.schema.Certificate;
import dev.efekos.pg.data.schema.GeneralInfo;
import dev.efekos.pg.resource.ResourceManager;
import dev.efekos.pg.resource.Resources;

import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -41,7 +43,7 @@ public void generateActualFile(GeneralInfo info, List<Certificate> certificates)

List<String> elementList = certificates.stream().map(certificate -> "<a href=\"./certificate/" + makeIdForLink(certificate.getDisplay().getTitle()) + ".html\"><img class=\"certificate-image-small\" src=\"./images/certificate/" + certificate.getDisplay().getImage() + "\" alt=\"" + certificate.getDisplay().getTitle() + "\" />").toList();

String fileString = Main.readStringResource("/site/html/certificates.html")
String fileString = ResourceManager.getResource(Resources.HTML_CERTIFICATES_PAGE)
.replaceAll("%%name%%", info.getName())
.replaceAll("%%title%%", info.getTitle())
.replaceAll("%%images%%", String.join("\n", elementList));
Expand Down Expand Up @@ -74,7 +76,7 @@ public void copyImages(List<Certificate> certificates) throws IOException {

public void generateSinglePages(GeneralInfo info, List<Certificate> certificates) throws IOException {
Main.LOGGER.info("Generating single certificate files");
String resourceFile = Main.readStringResource("/site/html/single_certificate.html");
String resourceFile = ResourceManager.getResource(Resources.HTML_SINGLE_CERTIFICATE);

Files.createDirectory(Path.of(binPath, "certificate"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import dev.efekos.pg.data.schema.EducationEntry;
import dev.efekos.pg.data.schema.EducationInfo;
import dev.efekos.pg.data.schema.GeneralInfo;
import dev.efekos.pg.resource.ResourceManager;
import dev.efekos.pg.resource.Resources;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -33,8 +35,6 @@ public EducationPageGenerator(String binPath) {
this.binPath = binPath;
}

private final String EDUCATION_ENTRY_ELEMENT = Main.readStringResource("/site/html/template/education_entry.html");

public void generate(GeneralInfo generalInfo, EducationInfo info) throws IOException {
Main.LOGGER.info("Generating file: education.html");
generateElements(info);
Expand All @@ -44,7 +44,7 @@ public void generate(GeneralInfo generalInfo, EducationInfo info) throws IOExcep

private void generateFile(GeneralInfo generalInfo) throws IOException {
Main.DEBUG_LOGGER.info("Generating file");
String file = Main.readStringResource("/site/html/education.html")
String file = ResourceManager.getResource(Resources.HTML_EDUCATION_PAGE)
.replaceAll("%%entries%%", String.join("", elementsGenerated))
.replaceAll("%%name%%", generalInfo.getName());

Expand All @@ -61,7 +61,7 @@ private void generateElements(EducationInfo info) {

elementsGenerated.clear();
for (EducationEntry entry : entries) {
String element = EDUCATION_ENTRY_ELEMENT.replaceAll("%%pname%%", entry.getTitle())
String element = ResourceManager.getResource(Resources.HTML_EDUCATION_ENTRY_TEMPLATE).replaceAll("%%pname%%", entry.getTitle())
.replaceAll("%%ptype%%", entry.getType().getDisplay())
.replaceAll("%%plocation%%", entry.getLocation())
.replaceAll("%%pstart%%", entry.getStart().toString())
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/dev/efekos/pg/output/ExperiencePageGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import dev.efekos.pg.data.schema.ExperienceEntry;
import dev.efekos.pg.data.schema.ExperienceInfo;
import dev.efekos.pg.data.schema.GeneralInfo;
import dev.efekos.pg.resource.ResourceManager;
import dev.efekos.pg.resource.Resources;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -33,10 +35,6 @@ public ExperiencePageGenerator(String binPath) {
this.binPath = binPath;
}

private final String EXPERIENCE_ENTRY_ELEMENT = Main.readStringResource("/site/html/template/experience_entry.html");

private final String CURRENT_JOB_ELEMENT = Main.readStringResource("/site/html/template/current_job.html");

public void generate(GeneralInfo generalInfo, ExperienceInfo info) throws IOException {
Main.LOGGER.info("Generating file: experience.html");
generateElements(info);
Expand All @@ -46,7 +44,7 @@ public void generate(GeneralInfo generalInfo, ExperienceInfo info) throws IOExce

private void generateFile(GeneralInfo generalInfo) throws IOException {
Main.DEBUG_LOGGER.info("Generating file");
String file = Main.readStringResource("/site/html/experience.html")
String file = ResourceManager.getResource(Resources.HTML_EXPERIENCE_PAGE)
.replaceAll("%%entries%%", String.join("", elementsGenerated))
.replaceAll("%%cc%%", currentJobElement)
.replaceAll("%%ch%%", !currentJobElement.isEmpty() ? "<h2>Job History</h2><br>" : "")
Expand All @@ -67,7 +65,7 @@ private void generateElements(ExperienceInfo info) {
elementsGenerated.clear();
for (ExperienceEntry entry : entries) {
if (entry.isCurrentJob()) continue;
String element = EXPERIENCE_ENTRY_ELEMENT.replaceAll("%%pcompany%%", entry.getCompany())
String element = ResourceManager.getResource(Resources.HTML_EXPERIENCE_ENTRY_TEMPLATE).replaceAll("%%pcompany%%", entry.getCompany())
.replaceAll("%%ppos%%", entry.getPosition())
.replaceAll("%%pstart%%", entry.getFrom().toString())
.replaceAll("%%pend%%", entry.getTo().toString());
Expand All @@ -77,7 +75,7 @@ private void generateElements(ExperienceInfo info) {

if (info.hasCurrentJob()) {
ExperienceEntry entry = info.getCurrentJob();
String element = CURRENT_JOB_ELEMENT.replaceAll("%%pcompany%%", entry.getCompany())
String element = ResourceManager.getResource(Resources.HTML_CURRENT_JOB_TEMPLATE).replaceAll("%%pcompany%%", entry.getCompany())
.replaceAll("%%ppos%%", entry.getPosition())
.replaceAll("%%pstart%%", entry.getFrom().toString())
.replaceAll("%%pend%%", entry.getTo().toString());
Expand Down
49 changes: 26 additions & 23 deletions src/main/java/dev/efekos/pg/output/FileGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
import dev.efekos.pg.data.schema.*;
import dev.efekos.pg.data.timeline.TimelineEvent;
import dev.efekos.pg.data.type.SocialLinkType;
import dev.efekos.pg.util.Locale;
import dev.efekos.pg.process.ProcessContext;
import dev.efekos.pg.resource.Resource;
import dev.efekos.pg.resource.ResourceManager;
import dev.efekos.pg.resource.Resources;
import dev.efekos.pg.util.Locale;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.*;

public class FileGenerator implements Generator {

Expand Down Expand Up @@ -60,7 +60,7 @@ public void generateIndexFile(ProcessContext context) throws IOException {
timelineElements.add("<li>"+generateAboutEntry(event.getIcon(), event.getTitle(), event.getTime(), false)+"</li>");
}

String fileString = Main.readStringResource("/site/html/index.html")
String fileString = ResourceManager.getResource(Resources.HTML_INDEX_PAGE)
.replaceAll("%%name%%", info.getName())
.replaceAll("%%title%%", info.getTitle())
.replaceAll("%%welcomer%%", info.getWelcomer())
Expand Down Expand Up @@ -96,7 +96,7 @@ public void generateContactPage(GeneralInfo generalInfo,ContactInfo contactInfo)
}
});

String file = Main.readStringResource("/site/html/contact.html");
String file = ResourceManager.getResource(Resources.HTML_CONTACT_PAGE);

if(contactInfo.isIncludeSocials()){
file = file.replaceAll(
Expand All @@ -118,7 +118,7 @@ public void generateContactPage(GeneralInfo generalInfo,ContactInfo contactInfo)
}

private String generatePlaceEntry(Place place){
String file = Main.readStringResource("/site/html/template/place_entry.html");
String file = ResourceManager.getResource(Resources.HTML_PLACE_ENTRY_TEMPLATE);

return file
.replaceAll("%%plmaps%%",place.getMapsLink())
Expand All @@ -128,7 +128,7 @@ private String generatePlaceEntry(Place place){
}

private String generateAboutEntry(String icon, String title, String alt, boolean age){
String templateEntry = Main.readStringResource("/site/html/template/about_entry.html");
String templateEntry = ResourceManager.getResource(Resources.HTML_ABOUT_ENTRY_TEMPLATE);

return templateEntry.replaceAll("%%title%%", title)
.replaceAll("%%icon%%", icon)
Expand All @@ -137,7 +137,7 @@ private String generateAboutEntry(String icon, String title, String alt, boolean
}

private String generateSocialElement(SocialLinkType type, String link) throws IOException{
String templateElement = Main.readStringResource("/site/html/template/social_icon.html");
String templateElement = ResourceManager.getResource(Resources.HTML_SOCIAL_ICON_TEMPLATE);
return templateElement.replaceAll("%%link%%", link).replaceAll("%%icon%%", type.getId());
}

Expand All @@ -150,11 +150,11 @@ public void generateExperienceFile(GeneralInfo generalInfo, ExperienceInfo exper
public void generateStyleFiles(GeneralInfo info,TagColorInfo tagColorInfo) throws IOException {
Main.LOGGER.info("Copying static style files");

copyStringResource("/site/style/style.css", "\\style\\main_style.css", binPath);
copyStringResource("/site/style/style_certificates.css", "\\style\\certificates.css", binPath);
copyStringResource("/site/style/style_education.css", "\\style\\education.css", binPath);
copyStringResource("/site/style/style_projects.css", "\\style\\projects.css", binPath);
copyStringResource("/site/style/style_gallery_modals.css", "\\style\\gallery_modals.css", binPath);
copyResource(Resources.STYLE_MAIN, "\\style\\main_style.css", binPath);
copyResource(Resources.STYLE_CERTIFICATES, "\\style\\certificates.css", binPath);
copyResource(Resources.STYLE_EDUCATION, "\\style\\education.css", binPath);
copyResource(Resources.STYLE_PROJECTS, "\\style\\projects.css", binPath);
copyResource(Resources.STYLE_GALLERY_MODALS, "\\style\\gallery_modals.css", binPath);

Main.LOGGER.success("Copied all static style files");

Expand All @@ -174,13 +174,13 @@ public void generateScriptFiles(GeneralInfo info) throws IOException {

// age_calculator.js
Main.DEBUG_LOGGER.info("Generating file: age_calculator.js");
String string = Main.readStringResource("/site/script/age_calculator.js").replaceAll("%%byear%%", info.getBirthDate().getYear() + "");
String string = ResourceManager.getResource(Resources.SCRIPT_AGE_CALCULATOR).replaceAll("%%byear%%", info.getBirthDate().getYear() + "");
writeFile(binPath + "\\age_calculator.js", string);
Main.DEBUG_LOGGER.success("Generated file: age_calculator.js");

// project_search.js
copyStringResource("/site/script/projects_search.js","\\projects_search.js",binPath);
copyStringResource("/site/script/expandable_entries.js","\\expandable_entries.js",binPath);
copyResource(Resources.SCRIPT_PROJECT_SEARCH,"\\projects_search.js",binPath);
copyResource(Resources.SCRIPT_EXPAND_ENTRIES,"\\expandable_entries.js",binPath);

Main.LOGGER.success("Generates script files");
}
Expand All @@ -202,7 +202,7 @@ public void generateBioFile(GeneralInfo info) throws IOException {


String bio = info.getBio();
String bioFile = Main.readStringResource("/site/html/bio.html").replaceAll("%%bio%%", bio);
String bioFile = ResourceManager.getResource(Resources.HTML_BIO_PAGE).replaceAll("%%bio%%", bio);

writeFile(binPath + "\\bio.html", bioFile);

Expand Down Expand Up @@ -261,7 +261,10 @@ public void copyIcons(GeneralInfo generalInfo) throws IOException {

private void copyIcon(String resourceName, String binName) throws IOException {
Main.DEBUG_LOGGER.info("Copying icon: " + resourceName);
String string = Main.readStringResource("/site/icon/" + resourceName + ".svg");
Optional<Resource> optional = Resources.all().stream().filter(resource -> resource.getPathName().equals("/site/icon/" + resourceName + ".svg")).findFirst();
if(optional.isEmpty()) throw new MissingResourceException("Missing icon","dev.efekos.pg.Main","/site/icon/" + resourceName + ".svg");

String string = ResourceManager.getResource(optional.get());
writeFile(binPath + "\\images\\icon\\" + binName + ".svg", string);
Main.DEBUG_LOGGER.success("Copied icon: " + resourceName);
}
Expand All @@ -278,9 +281,9 @@ public void generateProjectsPage(GeneralInfo generalInfo, List<Project> projects
public void copyLibraries() throws IOException {
Main.LOGGER.info("Copying library files");

copyStringResource("/site/lib/marked.js", "\\lib\\marked.js", binPath);
copyStringResource("/site/lib/prism.js", "\\lib\\prism.js", binPath);
copyStringResource("/site/lib/prism.css", "\\lib\\prism.css", binPath);
copyResource(Resources.SCRIPT_LIBRARY_MARKED, "\\lib\\marked.js", binPath);
copyResource(Resources.SCRIPT_LIBRARY_PRISM, "\\lib\\prism.js", binPath);
copyResource(Resources.STYLE_LIBRARY_PRISM, "\\lib\\prism.css", binPath);

Main.LOGGER.success("Copied library files");
}
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/dev/efekos/pg/output/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package dev.efekos.pg.output;

import dev.efekos.pg.Main;
import dev.efekos.pg.resource.Resource;
import dev.efekos.pg.resource.ResourceManager;
import dev.efekos.pg.resource.Resources;

import java.io.File;
import java.io.FileWriter;
Expand All @@ -38,10 +41,10 @@ default String makeIdForLink(String id) {
.toLowerCase(Locale.ROOT);
}

default void copyStringResource(String resourceLocation, String outputLocation, String binPath) throws IOException {
Main.DEBUG_LOGGER.info("Copying resource: " + resourceLocation);
default void copyResource(Resource resource, String outputLocation, String binPath) throws IOException {
Main.DEBUG_LOGGER.info("Copying resource: " + resource.getPathName());

String fileString = Main.readStringResource(resourceLocation,true);
String fileString = ResourceManager.getResource(resource);
File file = new File(binPath + outputLocation);
file.getParentFile().mkdirs();
file.createNewFile();
Expand All @@ -51,7 +54,7 @@ default void copyStringResource(String resourceLocation, String outputLocation,
writer.flush();
writer.close();

Main.DEBUG_LOGGER.success("Copied resource: " + resourceLocation+" to "+outputLocation.replaceAll("\\\\","/"));
Main.DEBUG_LOGGER.success("Copied resource: " + resource.getPathName()+" to "+outputLocation.replaceAll("\\\\","/"));
}

default void writeFile(String path, String content) throws IOException {
Expand All @@ -63,7 +66,7 @@ default void writeFile(String path, String content) throws IOException {

FileWriter writer = new FileWriter(file, StandardCharsets.UTF_8);

writer.write(content.replaceAll("%%footer%%",Main.FOOTER_ELEMENT));
writer.write(content.replaceAll("%%footer%%",ResourceManager.getResource(Resources.HTML_FOOTER)));
writer.flush();
writer.close();
Main.DEBUG_LOGGER.success("Wrote file: ",logPath);
Expand Down
Loading

0 comments on commit 392b94c

Please sign in to comment.