From 8bc85bc65696034d2d7cecf6e352283a84dcf2b5 Mon Sep 17 00:00:00 2001 From: Tim Jacomb Date: Thu, 26 May 2022 09:09:01 +0100 Subject: [PATCH 01/10] Use recommended theme from theme manager --- .gitignore | 1 + pom.xml | 5 +++ .../io/jenkins/plugins/prism/PrismTheme.java | 40 +++++++++++++------ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index bf30871..a851465 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ pom.xml.versionsBackup pom.xml.releaseBackup release.properties .DS_Store +work/ diff --git a/pom.xml b/pom.xml index add8b7a..b79f1be 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,11 @@ org.jenkins-ci.plugins antisamy-markup-formatter + + io.jenkins.plugins + theme-manager + 1.3-rc110.51e65a_de84e0 + org.jsoup jsoup diff --git a/src/main/java/io/jenkins/plugins/prism/PrismTheme.java b/src/main/java/io/jenkins/plugins/prism/PrismTheme.java index 68a4a8e..40ab896 100644 --- a/src/main/java/io/jenkins/plugins/prism/PrismTheme.java +++ b/src/main/java/io/jenkins/plugins/prism/PrismTheme.java @@ -1,6 +1,8 @@ package io.jenkins.plugins.prism; import hudson.util.ListBoxModel; +import io.jenkins.plugins.thememanager.Theme; +import io.jenkins.plugins.thememanager.ThemeManagerPageDecorator; /** * Defines the active theme to be used when rendering the source code with Prism. @@ -8,29 +10,43 @@ * @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; } + private String getId() { + if (this == PrismTheme.PRISM) { + Theme theme = ThemeManagerPageDecorator.get().findTheme(); + return theme.getProperty("prism-api", "theme").orElse("prism"); + } + + return id; + } + public String getFileName() { - return fileName; + String id = getId(); + if (id.equals("prism")) { + return "prism.css"; + } + + return "prism-" + getId() + ".css"; } /** From e2d10886ef7885d23554e2c9807a7e08d419bf21 Mon Sep 17 00:00:00 2001 From: Tim Jacomb <21194782+timja@users.noreply.github.com> Date: Wed, 1 Jun 2022 09:07:39 +0100 Subject: [PATCH 02/10] Update pom.xml --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b79f1be..ef76fc3 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,7 @@ io.jenkins.plugins theme-manager - 1.3-rc110.51e65a_de84e0 + 1.3 org.jsoup From bcb1abf3d3b047595e97fef1e6b981ace51e9f36 Mon Sep 17 00:00:00 2001 From: Tim Jacomb <21194782+timja@users.noreply.github.com> Date: Wed, 1 Jun 2022 14:54:16 +0100 Subject: [PATCH 03/10] Update pom.xml --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ef76fc3..af64e51 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,7 @@ io.jenkins.plugins theme-manager - 1.3 + 1.4 org.jsoup From c68c098a6f966abb3f73e5d3d5c74193e2ef22f3 Mon Sep 17 00:00:00 2001 From: Tim Jacomb Date: Wed, 1 Jun 2022 23:03:03 +0100 Subject: [PATCH 04/10] Refactor for tests/static analysis --- .../io/jenkins/plugins/prism/PrismTheme.java | 16 ++++++++++------ .../plugins/prism/SourceCodeViewModel.java | 5 ++++- .../plugins/prism/PrismConfigurationTest.java | 3 ++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/prism/PrismTheme.java b/src/main/java/io/jenkins/plugins/prism/PrismTheme.java index 40ab896..fd04cb4 100644 --- a/src/main/java/io/jenkins/plugins/prism/PrismTheme.java +++ b/src/main/java/io/jenkins/plugins/prism/PrismTheme.java @@ -31,22 +31,26 @@ public String getDisplayName() { return displayName; } - private String getId() { + private String getId(Theme theme) { if (this == PrismTheme.PRISM) { - Theme theme = ThemeManagerPageDecorator.get().findTheme(); return theme.getProperty("prism-api", "theme").orElse("prism"); } return id; } - public String getFileName() { - String id = getId(); - if (id.equals("prism")) { + /** + * 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-" + getId() + ".css"; + return "prism-" + themeId + ".css"; } /** diff --git a/src/main/java/io/jenkins/plugins/prism/SourceCodeViewModel.java b/src/main/java/io/jenkins/plugins/prism/SourceCodeViewModel.java index 1eb176e..e565aaf 100644 --- a/src/main/java/io/jenkins/plugins/prism/SourceCodeViewModel.java +++ b/src/main/java/io/jenkins/plugins/prism/SourceCodeViewModel.java @@ -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; @@ -79,7 +81,8 @@ public String getSourceCode() { * @return the theme CSS file */ public String getThemeCssFileName() { - return PrismConfiguration.getInstance().getTheme().getFileName(); + Theme theme = ThemeManagerPageDecorator.get().findTheme(); + return PrismConfiguration.getInstance().getTheme().getFileName(theme); } } diff --git a/src/test/java/io/jenkins/plugins/prism/PrismConfigurationTest.java b/src/test/java/io/jenkins/plugins/prism/PrismConfigurationTest.java index 2befd8b..287eb02 100644 --- a/src/test/java/io/jenkins/plugins/prism/PrismConfigurationTest.java +++ b/src/test/java/io/jenkins/plugins/prism/PrismConfigurationTest.java @@ -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; @@ -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); From 2e900b324c13003857b7add85e9b6d62fbe9150f Mon Sep 17 00:00:00 2001 From: Tim Jacomb Date: Wed, 1 Jun 2022 23:11:16 +0100 Subject: [PATCH 05/10] Checkstyle --- src/main/java/io/jenkins/plugins/prism/PrismTheme.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/jenkins/plugins/prism/PrismTheme.java b/src/main/java/io/jenkins/plugins/prism/PrismTheme.java index fd04cb4..73f1365 100644 --- a/src/main/java/io/jenkins/plugins/prism/PrismTheme.java +++ b/src/main/java/io/jenkins/plugins/prism/PrismTheme.java @@ -40,7 +40,7 @@ private String getId(Theme theme) { } /** - * File name for the CSS to load for the prism theme + * 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 */ From 06ef1008e74c42e33dc643be03f170043c31f365 Mon Sep 17 00:00:00 2001 From: Tim Jacomb Date: Sun, 5 Jun 2022 20:40:07 +0100 Subject: [PATCH 06/10] Compatibility and an API for loading current theme via css --- README.adoc | 17 ++++++++++++++--- .../plugins/prism/PrismConfiguration.java | 16 ++++++++++++++++ .../io/jenkins/plugins/prism/PrismTheme.java | 11 +++++++++++ .../resources/io/jenkins/plugins/prism.jelly | 11 +---------- .../prism/SourceCodeViewModel/index.jelly | 14 +++++++++++--- src/main/resources/lib/prism/prism.jelly | 19 +++++++++++++++++++ src/main/resources/lib/prism/tablib | 0 7 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 src/main/resources/lib/prism/prism.jelly create mode 100644 src/main/resources/lib/prism/tablib diff --git a/README.adoc b/README.adoc index bfcd270..1f64ba8 100644 --- a/README.adoc +++ b/README.adoc @@ -51,13 +51,24 @@ Then you can use Prism in your jelly files using the following snippet: [source,xml] ---- - + + + +---- + +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]. diff --git a/src/main/java/io/jenkins/plugins/prism/PrismConfiguration.java b/src/main/java/io/jenkins/plugins/prism/PrismConfiguration.java index 3ced1a6..7da0eb2 100644 --- a/src/main/java/io/jenkins/plugins/prism/PrismConfiguration.java +++ b/src/main/java/io/jenkins/plugins/prism/PrismConfiguration.java @@ -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; @@ -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; @@ -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); + } } diff --git a/src/main/java/io/jenkins/plugins/prism/PrismTheme.java b/src/main/java/io/jenkins/plugins/prism/PrismTheme.java index 73f1365..68d6a09 100644 --- a/src/main/java/io/jenkins/plugins/prism/PrismTheme.java +++ b/src/main/java/io/jenkins/plugins/prism/PrismTheme.java @@ -53,6 +53,17 @@ public String getFileName(Theme theme) { return "prism-" + themeId + ".css"; } + /** + * Use {@link #getFileName(Theme)} instead. + * You probably shouldn't be calling this directly though and should be calling + * {@link PrismConfiguration#getThemeCssFileName()}. + */ + @Deprecated + public String getFileName() { + Theme theme = ThemeManagerPageDecorator.get().findTheme(); + return getFileName(theme); + } + /** * Returns all available themes in a {@link ListBoxModel}. * diff --git a/src/main/resources/io/jenkins/plugins/prism.jelly b/src/main/resources/io/jenkins/plugins/prism.jelly index d84a9e4..16e4fc4 100644 --- a/src/main/resources/io/jenkins/plugins/prism.jelly +++ b/src/main/resources/io/jenkins/plugins/prism.jelly @@ -3,19 +3,10 @@ Use it like --> - ${h.initPageVariables(context)} - - - -