Skip to content

Commit

Permalink
fix: Updated about, reuse configuration
Browse files Browse the repository at this point in the history
Ref.: #DEV-11660
  • Loading branch information
nirikash committed Jun 7, 2024
1 parent e98c483 commit 33e0f7a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 24 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,14 @@ If HTML logging is switched on, then in standard polarion log file there will be

Here you can find out in which files HTML was stored.

### Enabling internalization of CSS Links
### Enabling Internalization of CSS Links
The converting HTML can contain some external CSS links referencing Polarion Server, like:
```
```html
<link rel="stylesheet" href="/polarion/diff-tool-app/ui/app/_next/static/css/3c374f9daffd361a.css" data-precedence="next">
```
In case if Polarion Server is not reachable from Weasyprint service, such links cannot be successfully resolved during Weasyprint PDF transformation.
The solution is to replace external CSS <links> to internal CSS <style> tags with CSS content, embedded into HTML document.
By default, CSS links internalization is disabled.
To enable internalization of CSS links it is necessary to activate the following property in file `<POLARION_HOME>/etc/polarion.properties`:

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.internalize-css-links=true
ch.sbb.polarion.extension.pdf-exporter.html.internalize-css-links=true
```

## Extension Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +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 WeasyPrintConnector getWeasyprintConnector() {
String value = SystemValueReader.getInstance().readString(getPropertyPrefix() + WEASYPRINT_CONNECTOR, WEASYPRINT_CONNECTOR_DEFAULT);
Expand All @@ -37,13 +38,19 @@ public String getWeasyprintPdfVariant() {
return SystemValueReader.getInstance().readString(getPropertyPrefix() + WEASYPRINT_PDF_VARIANT, null);
}

public Boolean htmlInternalizeCssLinks() {
System.out.println("***: " + getPropertyPrefix() + HTML_INTERNALIZE_CSS_LINKS);
return SystemValueReader.getInstance().readBoolean(getPropertyPrefix() + HTML_INTERNALIZE_CSS_LINKS, false);
}

@Override
public @NotNull List<String> getSupportedProperties() {
List<String> supportedProperties = new ArrayList<>(super.getSupportedProperties());
supportedProperties.add(WEASYPRINT_CONNECTOR);
supportedProperties.add(WEASYPRINT_SERVICE);
supportedProperties.add(WEASYPRINT_EXECUTABLE);
supportedProperties.add(WEASYPRINT_PDF_VARIANT);
supportedProperties.add(HTML_INTERNALIZE_CSS_LINKS);
return supportedProperties;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 com.polarion.core.config.impl.SystemValueReader;

import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -10,10 +10,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.polarion.core.util.StringUtils.isEmpty;

public class HtmlLinksHelper {
private static final String INTERNALIZE_CSS_LINKS_SYSTEM_PROPERTY = "ch.sbb.polarion.extension.pdf-exporter.internalize-css-links";
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");

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

public String internalizeLinks(String htmlContent) {
String enablingProperty = SystemValueReader.getInstance().readString(INTERNALIZE_CSS_LINKS_SYSTEM_PROPERTY, "false");
if (isEmpty(enablingProperty) || !Boolean.parseBoolean(enablingProperty)) {
boolean enablingProperty = PdfExporterExtensionConfiguration.getInstance().htmlInternalizeCssLinks();
if (!enablingProperty) {
return htmlContent;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ <h4>WeasyPrint as Service in Docker</h4>
</code>
</pre>

<h4>Enabling Internalization of CSS Links</h4>

<p>The converting HTML can contain some external CSS links referencing Polarion Server, like:</p>
<pre>
<code data-language="html">
<span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">link</span> <span class="cm-attribute">rel</span>=<span class="cm-string">"stylesheet"</span> <span class="cm-attribute">href</span>=<span class="cm-string">"/polarion/diff-tool-app/ui/app/_next/static/css/3c374f9daffd361a.css"</span> <span class="cm-attribute">data-precedence</span>=<span class="cm-string">"next"</span><span class="cm-tag cm-bracket">&gt;</span>
</code>
</pre>
<p>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 &lt;link&gt; elements with internal CSS &lt;style&gt; 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 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
</code>
</pre>


<h3>PDF exporter extension to appear on a Document's properties pane</h3>

<ol>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package ch.sbb.polarion.extension.pdf.exporter.util.html;

import ch.sbb.polarion.extension.pdf.exporter.properties.PdfExporterExtensionConfiguration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Map;
Expand All @@ -22,17 +25,27 @@ class HtmlLinksHelperTest {
@Mock
private LinkInternalizer linkInternalizer2;

private HtmlLinksHelper htmlInlineLinksHelper;
@Mock
private PdfExporterExtensionConfiguration pdfExporterExtensionConfiguration;
private HtmlLinksHelper htmlLinksHelper;
private MockedStatic<PdfExporterExtensionConfiguration> extensionConfigurationMockedStatic;

@BeforeEach
void setup() {
System.setProperty("ch.sbb.polarion.extension.pdf-exporter.internalize-css-links", "true");
htmlInlineLinksHelper = new HtmlLinksHelper(Set.of(linkInternalizer1, linkInternalizer2));
extensionConfigurationMockedStatic = mockStatic(PdfExporterExtensionConfiguration.class);
extensionConfigurationMockedStatic.when(PdfExporterExtensionConfiguration::getInstance).thenReturn(pdfExporterExtensionConfiguration);
when(pdfExporterExtensionConfiguration.htmlInternalizeCssLinks()).thenReturn(true);
htmlLinksHelper = new HtmlLinksHelper(Set.of(linkInternalizer1, linkInternalizer2));
}

@AfterEach
void shutdown() {
extensionConfigurationMockedStatic.close();
}

@Test
void shouldCallInlinersForLinkTags() {
htmlInlineLinksHelper.internalizeLinks("""
htmlLinksHelper.internalizeLinks("""
<html lang='en'><head><link>some content<link></head>""");

Stream.of(linkInternalizer1, linkInternalizer2)
Expand All @@ -42,7 +55,7 @@ void shouldCallInlinersForLinkTags() {
@Test
void shouldParseAttributesAndReplaceLinkTags() {
when(linkInternalizer1.inline(anyMap())).thenReturn(Optional.of("<style>replacement</style>"));
String resultHtml = htmlInlineLinksHelper.internalizeLinks("""
String resultHtml = htmlLinksHelper.internalizeLinks("""
<html lang='en'><head><link attr1="value1" attr2="value2">some content</head>""");

assertThat(resultHtml).isEqualTo("""
Expand All @@ -55,11 +68,9 @@ void shouldParseAttributesAndReplaceLinkTags() {

@Test
void shouldReturnUnchangedHtmlWhenPropertyNotSet() {
System.setProperty("ch.sbb.polarion.extension.pdf-exporter.internalize-css-links", "false");

String resultHtml = htmlInlineLinksHelper.internalizeLinks("""
<html lang='en'><head><link attr1="value1" attr2="value2">some content</head>""");

when(pdfExporterExtensionConfiguration.htmlInternalizeCssLinks()).thenReturn(false);
String resultHtml = htmlLinksHelper.internalizeLinks("""
<html lang='en'><head><link attr1="value1" attr2="value2">some content</head>""");
assertThat(resultHtml).isEqualTo("""
<html lang='en'><head><link attr1="value1" attr2="value2">some content</head>""");
verify(linkInternalizer1, times(0)).inline(anyMap());
Expand Down

0 comments on commit 33e0f7a

Please sign in to comment.