Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use recommended theme from theme manager #41

Closed
wants to merge 10 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ pom.xml.versionsBackup
pom.xml.releaseBackup
release.properties
.DS_Store
work/

src/main/webapp/css/**/prism*.css
src/main/webapp/js/**/prism*.js
src/main/webapp/js/index.js
17 changes: 14 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,24 @@ Then you can use Prism in your jelly files using the following snippet:

[source,xml]
----
<st:adjunct includes="io.jenkins.plugins.prism"/>
<j:jelly xmlns:j="jelly:core" xmlns:p="/prism">
<p:prism prismConfiguration="${it.prismConfiguration}"/>
</j:jelly>
----

In your descriptor you will need to add a `getPrismConfiguration()` method:

[source,java]
----
public PrismConfiguration getPrismConfiguration() {
return PrismConfiguration.getInstance();
}
----

== Examples

Currently, no additional help is available for this plugin. You can have a look into the
For source code rendering look into the
https://github.com/jenkinsci/warnings-ng-plugin[warnings plugin] that
uses the `SourceCodeViewModel` to render the source code with the selected warning.


For general prism API integration see https://github.com/jenkinsci/design-library-plugin/pull/72[design-library-plugin PR#72].
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>antisamy-markup-formatter</artifactId>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>theme-manager</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
Expand Down Expand Up @@ -178,7 +183,7 @@
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.artifactId}/js</outputDirectory>
<outputDirectory>${project.basedir}/src/main/webapp/js</outputDirectory>
Copy link
Member Author

Choose a reason for hiding this comment

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

maven-hpi-plugin cannot find resources if they are in the build directory. They need to be in the webapp directory to be picked up.

It works on a full winstone server but not the jetty dev server.

This also breaks SNAPSHOT dependencies across different plugins, e.g. creating a SNAPSHOT of this plugin and running it in the design library did not work for me

I think there's similar issues throughout the echarts / bootstrap plugins

Copy link
Member

Choose a reason for hiding this comment

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

This looks like a bug in the maven-hpi-plugin, can't we fix that there? Copying generated sources to the src folder violates all maven standards and degenerates the developer experience in IntelliJ.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unsure the impacts of changing it or if it supports multiple paths etc.

This is where the code is I believe:
https://github.com/jenkinsci/maven-hpi-plugin/blob/a041c9f3ec9675ae0ae3b56b3aa49a3c3ab7f089/src/main/java/org/jenkinsci/maven/plugins/hpi/HplMojo.java#L97-L98

degenerates the developer experience in IntelliJ.
Without this fix the developer experience doesn't work at all in IntelliJ it's 100% broken.

I assume you use that docker dev thing here without hpi:run? (that's not how plugins are normally developed though)

I assume your complaint is around generated resources showing up in search, if we can't fix maven-hpi-plugin safely then we could likely include some config in this repo to mark it as excluded.

<resources>
<resource>
<directory>${project.basedir}/node_modules/prismjs/components</directory>
Expand All @@ -201,7 +206,7 @@
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.artifactId}/css</outputDirectory>
<outputDirectory>${project.basedir}/src/main/webapp/css</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/node_modules/prismjs/themes</directory>
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/io/jenkins/plugins/prism/PrismConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.jenkins.plugins.prism;

import io.jenkins.plugins.thememanager.Theme;
import io.jenkins.plugins.thememanager.ThemeManagerPageDecorator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -9,6 +11,8 @@
import edu.hm.hafner.util.PathUtil;
import edu.hm.hafner.util.VisibleForTesting;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.verb.POST;
import org.jenkinsci.Symbol;
Expand Down Expand Up @@ -158,4 +162,16 @@ public ListBoxModel doFillThemeItems() {
}
return options;
}

/**
* Returns the filename of the prism theme. Themes are stored in the package below the css folder.
*
* @return the theme CSS file
*/
@Restricted(NoExternalUse.class)
@SuppressWarnings("unused") // used in jelly
public static String getThemeCssFileName() {
Theme theme = ThemeManagerPageDecorator.get().findTheme();
return PrismConfiguration.getInstance().getTheme().getFileName(theme);
}
}
45 changes: 32 additions & 13 deletions src/main/java/io/jenkins/plugins/prism/PrismTheme.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,55 @@
package io.jenkins.plugins.prism;

import hudson.util.ListBoxModel;
import io.jenkins.plugins.thememanager.Theme;

/**
* Defines the active theme to be used when rendering the source code with Prism.
*
* @author Ullrich Hafner
*/
public enum PrismTheme {
PRISM("Default", "prism.css"),
COY("Coy", "prism-coy.css"),
DARK("Dark", "prism-dark.css"),
FUNKY("Funky", "prism-funky.css"),
OKAIDIA("Okaidia", "prism-okaidia.css"),
SOLARIZED_LIGHT("Solarized Light", "prism-solarizedlight.css"),
TOMORROW_NIGHT("Tomorrow Night", "prism-tomorrow.css"),
TWILIGHT("Twilight", "prism-twilight.css");
PRISM("Recommended", null),
COY("Coy", "coy"),
DARK("Dark", "dark"),
FUNKY("Funky", "funky"),
OKAIDIA("Okaidia", "okaidia"),
SOLARIZED_LIGHT("Solarized Light", "solarizedlight"),
TOMORROW_NIGHT("Tomorrow Night", "tomorrow"),
TWILIGHT("Twilight", "twilight");

private final String displayName;
private final String fileName;
private final String id;

PrismTheme(final String displayName, final String fileName) {
PrismTheme(final String displayName, final String id) {
this.displayName = displayName;
this.fileName = fileName;
this.id = id;
}

public String getDisplayName() {
return displayName;
}

public String getFileName() {
return fileName;
private String getId(Theme theme) {
if (this == PrismTheme.PRISM) {
return theme.getProperty("prism-api", "theme").orElse("prism");
}

return id;
}

/**
* File name for the CSS to load for the prism theme.
* @param theme the currently active theme from theme manager plugin
* @return relative file path to the theme to load
*/
public String getFileName(Theme theme) {
String themeId = getId(theme);
if ("prism".equals(themeId)) {
return "prism.css";
}

return "prism-" + themeId + ".css";
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/io/jenkins/plugins/prism/SourceCodeViewModel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.jenkins.plugins.prism;

import io.jenkins.plugins.thememanager.Theme;
import io.jenkins.plugins.thememanager.ThemeManagerPageDecorator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
Expand All @@ -8,6 +10,8 @@

import hudson.model.ModelObject;
import hudson.model.Run;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* Renders a source code file with Prism syntax highlighting in a separate Jenkins view. Optionally, highlights a marker
Expand Down Expand Up @@ -73,13 +77,9 @@ public String getSourceCode() {
return sourceCode;
}

/**
* Returns the filename of the prism theme. Themes are stored in the package below the css folder.
*
* @return the theme CSS file
*/
public String getThemeCssFileName() {
return PrismConfiguration.getInstance().getTheme().getFileName();
@Restricted(NoExternalUse.class) // stapler only
public PrismConfiguration getPrismConfiguration() {
return PrismConfiguration.getInstance();
}
}

11 changes: 1 addition & 10 deletions src/main/resources/io/jenkins/plugins/prism.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@
Use it like <st:adjunct includes="io.jenkins.plugins.prism"/>
-->
<j:jelly xmlns:j="jelly:core">

<j:new var="h" className="hudson.Functions" />
${h.initPageVariables(context)}

<link type="text/css" rel="stylesheet" href="${resURL}/plugin/prism-api/css/line-highlight/prism-line-highlight.css"/>
<link type="text/css" rel="stylesheet" href="${resURL}/plugin/prism-api/css/line-numbers/prism-line-numbers.css"/>
<link type="text/css" rel="stylesheet" href="${resURL}/plugin/prism-api/css/match-braces/prism-match-braces.css"/>

<script type="text/javascript" src="${resURL}/plugin/prism-api/js/prism-core.min.js"/>
<script type="text/javascript" src="${resURL}/plugin/prism-api/js/autoloader/prism-autoloader.min.js"
data-autoloader-path="${resURL}/plugin/prism-api/js/"/>
<script type="text/javascript" src="${resURL}/plugin/prism-api/js/line-highlight/prism-line-highlight.min.js"/>
<script type="text/javascript" src="${resURL}/plugin/prism-api/js/line-numbers/prism-line-numbers.min.js"/>
<script type="text/javascript" src="${resURL}/plugin/prism-api/js/match-braces/prism-match-braces.min.js"/>
<script type="text/javascript" src="${resURL}/plugin/prism-api/js/keep-markup/prism-keep-markup.min.js"/>
</j:jelly>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:bs="/bootstrap5">
<j:jelly xmlns:p="/lib/prism" xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:bs="/bootstrap5">

<st:header name="Content-Type" value="text/html;charset=UTF-8"/>

<bs:page it="${it}" notitle="true">

<st:adjunct includes="io.jenkins.plugins.prism"/>
<p:prism prismConfiguration="${it.prismConfiguration}" />
timja marked this conversation as resolved.
Show resolved Hide resolved

<link type="text/css" rel="stylesheet" href="${resURL}/plugin/prism-api/css/line-highlight/prism-line-highlight.css"/>
<link type="text/css" rel="stylesheet" href="${resURL}/plugin/prism-api/css/line-numbers/prism-line-numbers.css"/>
<link type="text/css" rel="stylesheet" href="${resURL}/plugin/prism-api/css/match-braces/prism-match-braces.css"/>
<script type="text/javascript" src="${resURL}/plugin/prism-api/js/line-highlight/prism-line-highlight.min.js"/>
<script type="text/javascript" src="${resURL}/plugin/prism-api/js/line-numbers/prism-line-numbers.min.js"/>
<script type="text/javascript" src="${resURL}/plugin/prism-api/js/match-braces/prism-match-braces.min.js"/>
<script type="text/javascript" src="${resURL}/plugin/prism-api/js/keep-markup/prism-keep-markup.min.js"/>

<link type="text/css" rel="stylesheet" href="${resURL}/plugin/prism-api/css/custom-prism.css"/>
<link type="text/css" rel="stylesheet" href="${resURL}/plugin/prism-api/css/${it.themeCssFileName}"/>

<h1>${%sourcedetail.header(it.displayName)}</h1>

Expand Down
19 changes: 19 additions & 0 deletions src/main/resources/prism/prism.jelly
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler">
<st:documentation>
<!--
I wasn't able to retrieve this at runtime due to invokeStatic not using the right classloader,
which is why the user has to pass it in :(.
-->
<st:attribute name="prismConfiguration">
Pass a reference to the 'PrismConfiguration' instance.
</st:attribute>
</st:documentation>

<st:adjunct includes="io.jenkins.plugins.prism" />
<st:once>
<link type="text/css" rel="stylesheet" href="${resURL}/plugin/prism-api/css/${prismConfiguration.themeCssFileName}"/>
</st:once>


</j:jelly>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.jenkins.plugins.prism;

import io.jenkins.plugins.thememanager.Theme;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -121,7 +122,7 @@ void shouldInitializeThemes() {

assertThat(configuration.getTheme())
.isEqualTo(PrismTheme.PRISM)
.extracting(PrismTheme::getFileName)
.extracting(a -> a.getFileName(Theme.builder().build()))
.isEqualTo("prism.css");
configuration.setTheme(PrismTheme.COY);
assertThat(configuration.getTheme()).isEqualTo(PrismTheme.COY);
Expand Down