Skip to content

Commit

Permalink
feat: Refactored property name, moved internalize call to PdfConverter
Browse files Browse the repository at this point in the history
Refs: #DEV-11660
  • Loading branch information
nirikash committed Jun 13, 2024
1 parent 6f62e75 commit f1a8dc4
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ The converting HTML can contain some external CSS links referencing Polarion Ser
```
In case the Polarion Server is not reachable from the Weasyprint service, such links cannot be successfully resolved during the Weasyprint PDF transformation. The solution is to replace external CSS <link> elements with internal CSS <style> tags containing the CSS content embedded into the HTML document. By default, CSS link internalization is disabled. To enable internalization of CSS links, it is necessary to activate the following property in the configuration file:
```properties
ch.sbb.polarion.extension.pdf-exporter.html.internalize-css-links=true
ch.sbb.polarion.extension.pdf-exporter.internalizeExternalCss=true
```

## Extension Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ String preprocessHtml(String origHtml, Orientation orientation, PaperSize paperS
html = replaceTagContent(origHtml, "head", head);
}
html = htmlProcessor.replaceImagesAsBase64Encoded(html);
html = htmlProcessor.internalizeCssLinks(html);
html = htmlProcessor.internalizeLinks(html);

return html;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public byte[] convertToPdf(@NotNull ExportParams exportParams, @Nullable ExportM
if (metaInfoCallback != null) {
metaInfoCallback.setLinkedWorkItems(WorkItemRefData.extractListFromHtml(htmlContent, exportParams.getProjectId()));
}
htmlContent = htmlProcessor.internalizeLinks(htmlContent);

generationLog.log("Html is ready, starting pdf generation");
byte[] bytes = generatePdf(documentData, exportParams, metaInfoCallback, htmlContent, generationLog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PdfExporterExtensionConfiguration extends ExtensionConfiguration {
public static final String WEASYPRINT_EXECUTABLE = "weasyprint.executable";
public static final String WEASYPRINT_EXECUTABLE_DEFAULT = "weasyprint";
public static final String WEASYPRINT_PDF_VARIANT = "weasyprint.pdf.variant";
public static final String HTML_INTERNALIZE_CSS_LINKS = "html.internalize-css-links";
public static final String INTERNALIZE_EXTERNAL_CSS = "internalizeExternalCss";

public WeasyPrintConnector getWeasyprintConnector() {
String value = SystemValueReader.getInstance().readString(getPropertyPrefix() + WEASYPRINT_CONNECTOR, WEASYPRINT_CONNECTOR_DEFAULT);
Expand All @@ -38,8 +38,8 @@ public String getWeasyprintPdfVariant() {
return SystemValueReader.getInstance().readString(getPropertyPrefix() + WEASYPRINT_PDF_VARIANT, null);
}

public Boolean htmlInternalizeCssLinks() {
return SystemValueReader.getInstance().readBoolean(getPropertyPrefix() + HTML_INTERNALIZE_CSS_LINKS, false);
public Boolean getInternalizeExternalCss() {
return SystemValueReader.getInstance().readBoolean(getPropertyPrefix() + INTERNALIZE_EXTERNAL_CSS, false);
}

@Override
Expand All @@ -49,7 +49,7 @@ public Boolean htmlInternalizeCssLinks() {
supportedProperties.add(WEASYPRINT_SERVICE);
supportedProperties.add(WEASYPRINT_EXECUTABLE);
supportedProperties.add(WEASYPRINT_PDF_VARIANT);
supportedProperties.add(HTML_INTERNALIZE_CSS_LINKS);
supportedProperties.add(INTERNALIZE_EXTERNAL_CSS);
return supportedProperties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ public String processHtmlForPDF(@NotNull String html, @NotNull ExportParams expo

// Do not change this entry order, '&nbsp;' can be used in the logic above, so we must cut them off as the last step
html = cutExtraNbsp(html);
html = internalizeCssLinks(html);
return html;
}

Expand Down Expand Up @@ -1141,7 +1140,7 @@ public String replaceImagesAsBase64Encoded(String html) {
return html;
}

public String internalizeCssLinks(String html) {
public String internalizeLinks(String html) {
return httpLinksHelper.internalizeLinks(html);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import java.util.Map;
import java.util.Optional;

public class CssLinkInternalizer implements LinkInternalizer {
public class ExternalCssInternalizer implements LinkInternalizer {
private static final String DATA_PRECEDENCE = "data-precedence";
private final FileResourceProvider fileResourceProvider;

public CssLinkInternalizer(FileResourceProvider fileResourceProvider) {
public ExternalCssInternalizer(FileResourceProvider fileResourceProvider) {
this.fileResourceProvider = fileResourceProvider;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class HtmlLinksHelper {

public HtmlLinksHelper(FileResourceProvider fileResourceProvider) {
this (Set.of(
new CssLinkInternalizer(fileResourceProvider)
new ExternalCssInternalizer(fileResourceProvider)
));
}

Expand All @@ -27,7 +27,7 @@ public HtmlLinksHelper(Set<LinkInternalizer> linkInliners) {
}

public String internalizeLinks(String htmlContent) {
boolean enablingProperty = PdfExporterExtensionConfiguration.getInstance().htmlInternalizeCssLinks();
boolean enablingProperty = PdfExporterExtensionConfiguration.getInstance().getInternalizeExternalCss();
if (!enablingProperty) {
return htmlContent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ <h4>Enabling Internalization of CSS Links</h4>
By default, CSS link internalization is disabled. To enable internalization of CSS links, it is necessary to activate the following property in file <code>&lt;POLARION_HOME&gt;/etc/polarion.properties</code>:</p>
<pre>
<code data-language="properties">
<span class="cm-def">ch.sbb.polarion.extension.pdf-exporter.html.internalize-css-links</span><span class=".cm-operator">=</span>true
<span class="cm-def">ch.sbb.polarion.extension.pdf-exporter.internalizeExternalCss</span><span class=".cm-operator">=</span>true
</code>
</pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void shouldInjectHeadAndStyle() {
when(pdfTemplateProcessor.buildSizeCss(Orientation.PORTRAIT, PaperSize.A4)).thenReturn("@page {size: test;}");
when(htmlProcessor.replaceImagesAsBase64Encoded(anyString())).thenAnswer(invocation ->
invocation.getArgument(0));
when(htmlProcessor.internalizeCssLinks(anyString())).thenAnswer(a -> a.getArgument(0));
when(htmlProcessor.internalizeLinks(anyString())).thenAnswer(a -> a.getArgument(0));
String resultHtml = htmlToPdfConverter.preprocessHtml(html, Orientation.PORTRAIT, PaperSize.A4);

assertThat(resultHtml).isEqualTo("""
Expand Down Expand Up @@ -75,7 +75,7 @@ void shouldExtendExistingHeadAndStyle() {
when(pdfTemplateProcessor.buildSizeCss(Orientation.LANDSCAPE, PaperSize.A3)).thenReturn(" @page {size: test;}");
when(htmlProcessor.replaceImagesAsBase64Encoded(anyString())).thenAnswer(invocation ->
invocation.getArgument(0));
when(htmlProcessor.internalizeCssLinks(anyString())).thenAnswer(a -> a.getArgument(0));
when(htmlProcessor.internalizeLinks(anyString())).thenAnswer(a -> a.getArgument(0));
String resultHtml = htmlToPdfConverter.preprocessHtml(html, Orientation.LANDSCAPE, PaperSize.A3);

assertThat(resultHtml).isEqualTo("""
Expand Down Expand Up @@ -108,7 +108,7 @@ void shouldExtendHeadAndAddStyle() {
when(pdfTemplateProcessor.buildSizeCss(Orientation.LANDSCAPE, PaperSize.A3)).thenReturn(" @page {size: test;}");
when(htmlProcessor.replaceImagesAsBase64Encoded(anyString())).thenAnswer(invocation ->
invocation.getArgument(0));
when(htmlProcessor.internalizeCssLinks(anyString())).thenAnswer(a -> a.getArgument(0));
when(htmlProcessor.internalizeLinks(anyString())).thenAnswer(a -> a.getArgument(0));
String resultHtml = htmlToPdfConverter.preprocessHtml(html, Orientation.LANDSCAPE, PaperSize.A3);

assertThat(resultHtml).isEqualTo("""
Expand Down Expand Up @@ -144,7 +144,7 @@ void shouldAcceptAttributesAndEmptyTags() {
when(pdfTemplateProcessor.buildSizeCss(Orientation.PORTRAIT, PaperSize.A4)).thenReturn("@page {size: test;}");
when(htmlProcessor.replaceImagesAsBase64Encoded(anyString())).thenAnswer(invocation ->
invocation.getArgument(0));
when(htmlProcessor.internalizeCssLinks(anyString())).thenAnswer(a -> a.getArgument(0));
when(htmlProcessor.internalizeLinks(anyString())).thenAnswer(a -> a.getArgument(0));
String resultHtml = htmlToPdfConverter.preprocessHtml(html, Orientation.PORTRAIT, PaperSize.A4);

assertThat(resultHtml).isEqualTo("""
Expand All @@ -165,7 +165,7 @@ void shouldInvokeWeasyPrint() {
when(pdfTemplateProcessor.buildSizeCss(Orientation.LANDSCAPE, PaperSize.A3)).thenReturn("@page {size: test;}");
when(htmlProcessor.replaceImagesAsBase64Encoded(anyString())).thenAnswer(invocation ->
invocation.getArgument(0));
when(htmlProcessor.internalizeCssLinks(anyString())).thenAnswer(a -> a.getArgument(0));
when(htmlProcessor.internalizeLinks(anyString())).thenAnswer(a -> a.getArgument(0));
when(weasyPrintConverter.convertToPdf(resultHtml, new WeasyPrintOptions(true))).thenReturn("test content".getBytes());

byte[] result = htmlToPdfConverter.convert(origHtml, Orientation.LANDSCAPE, PaperSize.A3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void shouldConvertToPdfInSimplestWorkflow() {
when(velocityEvaluator.evaluateVelocityExpressions(eq(documentData), anyString())).thenAnswer(a -> a.getArguments()[1]);
when(pdfTemplateProcessor.processUsing(eq(exportParams), eq("testDocument"), eq("css content"), anyString())).thenReturn("test html content");
when(weasyPrintConverter.convertToPdf("test html content", new WeasyPrintOptions())).thenReturn("test document content".getBytes());
when(htmlProcessor.internalizeLinks(anyString())).thenAnswer(a -> a.getArgument(0));

// Act
byte[] result = pdfConverter.convertToPdf(exportParams, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ void cutLocalUrlsTest() {
@SneakyThrows
void cutLocalUrlsWithRolesFilteringTest() {
when(localizationSettings.load(any(), any(SettingId.class))).thenReturn(new LocalizationModel(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()));
when(htmlLinksHelper.internalizeLinks(anyString())).thenAnswer(a -> a.getArgument(0));

try (InputStream isInvalidHtml = this.getClass().getResourceAsStream("/cutLocalUrlsWithRolesFilteringBeforeProcessing.html");
InputStream isValidHtml = this.getClass().getResourceAsStream("/cutLocalUrlsWithRolesFilteringAfterProcessing.html")) {
Expand Down Expand Up @@ -268,7 +267,6 @@ void removeSvgUnsupportedFeatureHintTest() {
@Test
@SneakyThrows
void processHtmlForPDFTestCutEmptyWorkItemAttributesDisabled() {
when(htmlLinksHelper.internalizeLinks(anyString())).thenAnswer(a -> a.getArgument(0));
try (InputStream isHtml = this.getClass().getResourceAsStream("/emptyWIAttributesBeforeProcessing.html")) {

String html = new String(isHtml.readAllBytes(), StandardCharsets.UTF_8);
Expand All @@ -288,15 +286,13 @@ void processHtmlForPDFTestCutEmptyWorkItemAttributesDisabled() {
@Test
void adjustHeadingForPDFTestH1ReplacedWithDiv() {
String html = "<h1>First level heading</h1>";
when(htmlLinksHelper.internalizeLinks(anyString())).thenAnswer(a -> a.getArgument(0));
String result = processor.processHtmlForPDF(html, getExportParams(), List.of());
assertEquals("<div class=\"title\">First level heading</div>", result);
}

@Test
void adjustHeadingForPDFTestLiftHeadingTag() {
String html = "<h2>First level heading</h2>";
when(htmlLinksHelper.internalizeLinks(anyString())).thenAnswer(a -> a.getArgument(0));
String result = processor.processHtmlForPDF(html, getExportParams(), List.of());
assertEquals("<h1>First level heading</h1>", result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CssLinkInternalizerTest {
private FileResourceProvider fileResourceProvider;

@InjectMocks
private CssLinkInternalizer cssLinkInliner;
private ExternalCssInternalizer cssLinkInliner;


@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class HtmlLinksHelperTest {
void setup() {
extensionConfigurationMockedStatic = mockStatic(PdfExporterExtensionConfiguration.class);
extensionConfigurationMockedStatic.when(PdfExporterExtensionConfiguration::getInstance).thenReturn(pdfExporterExtensionConfiguration);
when(pdfExporterExtensionConfiguration.htmlInternalizeCssLinks()).thenReturn(true);
when(pdfExporterExtensionConfiguration.getInternalizeExternalCss()).thenReturn(true);
htmlLinksHelper = new HtmlLinksHelper(Set.of(linkInternalizer1, linkInternalizer2));
}

Expand Down Expand Up @@ -68,7 +68,7 @@ void shouldParseAttributesAndReplaceLinkTags() {

@Test
void shouldReturnUnchangedHtmlWhenPropertyNotSet() {
when(pdfExporterExtensionConfiguration.htmlInternalizeCssLinks()).thenReturn(false);
when(pdfExporterExtensionConfiguration.getInternalizeExternalCss()).thenReturn(false);
String resultHtml = htmlLinksHelper.internalizeLinks("""
<html lang='en'><head><link attr1="value1" attr2="value2">some content</head>""");
assertThat(resultHtml).isEqualTo("""
Expand Down

0 comments on commit f1a8dc4

Please sign in to comment.