Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/main/java/io/jenkins/plugins/prism/SourcePrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jenkins.ui.symbol.Symbol;
import org.jenkins.ui.symbol.SymbolRequest;
import org.jenkins.ui.symbol.SymbolRequest.Builder;
import org.apache.commons.lang3.Strings;

import edu.hm.hafner.util.LookaheadStream;
import edu.hm.hafner.util.VisibleForTesting;
Expand All @@ -29,6 +30,7 @@
private static final Sanitizer SANITIZER = new Sanitizer();

private static final ColumnMarker COLUMN_MARKER = new ColumnMarker("-n/a-");
private static final String QT_LINGUIST_PATTERN = "<!DOCTYPE TS>";
private static final String LINE_NUMBERS = "line-numbers";
private static final String MATCH_BRACES = "match-braces";
private static final String ICON_MD = "icon-md";
Expand Down Expand Up @@ -69,7 +71,7 @@
StringBuilder marked = readBlockUntilLine(stream, end);
StringBuilder after = readBlockUntilLine(stream, Integer.MAX_VALUE);

String language = selectLanguageClass(fileName);
String language = selectLanguageClass(fileName, before);
String code = asCode(before, language, LINE_NUMBERS, MATCH_BRACES)
+ asMarkedCode(marked, marker, language, LINE_NUMBERS, "highlight", MATCH_BRACES)
+ createInfoPanel(marker)
Expand Down Expand Up @@ -150,8 +152,14 @@
}

@SuppressWarnings({"javancss", "PMD.CyclomaticComplexity"})
private String selectLanguageClass(final String fileName) {
return switch (StringUtils.substringAfterLast(fileName, ".")) {
private String selectLanguageClass(final String fileName, final StringBuilder before) {
String extension = StringUtils.substringAfterLast(fileName, ".");

if ("ts".equals(extension) && Strings.CS.contains(before, QT_LINGUIST_PATTERN)) {
return "language-markup";
}

return switch (extension) {

Check warning on line 162 in src/main/java/io/jenkins/plugins/prism/SourcePrinter.java

View workflow job for this annotation

GitHub Actions / Quality Monitor - Quality gates failed

Partially covered line

Line 162 is only partially covered, 22 branches are missing

Check warning on line 162 in src/main/java/io/jenkins/plugins/prism/SourcePrinter.java

View workflow job for this annotation

GitHub Actions / Quality Monitor - Quality gates failed

Partially covered line

Line 162 is only partially covered, 22 branches are missing

Check warning on line 162 in src/main/java/io/jenkins/plugins/prism/SourcePrinter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 162 is only partially covered, 22 branches are missing
case "htm", "html", "xml", "xsd" -> "language-markup";
case "css" -> "language-css";
case "js" -> "language-javascript";
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/io/jenkins/plugins/prism/SourcePrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import io.jenkins.plugins.prism.Marker.MarkerBuilder;
import io.jenkins.plugins.util.JenkinsFacade;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -221,6 +223,49 @@ void shouldRenderXmlFiles() {
assertThat(pre.text()).isEqualToIgnoringWhitespace(expectedFile);
}

@Test
@org.junitpioneer.jupiter.Issue("JENKINS-64584")
void shouldRenderTypeScriptFileAsTypeScript() {
Marker issue = new MarkerBuilder().build();
SourcePrinter printer = new SourcePrinter();

Document document = Jsoup.parse(printer.render("sample.ts", Stream.of("const answer: number = 42;"), issue));

assertThat(document.getElementsByTag("code").first())
.isNotNull();
assertThat(document.getElementsByTag("code").first().classNames())
.contains("language-typescript");
}

@Test
@org.junitpioneer.jupiter.Issue("JENKINS-64584")
void shouldRenderQtTranslationFileAsMarkup() {
Marker issue = new MarkerBuilder().withLineStart(7).build();
SourcePrinter printer = new SourcePrinter();

Document document = Jsoup.parse(printer.render("sample.ts", Stream.of(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>",
"<!DOCTYPE TS>",
"<TS version=\"2.1\" language=\"en_US\">",
" <context>",
" <name>MainWindow</name>",
" <message>",
" <source>Hello, world!</source>",
" <translation>Hello, world!</translation>",
" </message>",
" <message>",
" <source>File not found</source>",
" <translation>File not found</translation>",
" </message>",
" </context>",
"</TS>"), issue));

assertThat(document.getElementsByTag("code").first())
.isNotNull();
assertThat(document.getElementsByTag("code").first().classNames())
.contains("language-markup");
}

private JenkinsFacade createJenkinsFacade() {
JenkinsFacade jenkinsFacade = mock(JenkinsFacade.class);
when(jenkinsFacade.getImagePath(anyString())).thenReturn("/path/to/icon");
Expand Down
Loading