Skip to content

Commit

Permalink
add some singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
clagomess committed Mar 17, 2024
1 parent 918d06a commit ffdfa5e
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.github.clagomess.pirilampo.core.compilers;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.clagomess.pirilampo.core.dto.FeatureMetadataDto;
import com.github.clagomess.pirilampo.core.dto.ParametersDto;
import com.github.clagomess.pirilampo.core.fi.UIProgressFI;
Expand Down Expand Up @@ -29,10 +27,6 @@ public abstract class Compiler {
@Setter
protected float progressCount = 0;

protected final ObjectMapper mapper = new ObjectMapper() {{
disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
}};

private final Pattern pFeatureExt = Pattern.compile("\\.feature$", Pattern.CASE_INSENSITIVE);
public String getFeatureExtension(File f){
Matcher matcher = pFeatureExt.matcher(f.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import com.github.clagomess.pirilampo.core.dto.MenuDto;
import com.github.clagomess.pirilampo.core.dto.ParametersDto;
import com.github.clagomess.pirilampo.core.enums.DiffEnum;
import com.github.clagomess.pirilampo.core.parsers.GherkinDocumentParser;
import com.github.clagomess.pirilampo.core.parsers.ImageParser;
import com.github.clagomess.pirilampo.core.parsers.IndexParser;
import com.github.clagomess.pirilampo.core.parsers.MenuParser;
import com.github.clagomess.pirilampo.core.parsers.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

Expand All @@ -27,7 +24,6 @@
@Slf4j
public class FolderToHTMLCompiler extends Compiler implements ArtifactCompiler {
private final PropertiesCompiler propertiesCompiler = new PropertiesCompiler();
private final ImageParser imageParser = new ImageParser();
private final ParametersDto parameters;
private final IndexParser indexParser = new IndexParser();
protected final MenuParser menuParser;
Expand Down Expand Up @@ -104,7 +100,7 @@ protected void buildSidebar(PrintWriter out){

if(parameters.getProjectLogo() != null){
out.print("<a href=\"#/\"><img class=\"logo\" src=\"");
imageParser.parse(out, parameters, null, parameters.getProjectLogo().toString());
ImageParser.getInstance().parse(out, parameters, null, parameters.getProjectLogo().toString());
out.print("\"></a>");
}else{
out.print(String.format(
Expand Down Expand Up @@ -134,12 +130,12 @@ protected void buildMenu(PrintWriter out) throws IOException {

if(menuParser.getMenu().getChildren().isEmpty()) {
out.print("createMenuItem(document.getElementsByClassName('sidebar-nav')[0], ");
mapper.writeValue(out, menuParser.getMenu());
JsonParser.getInstance().writeValue(out, menuParser.getMenu());
out.println(");");
} else {
for(MenuDto menu : menuParser.getMenu().getChildren()) {
out.print("createMenuItem(document.getElementsByClassName('sidebar-nav')[0], ");
mapper.writeValue(out, menu);
JsonParser.getInstance().writeValue(out, menu);
out.println(");");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.clagomess.pirilampo.core.compilers.Compiler;
import com.github.clagomess.pirilampo.core.dto.ParametersDto;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
Expand All @@ -11,6 +12,10 @@

@Slf4j
public class ImageParser extends Compiler {
@Getter
private static final ImageParser instance = new ImageParser();
private ImageParser(){}

public void parse(PrintWriter out, ParametersDto parameters, File feature, String fileName){
File file = getAbsolutePathFile(parameters, feature, fileName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public void putFeaturePhrase(String featureId, String rawPhrase){

public void buildIndex(PrintWriter out) throws IOException {
out.print("let indexPhrases = ");
mapper.writeValue(out, phrases.entrySet().stream()
JsonParser.getInstance().writeValue(out, phrases.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)));
out.println(";");

out.print("let indexMap = ");
mapper.writeValue(out, index);
JsonParser.getInstance().writeValue(out, index);
out.println(";");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.clagomess.pirilampo.core.parsers;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;

public class JsonParser extends ObjectMapper {
@Getter
private static final JsonParser instance = new JsonParser();

private JsonParser() {
disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.clagomess.pirilampo.core.parsers;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.commonmark.node.Document;
import org.commonmark.node.Node;
Expand All @@ -17,7 +18,10 @@ public class MarkdownParser {
private final Parser parser = Parser.builder().build();
private final HtmlRenderer renderer;

public MarkdownParser() {
@Getter
private static final MarkdownParser instance = new MarkdownParser();

private MarkdownParser() {
this.renderer = HtmlRenderer.builder()
.nodeRendererFactory(SkipParentWrapperParagraphsRenderer::new)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import java.util.regex.Pattern;

public class TextParser extends Compiler {
private final MarkdownParser markdownParser = new MarkdownParser();
private final ImageParser imageParser = new ImageParser();
private final IndexParser indexParser;

private final ParametersDto parameters;
Expand Down Expand Up @@ -44,7 +42,7 @@ public void format(PrintWriter out, String txtRaw, boolean makdown){
return;
}

if(makdown) txt = markdownParser.build(txt);
if(makdown) txt = MarkdownParser.getInstance().build(txt);

String img = "<br/><p><img src=\"$1\" $2/></p>";
txt = txt.replaceAll("<img src=\"(.+?)\"(.*?)>", img);
Expand Down Expand Up @@ -96,7 +94,7 @@ protected void findAndReplaceImagesSrcAndWriteOut(PrintWriter out, String txt){

out.write(txt, position, groupPosition - position);
out.write("src=\"");
imageParser.parse(out, parameters, feature, mImgSrc.group(1));
ImageParser.getInstance().parse(out, parameters, feature, mImgSrc.group(1));
out.write("\"");

position = groupPosition + group.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

@Slf4j
public class ImageParserTest extends Common {
private final ImageParser imageParser = new ImageParser();

@ParameterizedTest
@CsvSource(value = {
"true$smallest.png$data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVR4AWNgAAAAAgABc3UBGAAAAABJRU5ErkJggg==",
Expand All @@ -33,7 +31,7 @@ public void parse(Boolean embedded, String filename, String expected){
StringWriter sw = new StringWriter();

try (PrintWriter out = new PrintWriter(sw)){
imageParser.parse(out, parameters, featureFile, filename);
ImageParser.getInstance().parse(out, parameters, featureFile, filename);

assertEquals(expected, sw.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@

@Slf4j
public class MarkdownParserTest {
private final MarkdownParser markdownParser = new MarkdownParser();

@Test
public void build(){
assertEquals(
"a: <a href=\"xx_a.html\">xx_a</a>",
markdownParser.build("a: [xx_a](xx_a.html)")
MarkdownParser.getInstance().build("a: [xx_a](xx_a.html)")
);
}
}

0 comments on commit ffdfa5e

Please sign in to comment.