-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added links internalization (#10)
* feat: Added links internalization Refs: #DEV-11660
- Loading branch information
Showing
16 changed files
with
321 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/ch/sbb/polarion/extension/pdf/exporter/util/html/ExternalCssInternalizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ch.sbb.polarion.extension.pdf.exporter.util.html; | ||
|
||
import ch.sbb.polarion.extension.pdf.exporter.util.FileResourceProvider; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class ExternalCssInternalizer implements LinkInternalizer { | ||
private static final String DATA_PRECEDENCE = "data-precedence"; | ||
private final FileResourceProvider fileResourceProvider; | ||
|
||
public ExternalCssInternalizer(FileResourceProvider fileResourceProvider) { | ||
this.fileResourceProvider = fileResourceProvider; | ||
} | ||
|
||
@Override | ||
public Optional<String> inline(Map<String, String> attributes) { | ||
if (!"stylesheet".equals(attributes.get("rel")) | ||
|| !attributes.containsKey("href")) { | ||
return Optional.empty(); | ||
} | ||
StringBuilder inlinedContent = new StringBuilder("<style"); | ||
if (attributes.containsKey(DATA_PRECEDENCE)) { | ||
inlinedContent.append(" ") | ||
.append(DATA_PRECEDENCE) | ||
.append("=\"") | ||
.append(attributes.get(DATA_PRECEDENCE)) | ||
.append("\""); | ||
} | ||
inlinedContent.append(">"); | ||
String url = attributes.get("href"); | ||
inlinedContent.append(new String(fileResourceProvider.getResourceAsBytes(url))); | ||
inlinedContent.append("</style>"); | ||
|
||
return Optional.of(inlinedContent.toString()); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/ch/sbb/polarion/extension/pdf/exporter/util/html/HtmlLinksHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package ch.sbb.polarion.extension.pdf.exporter.util.html; | ||
|
||
import ch.sbb.polarion.extension.pdf.exporter.properties.PdfExporterExtensionConfiguration; | ||
import ch.sbb.polarion.extension.pdf.exporter.util.FileResourceProvider; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class HtmlLinksHelper { | ||
private static final Pattern LINK_PATTERN = Pattern.compile("<link\\s*[^>]*>", Pattern.CASE_INSENSITIVE); | ||
private static final Pattern ATTRIBUTE_PATTERN = Pattern.compile("([\\w-]+)\\s*=\\s*(['\"])(.*?)\\2"); | ||
|
||
private final Set<LinkInternalizer> linkInliners; | ||
|
||
public HtmlLinksHelper(FileResourceProvider fileResourceProvider) { | ||
this (Set.of( | ||
new ExternalCssInternalizer(fileResourceProvider) | ||
)); | ||
} | ||
|
||
public HtmlLinksHelper(Set<LinkInternalizer> linkInliners) { | ||
this.linkInliners = linkInliners; | ||
} | ||
|
||
public String internalizeLinks(String htmlContent) { | ||
boolean linksInternalizationEnabled = PdfExporterExtensionConfiguration.getInstance().getInternalizeExternalCss(); | ||
if (!linksInternalizationEnabled) { | ||
return htmlContent; | ||
} | ||
|
||
Matcher matcher = LINK_PATTERN.matcher(htmlContent); | ||
StringBuilder newHtmlContent = new StringBuilder(); | ||
|
||
while (matcher.find()) { | ||
String linkTag = matcher.group(0); | ||
|
||
Map<String, String> attributesMap = parseLinkTagAttributes(linkTag); | ||
String replacement = inlineLinkTag(linkTag, attributesMap); | ||
|
||
matcher.appendReplacement(newHtmlContent, Matcher.quoteReplacement(replacement)); | ||
} | ||
matcher.appendTail(newHtmlContent); | ||
|
||
return newHtmlContent.toString(); | ||
} | ||
|
||
public static Map<String, String> parseLinkTagAttributes(String linkTag) { | ||
Map<String, String> attributes = new LinkedHashMap<>(); | ||
|
||
Matcher matcher = ATTRIBUTE_PATTERN.matcher(linkTag); | ||
|
||
while (matcher.find()) { | ||
String attributeName = matcher.group(1); | ||
String attributeValue = matcher.group(3); | ||
attributes.put(attributeName, attributeValue); | ||
} | ||
return attributes; | ||
} | ||
|
||
private String inlineLinkTag(String linkTag, Map<String, String> attributesMap) { | ||
return linkInliners.stream() | ||
.map(i -> i.inline(attributesMap)) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.findFirst() | ||
.orElse(linkTag); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/ch/sbb/polarion/extension/pdf/exporter/util/html/LinkInternalizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ch.sbb.polarion.extension.pdf.exporter.util.html; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public interface LinkInternalizer { | ||
Optional<String> inline(Map<String, String> attributes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.