Refactor publication parser location.
This commit is contained in:
@@ -1,143 +0,0 @@
|
||||
package org.codiki.infrastructure.publication;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;
|
||||
|
||||
@Service
|
||||
public class ParserService {
|
||||
private static final String REG_CODE = "\\[code lg="([a-z]+)"\\](.*)\\[\\/code\\]\\n";
|
||||
private static final String REG_IMAGES = "\\[img src="([^\"| ]+)"( alt="([^\"| ]+)")? \\/\\]";
|
||||
private static final String REG_LINKS = "\\[link href="([^\"| ]+)" txt="([^\"| ]+)" \\/\\]";
|
||||
|
||||
private static final Pattern PATTERN_CODE;
|
||||
private static final Pattern PATTERN_IMAGES;
|
||||
private static final Pattern PATTERN_LINKS;
|
||||
|
||||
static {
|
||||
PATTERN_CODE = Pattern.compile(REG_CODE);
|
||||
PATTERN_IMAGES = Pattern.compile(REG_IMAGES);
|
||||
PATTERN_LINKS = Pattern.compile(REG_LINKS);
|
||||
}
|
||||
|
||||
public String parse(String pSource) {
|
||||
return unParseDolars(parseCode(parseHeaders(parseImages(parseLinks(parseBackSpaces(escapeHtml4(parseDolars(pSource))))))));
|
||||
}
|
||||
|
||||
private String parseDolars(final String pSource) {
|
||||
return pSource.replace("$", "£ø");
|
||||
}
|
||||
|
||||
private String unParseDolars(final String pSource) {
|
||||
return pSource.replace("£ø", "$");
|
||||
}
|
||||
|
||||
String parseHeaders(final String pSource) {
|
||||
String result = pSource;
|
||||
for(int i = 1 ; i <= 3 ; i++) {
|
||||
result = parseHeadersHX(result, i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
String parseHeadersHX(final String pSource, final int pNumHeader) {
|
||||
String result = pSource;
|
||||
|
||||
// (.*)(\[hX\](.*)\[\/hX\])+(.*)
|
||||
final String regex = concat("(.*)(\\[h", pNumHeader, "\\](.*)\\[\\/h", pNumHeader, "\\])+(.*)");
|
||||
|
||||
final Pattern pattern = Pattern.compile(regex);
|
||||
|
||||
Matcher matcher = pattern.matcher(result);
|
||||
|
||||
while(matcher.find()) {
|
||||
// \1<hX>\3</hX>\4
|
||||
result = matcher.replaceFirst(concat(matcher.group(1),
|
||||
"<h", pNumHeader, ">", matcher.group(3), "</h", pNumHeader, ">", matcher.group(4)));
|
||||
matcher = pattern.matcher(result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
String parseBackSpaces(final String pSource) {
|
||||
return pSource.replaceAll("\r?\n", "<br/>").replaceAll("\\[\\/code\\]<br\\/><br\\/>", "[/code]\n");
|
||||
}
|
||||
|
||||
String parseImages(final String pSource) {
|
||||
String result = pSource;
|
||||
|
||||
Matcher matcher = PATTERN_IMAGES.matcher(result);
|
||||
|
||||
while(matcher.find()) {
|
||||
String altStr = matcher.group(3);
|
||||
|
||||
if(altStr == null) {
|
||||
altStr = "";
|
||||
}
|
||||
|
||||
result = matcher.replaceFirst(concat("<img src=\"", matcher.group(1), "\" alt=\"", altStr, "\" />"));
|
||||
matcher = PATTERN_IMAGES.matcher(result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
String parseLinks(final String pSource) {
|
||||
String result = pSource;
|
||||
|
||||
Matcher matcher = PATTERN_LINKS.matcher(result);
|
||||
|
||||
while(matcher.find()) {
|
||||
result = matcher.replaceFirst(concat("<a href=\"", matcher.group(1), "\">", matcher.group(2), "</a>"));
|
||||
matcher = PATTERN_LINKS.matcher(result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String parseCode(final String pSource) {
|
||||
String result = pSource;
|
||||
|
||||
Matcher matcher = PATTERN_CODE.matcher(result);
|
||||
|
||||
while(matcher.find()) {
|
||||
// replace the '<br/>' in group by '\n'
|
||||
String codeContent = matcher.group(2).replaceAll("<br\\/>", "\n");
|
||||
if(codeContent.startsWith("\n")) {
|
||||
codeContent = codeContent.substring(1);
|
||||
}
|
||||
|
||||
result = matcher.replaceFirst(
|
||||
concat(
|
||||
"<pre class=\"line-numbers\"><code class=\"language-",
|
||||
matcher.group(1),
|
||||
"\">",
|
||||
codeContent,
|
||||
"</code></pre>"
|
||||
)
|
||||
);
|
||||
matcher = PATTERN_CODE.matcher(result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenate the parameters to form just one single string.
|
||||
*
|
||||
* @param pArgs
|
||||
* The strings to concatenate.
|
||||
* @return The parameters concatenated.
|
||||
*/
|
||||
private static String concat(final Object... pArgs) {
|
||||
final StringBuilder result = new StringBuilder();
|
||||
for (final Object arg : pArgs) {
|
||||
result.append(arg);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
@@ -14,23 +14,18 @@ import java.util.UUID;
|
||||
|
||||
import static java.util.Collections.reverseOrder;
|
||||
import static java.util.Comparator.comparingInt;
|
||||
import static org.codiki.domain.publication.model.builder.PublicationBuilder.aPublication;
|
||||
import static org.springframework.util.ObjectUtils.isEmpty;
|
||||
|
||||
@Component
|
||||
public class PublicationJpaAdapter implements PublicationPort {
|
||||
private final PublicationRepository repository;
|
||||
private final PublicationSearchCriteriaJpaAdapter publicationSearchCriteriaJpaAdapter;
|
||||
private final ParserService parserService;
|
||||
|
||||
public PublicationJpaAdapter(
|
||||
PublicationRepository repository,
|
||||
PublicationSearchCriteriaJpaAdapter publicationSearchCriteriaJpaAdapter,
|
||||
ParserService parserService
|
||||
PublicationSearchCriteriaJpaAdapter publicationSearchCriteriaJpaAdapter
|
||||
) {
|
||||
this.repository = repository;
|
||||
this.publicationSearchCriteriaJpaAdapter = publicationSearchCriteriaJpaAdapter;
|
||||
this.parserService = parserService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -41,24 +36,8 @@ public class PublicationJpaAdapter implements PublicationPort {
|
||||
|
||||
@Override
|
||||
public Optional<Publication> findById(UUID publicationId) {
|
||||
Optional<Publication> result = repository.findById(publicationId)
|
||||
return repository.findById(publicationId)
|
||||
.map(PublicationEntity::toDomain);
|
||||
|
||||
if (result.isPresent()) {
|
||||
Publication publication = result.get();
|
||||
if (isEmpty(publication.parsedText())) {
|
||||
Publication editedPublication = aPublication()
|
||||
.basedOn(publication)
|
||||
.withParsedText(parserService.parse(publication.text()))
|
||||
.build();
|
||||
PublicationEntity editedPublicationEntity = new PublicationEntity(editedPublication);
|
||||
repository.save(editedPublicationEntity);
|
||||
|
||||
result = Optional.of(editedPublication);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user