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

Add style parameter to SvgTag (regular, solid, brands) #178

Merged
merged 2 commits into from
Sep 22, 2022
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
2 changes: 1 addition & 1 deletion go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e

JENKINS_HOME=../docker/volumes/jenkins-home

mvn clean install || { echo "Build failed"; exit 1; }
mvn clean install -Pskip || { echo "Build failed"; exit 1; }

echo "Installing plugin in $JENKINS_HOME"

Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.revapi</groupId>
<artifactId>revapi-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
58 changes: 55 additions & 3 deletions src/main/java/io/jenkins/plugins/fontawesome/api/SvgTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;

import j2html.tags.ContainerTag;

import io.jenkins.plugins.util.JenkinsFacade;
Expand All @@ -12,13 +14,22 @@
* @author Ullrich Hafner
*/
public class SvgTag {
/** Available Font Awesome styles. */
public enum FontAwesomeStyle {
SOLID,
REGULAR,
BRANDS
}

private static final String SVG_ICON = "fa-svg-icon";
private static final String ICON_PREFIX = "/plugin/font-awesome-api/sprites/solid.svg#";
private static final String ICON_PREFIX = "/plugin/font-awesome-api/sprites/";
private static final String ICON_SUFFIX = ".svg#";

private final ContainerTag container;

/**
* Creates a new {@link SvgTag} that renders the specified SVG icon of FontAwesome.
* Creates a new {@link SvgTag} that renders the specified SVG icon of FontAwesome using
* {@link FontAwesomeStyle#SOLID}.
*
* @param iconName
* the name of the icon (without fa- prefix), e.g. {@code chevron-circle-up}.
Expand All @@ -34,11 +45,38 @@ public static SvgTag fontAwesomeSvgIcon(final String iconName) {
*
* @param iconName
* the name of the icon (without fa- prefix), e.g. {@code chevron-circle-up}.
* @param style
* Font Awesome style of the icon
*
* @return the HTML tag
*/
public static SvgTag fontAwesomeSvgIcon(final String iconName, final FontAwesomeStyle style) {
return new SvgTag(iconName, style);
}

/**
* Creates a new {@link SvgTag} that renders the specified SVG icon of FontAwesome using
* {@link FontAwesomeStyle#SOLID}.
*
* @param iconName
* the name of the icon (without fa- prefix), e.g. {@code chevron-circle-up}.
*/
public SvgTag(final String iconName) {
this(iconName, new JenkinsFacade());
}

/**
* Creates a new {@link SvgTag} that renders the specified SVG icon of FontAwesome.
*
* @param iconName
* the name of the icon (without fa- prefix), e.g. {@code chevron-circle-up}.
* @param style
* Font Awesome style of the icon
*/
public SvgTag(final String iconName, final FontAwesomeStyle style) {
this(iconName, new JenkinsFacade(), style);
}

/**
* Creates a new {@link SvgTag} that renders the specified SVG icon of FontAwesome.
*
Expand All @@ -48,9 +86,23 @@ public SvgTag(final String iconName) {
* Jenkins facade to replaced with a stub during unit tests
*/
public SvgTag(final String iconName, final JenkinsFacade jenkinsFacade) {
this(iconName, jenkinsFacade, FontAwesomeStyle.SOLID);
}

/**
* Creates a new {@link SvgTag} that renders the specified SVG icon of FontAwesome.
*
* @param iconName
* the name of the icon (without fa- prefix), e.g. {@code chevron-circle-up}.
* @param jenkinsFacade
* Jenkins facade to replaced with a stub during unit tests
* @param style
* Font Awesome style of the icon
*/
public SvgTag(final String iconName, final JenkinsFacade jenkinsFacade, final FontAwesomeStyle style) {
container = new ContainerTag("svg")
.withClasses(SVG_ICON)
.with(use().withHref(jenkinsFacade.getImagePath(ICON_PREFIX + iconName)));
.with(use().withHref(jenkinsFacade.getImagePath(ICON_PREFIX + StringUtils.lowerCase(style.name()) + ICON_SUFFIX + iconName)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.jupiter.api.Test;

import io.jenkins.plugins.fontawesome.api.SvgTag.FontAwesomeStyle;
import io.jenkins.plugins.util.JenkinsFacade;

import static org.assertj.core.api.Assertions.*;
Expand All @@ -16,9 +17,17 @@ class SvgTagCreatorTest {
@Test
void shouldCreateIcon() {
JenkinsFacade jenkins = mock(JenkinsFacade.class);
when(jenkins.getImagePath(anyString())).thenReturn("path");
when(jenkins.getImagePath("/plugin/font-awesome-api/sprites/solid.svg#hello")).thenReturn("path-solid");

assertThat(new SvgTag("hello", jenkins).render())
.isEqualTo("<svg class=\"fa-svg-icon\"><use href=\"path\"></use></svg>");
.isEqualTo("<svg class=\"fa-svg-icon\"><use href=\"path-solid\"></use></svg>");

when(jenkins.getImagePath("/plugin/font-awesome-api/sprites/regular.svg#hello")).thenReturn("path-regular");
SvgTag tag = new SvgTag("hello", jenkins, FontAwesomeStyle.REGULAR);
assertThat(tag.render())
.isEqualTo("<svg class=\"fa-svg-icon\"><use href=\"path-regular\"></use></svg>");

assertThat(tag.withClasses("icon-md").render())
.isEqualTo("<svg class=\"icon-md svg-icon\"><use href=\"path-regular\"></use></svg>");
}
}