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
37 changes: 33 additions & 4 deletions src/main/java/io/jenkins/plugins/prism/SourcePrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ class SourcePrinter {

private static final ColumnMarker COLUMN_MARKER = new ColumnMarker("-n/a-");
private static final String QT_LINGUIST_PATTERN = "<!DOCTYPE TS>";
private static final int MAX_LINES_FOR_SYNTAX_HIGHLIGHTING = 5_000;
private static final String LINE_NUMBERS = "line-numbers";
private static final String MATCH_BRACES = "match-braces";
private static final String ICON_MD = "icon-md";
private static final char NEW_LINE = '\n';

private final JenkinsFacade jenkinsFacade;

Expand Down Expand Up @@ -72,20 +74,47 @@ String render(final String fileName, final Stream<String> lines, final Marker ma
StringBuilder after = readBlockUntilLine(stream, Integer.MAX_VALUE);

String language = selectLanguageClass(fileName, before);
String code = asCode(before, language, LINE_NUMBERS, MATCH_BRACES)
+ asMarkedCode(marked, marker, language, LINE_NUMBERS, "highlight", MATCH_BRACES)
boolean enableSyntaxHighlighting = shouldEnableSyntaxHighlighting(before, marked, after);
String code = asCode(before, getCodeClasses(language, enableSyntaxHighlighting))
+ asMarkedCode(marked, marker, getMarkedCodeClasses(language, enableSyntaxHighlighting))
+ createInfoPanel(marker)
+ asCode(after, language, LINE_NUMBERS, MATCH_BRACES);
+ asCode(after, getCodeClasses(language, enableSyntaxHighlighting));

return pre().with(new UnescapedText(code)).renderFormatted();
}
}

private boolean shouldEnableSyntaxHighlighting(
final StringBuilder before, final StringBuilder marked, final StringBuilder after) {
return countLines(before) + countLines(marked) + countLines(after) <= MAX_LINES_FOR_SYNTAX_HIGHLIGHTING;
}

private int countLines(final StringBuilder text) {
if (text.isEmpty()) {
return 0;
}
return (int) text.chars().filter(c -> c == NEW_LINE).count();
}
Comment on lines +92 to +97
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with sb.chars().filter(c -> c == '\n').count()


private String[] getCodeClasses(final String language, final boolean enableSyntaxHighlighting) {
if (enableSyntaxHighlighting) {
return new String[] {language, LINE_NUMBERS, MATCH_BRACES};
}
return new String[0];
}

private String[] getMarkedCodeClasses(final String language, final boolean enableSyntaxHighlighting) {
if (enableSyntaxHighlighting) {
return new String[] {language, LINE_NUMBERS, "highlight", MATCH_BRACES};
}
return new String[] {"highlight"};
}

private StringBuilder readBlockUntilLine(final LookaheadStream stream, final int end) {
StringBuilder marked = new StringBuilder();
while (stream.hasNext() && stream.getLine() < end) {
marked.append(stream.next());
marked.append("\n");
marked.append(NEW_LINE);
}
return marked;
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/io/jenkins/plugins/prism/SourcePrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,38 @@ void shouldRenderQtTranslationFileAsMarkup() {
.contains("language-markup");
}

@Test
@org.junitpioneer.jupiter.Issue("JENKINS-73298")
void shouldSkipSyntaxHighlightingForLargeFiles() {
Marker issue = new MarkerBuilder().withLineStart(2_500).build();
SourcePrinter printer = new SourcePrinter();

Document document = Jsoup.parse(printer.render("sample.xml",
Stream.generate(() -> "line").limit(5_001), issue));

assertThat(document.getElementsByTag("code").first())
.isNotNull();
assertThat(document.getElementsByTag("code").first().classNames())
.doesNotContain("language-markup", "line-numbers", "match-braces");
assertThat(document.getElementsByTag("code").get(1).classNames())
.containsExactly("highlight");
}

@Test
@org.junitpioneer.jupiter.Issue("JENKINS-73298")
void shouldKeepSyntaxHighlightingForFilesWithinLimit() {
Marker issue = new MarkerBuilder().withLineStart(2_500).build();
SourcePrinter printer = new SourcePrinter();

Document document = Jsoup.parse(printer.render("sample.xml",
Stream.generate(() -> "line").limit(5_000), issue));

assertThat(document.getElementsByTag("code").first())
.isNotNull();
assertThat(document.getElementsByTag("code").first().classNames())
.contains("language-markup", "line-numbers", "match-braces");
}

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