mirror of
https://github.com/sismics/docs.git
synced 2024-12-22 11:23:48 +01:00
#79: CSS generator
This commit is contained in:
parent
8ad9c529b6
commit
7d7adeeca0
39
docs-core/src/main/java/com/sismics/util/css/Rule.java
Normal file
39
docs-core/src/main/java/com/sismics/util/css/Rule.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.sismics.util.css;
|
||||
|
||||
/**
|
||||
* A CSS rule.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class Rule {
|
||||
/**
|
||||
* Rule separator.
|
||||
*/
|
||||
private static String SEPARATOR = ": ";
|
||||
|
||||
/**
|
||||
* CSS rule property name.
|
||||
*/
|
||||
private String property;
|
||||
|
||||
/**
|
||||
* CSS rule value.
|
||||
*/
|
||||
private String value;
|
||||
|
||||
/**
|
||||
* Create a new CSS rule.
|
||||
*
|
||||
* @param property Property name
|
||||
* @param value Value
|
||||
*/
|
||||
public Rule(String property, String value) {
|
||||
this.property = property;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return property + SEPARATOR + value;
|
||||
}
|
||||
}
|
56
docs-core/src/main/java/com/sismics/util/css/Selector.java
Normal file
56
docs-core/src/main/java/com/sismics/util/css/Selector.java
Normal file
@ -0,0 +1,56 @@
|
||||
package com.sismics.util.css;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A CSS selector.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class Selector {
|
||||
/**
|
||||
* Selector name.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Rules in this selector.
|
||||
*/
|
||||
private List<Rule> ruleList;
|
||||
|
||||
/**
|
||||
* Create a new CSS selector.
|
||||
*
|
||||
* @param name Selector name
|
||||
*/
|
||||
public Selector(String name) {
|
||||
this.name = name;
|
||||
ruleList = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a CSS rule.
|
||||
*
|
||||
* @param property Property name
|
||||
* @param value Value
|
||||
* @return CSS selector
|
||||
*/
|
||||
public Selector rule(String property, String value) {
|
||||
ruleList.add(new Rule(property, value));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder(name);
|
||||
sb.append(" {\n");
|
||||
for (Rule rule : ruleList) {
|
||||
sb.append(" ");
|
||||
sb.append(rule);
|
||||
sb.append(";\n");
|
||||
}
|
||||
sb.append("}\n");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
20
docs-core/src/test/java/com/sismics/util/TestCss.java
Normal file
20
docs-core/src/test/java/com/sismics/util/TestCss.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.sismics.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.sismics.util.css.Selector;
|
||||
|
||||
/**
|
||||
* Test of CSS utilities.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class TestCss {
|
||||
@Test
|
||||
public void testBuildCss() {
|
||||
Selector selector = new Selector(".test")
|
||||
.rule("background-color", "yellow")
|
||||
.rule("font-family", "Comic Sans");
|
||||
System.out.println(selector);
|
||||
}
|
||||
}
|
@ -5,6 +5,8 @@ import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import com.sismics.util.css.Selector;
|
||||
|
||||
/**
|
||||
* Theme REST resources.
|
||||
*
|
||||
@ -22,8 +24,7 @@ public class ThemeResource extends BaseResource {
|
||||
@Produces("text/css")
|
||||
public Response stylesheet() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("body {\n");
|
||||
sb.append("}");
|
||||
sb.append(new Selector("body"));
|
||||
return Response.ok().entity(sb.toString()).build();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user