Skip to content

Commit 152a667

Browse files
committed
Renamed ScriptBox to JScriptBox.
1 parent f080e3f commit 152a667

File tree

9 files changed

+59
-60
lines changed

9 files changed

+59
-60
lines changed

src/main/java/com/diffplug/freshmark/CommentScript.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,18 @@ protected CommentScript(String intron, String exon, String regex) {
7979
/** Parser which splits up the raw document into structured tags which get passed to the compiler. */
8080
final Parser parser;
8181

82-
/** Compiles a single section/program/input combo into the appropriate output. */
82+
/** Compiles a single section/script/input combo into the appropriate output. */
8383
final Parser.Compiler compiler = new Parser.Compiler() {
8484
@Override
85-
public String compileSection(String section, String program, String input) {
85+
public String compileSection(String section, String script, String input) {
8686
return Errors.rethrow().get(() -> {
8787
ScriptEngine engine = setupScriptEngine(section);
8888

89-
// apply the templating engine to the program
90-
String templatedProgram = mustacheTemplate(program, key -> keyToValue(section, key));
89+
// apply the templating engine to the script
90+
String templatedProgram = template(section, script);
9191
// populate the input data
9292
engine.put("input", input);
93-
// evaluate the program and get the result
93+
// evaluate the script and get the result
9494
engine.eval(templatedProgram);
9595
String compiled = Check.cast(engine.get("output"), String.class);
9696
// make sure that the compiled output starts and ends with a newline,
@@ -101,11 +101,11 @@ public String compileSection(String section, String program, String input) {
101101
if (!compiled.endsWith("\n")) {
102102
compiled = compiled + "\n";
103103
}
104-
return parser.prefix + " " + section + "\n" +
105-
program +
106-
parser.postfix +
104+
return parser.intron + " " + section + "\n" +
105+
script +
106+
parser.exon +
107107
compiled +
108-
parser.prefix + " /" + section + " " + parser.postfix;
108+
parser.intron + " /" + section + " " + parser.exon;
109109
});
110110
}
111111
};

src/main/java/com/diffplug/freshmark/FreshMark.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import javax.script.ScriptException;
2828

2929
import com.diffplug.common.base.Errors;
30+
import com.diffplug.jscriptbox.JScriptBox;
3031
import com.diffplug.jscriptbox.Language;
31-
import com.diffplug.jscriptbox.ScriptBox;
3232

3333
/** The defaault implementation. */
3434
public class FreshMark extends CommentScript {
@@ -46,7 +46,7 @@ public FreshMark(Map<String, ?> properties, Consumer<String> warningStream) {
4646

4747
@Override
4848
protected ScriptEngine setupScriptEngine(String section) throws ScriptException {
49-
return ScriptBox.create()
49+
return JScriptBox.create()
5050
.setAll(properties)
5151
.set("link").toFunc2(FreshMark::link)
5252
.set("image").toFunc2(FreshMark::image)

src/main/java/com/diffplug/freshmark/Parser.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,15 @@
2121

2222
/** A format defined by "tag start" and "tag end" chunks of text. */
2323
class Parser {
24-
final String prefix, postfix;
24+
final String intron, exon;
2525
final Pattern pattern;
2626

27-
Parser(String prefix, String postfix, String regex) {
28-
this.prefix = prefix;
29-
this.postfix = postfix;
27+
Parser(String intron, String exon, String regex) {
28+
this.intron = intron;
29+
this.exon = exon;
3030
pattern = Pattern.compile(regex, Pattern.DOTALL);
3131
}
3232

33-
Parser(String prefix, String postfix) {
34-
this.prefix = prefix;
35-
this.postfix = postfix;
36-
pattern = Pattern.compile(Pattern.quote(prefix) + "(.*?)" + Pattern.quote(postfix), Pattern.DOTALL);
37-
}
38-
3933
/**
4034
* Given an input string, parses out the body sections from the tag sections.
4135
*
@@ -87,7 +81,7 @@ Consumer<String> wrap(Consumer<String> action) {
8781
numReadSoFar += input.length();
8882
} else {
8983
// tag
90-
String tag = prefix + input + postfix;
84+
String tag = intron + input + exon;
9185
assert(toRead.startsWith(tag));
9286
numReadSoFar += tag.length();
9387
}

src/main/java/com/diffplug/jscriptbox/ArityN.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package com.diffplug.jscriptbox;
1717

1818
/**
19-
* We'd like to pass lambdas to {@link ScriptBox} through a fluent API,
19+
* We'd like to pass lambdas to {@link JScriptBox} through a fluent API,
2020
* which means that we need to describe the function signature
2121
* in the method name.
2222
* <p>

src/main/java/com/diffplug/jscriptbox/ScriptBox.java renamed to src/main/java/com/diffplug/jscriptbox/JScriptBox.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@
3030
* ScriptBox makes it easy to expose both objects and
3131
* functions to client code in a language-agnostic way.
3232
*/
33-
public class ScriptBox {
33+
public class JScriptBox {
3434
private Map<String, Object> names = new HashMap<>();
3535

36-
protected ScriptBox() {}
36+
protected JScriptBox() {}
3737

3838
/** Creates a new NashornHarness and returns it. */
39-
public static ScriptBox create() {
40-
return new ScriptBox();
39+
public static JScriptBox create() {
40+
return new JScriptBox();
4141
}
4242

4343
/** Sets all of the properties contained in the given map. */
44-
public ScriptBox setAll(Map<String, ?> map) {
44+
public JScriptBox setAll(Map<String, ?> map) {
4545
names.putAll(map);
4646
return this;
4747
}
@@ -68,32 +68,32 @@ public NameSetter(String name) {
6868
this.name = checkValidIdentifier(name);
6969
}
7070

71-
public ScriptBox toValue(Object value) {
71+
public JScriptBox toValue(Object value) {
7272
names.put(name, value);
73-
return ScriptBox.this;
73+
return JScriptBox.this;
7474
}
7575

7676
// @formatter:off
77-
public ScriptBox toVoid0(Void0 value) { return toValue(value); }
78-
public <A> ScriptBox toVoid1(Void1<A> value) { return toValue(value); }
79-
public <A, B> ScriptBox toVoid2(Void2<A, B> value) { return toValue(value); }
80-
public <A, B, C> ScriptBox toVoid3(Void3<A, B, C> value) { return toValue(value); }
81-
public <A, B, C, D> ScriptBox toVoid4(Void4<A, B, C, D> value) { return toValue(value); }
82-
83-
public <R> ScriptBox toFunc0(Func0<R> value) { return toValue(value); }
84-
public <A, R> ScriptBox toFunc1(Func1<A, R> value) { return toValue(value); }
85-
public <A, B, R> ScriptBox toFunc2(Func2<A, B, R> value) { return toValue(value); }
86-
public <A, B, C, R> ScriptBox toFunc3(Func3<A, B, C, R> value) { return toValue(value); }
87-
public <A, B, C, D, R> ScriptBox toFunc4(Func4<A, B, C, D, R> value) { return toValue(value); }
77+
public JScriptBox toVoid0(Void0 value) { return toValue(value); }
78+
public <A> JScriptBox toVoid1(Void1<A> value) { return toValue(value); }
79+
public <A, B> JScriptBox toVoid2(Void2<A, B> value) { return toValue(value); }
80+
public <A, B, C> JScriptBox toVoid3(Void3<A, B, C> value) { return toValue(value); }
81+
public <A, B, C, D> JScriptBox toVoid4(Void4<A, B, C, D> value) { return toValue(value); }
82+
83+
public <R> JScriptBox toFunc0(Func0<R> value) { return toValue(value); }
84+
public <A, R> JScriptBox toFunc1(Func1<A, R> value) { return toValue(value); }
85+
public <A, B, R> JScriptBox toFunc2(Func2<A, B, R> value) { return toValue(value); }
86+
public <A, B, C, R> JScriptBox toFunc3(Func3<A, B, C, R> value) { return toValue(value); }
87+
public <A, B, C, D, R> JScriptBox toFunc4(Func4<A, B, C, D, R> value) { return toValue(value); }
8888
// @formatter:on
8989
}
9090

91-
/** Returns a ScriptEngine with the stuff above. */
91+
/** Returns a {@link ScriptEngine} which has been populated with the values of this box. */
9292
public ScriptEngine build(Language language) throws ScriptException {
9393
return language.initializeEngine(names);
9494
}
9595

96-
/** Returns a {@link TypedScriptEngine} with the stuff above. */
96+
/** Returns a {@link TypedScriptEngine} which has been populated with the values of this box. */
9797
public TypedScriptEngine buildTyped(Language language) throws ScriptException {
9898
return new TypedScriptEngine(build(language));
9999
}

src/main/java/com/diffplug/jscriptbox/Language.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import javax.script.ScriptEngineManager;
2323
import javax.script.ScriptException;
2424

25-
/** Interface which converts the result of a {@link ScriptBox} into a {@link ScriptEngine}. */
25+
/** Interface which converts the result of a {@link JScriptBox} into a {@link ScriptEngine}. */
2626
public interface Language {
2727
/**
2828
* @param names a map from names which should be defined in the script to their objects, particularly elements of {@link ArityN}.
@@ -31,7 +31,7 @@ public interface Language {
3131
*/
3232
ScriptEngine initializeEngine(Map<String, Object> names) throws ScriptException;
3333

34-
/** Language implementation for nashorn. */
34+
/** Language implementation for javascript using the nashorn engine. */
3535
public static Language javascript() {
3636
return map -> {
3737
ScriptEngine jsEngine = new ScriptEngineManager().getEngineByName("nashorn");

src/main/java/com/diffplug/jscriptbox/TypedScriptEngine.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public <T> Optional<T> getOpt(String name, Class<T> clazz) throws ScriptExceptio
6969
return Check.castOpt(scriptEngine.get(name), clazz);
7070
}
7171

72+
/** Puts the given value into the script engine. */
73+
public void put(String name, Object value) {
74+
scriptEngine.put(name, value);
75+
}
76+
7277
/** The underlying ScriptEngine. */
7378
public ScriptEngine getRaw() {
7479
return scriptEngine;

src/test/java/com/diffplug/freshmark/ParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.diffplug.common.base.StringPrinter;
2424

2525
public class ParserTest {
26-
static final Parser freshmarkParser = new Parser("<!---freshmark", "-->");
26+
static final Parser freshmarkParser = new FreshMark(null, null).parser;
2727

2828
@Test
2929
public void testBodyAndTags() {

src/test/java/com/diffplug/scriptbox/ScriptBoxNashornTest.java renamed to src/test/java/com/diffplug/scriptbox/JScriptBoxNashornTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@
2424
import org.junit.Assert;
2525
import org.junit.Test;
2626

27+
import com.diffplug.jscriptbox.JScriptBox;
2728
import com.diffplug.jscriptbox.Language;
28-
import com.diffplug.jscriptbox.ScriptBox;
2929

30-
public class ScriptBoxNashornTest {
30+
public class JScriptBoxNashornTest {
3131
@Test
3232
public void testBasicExpressions() throws ScriptException {
33-
ScriptEngine engine = ScriptBox.create().build(Language.javascript());
33+
ScriptEngine engine = JScriptBox.create().build(Language.javascript());
3434
Assert.assertEquals("abc", engine.eval("'abc'"));
3535
Assert.assertEquals(123, engine.eval("123"));
3636
Assert.assertEquals(123.5, engine.eval("123.5"));
3737
}
3838

3939
@Test
4040
public void testBasicScript() throws ScriptException {
41-
ScriptEngine engine = ScriptBox.create().build(Language.javascript());
41+
ScriptEngine engine = JScriptBox.create().build(Language.javascript());
4242
engine.eval("var txt = 'abc';" +
4343
"var int = 123;" +
4444
"var float = 123.5;");
@@ -53,7 +53,7 @@ public void testBasicScript() throws ScriptException {
5353
@Test
5454
public void testVoid0() throws ScriptException {
5555
AtomicBoolean wasRun = new AtomicBoolean(false);
56-
ScriptEngine engine = ScriptBox.create()
56+
ScriptEngine engine = JScriptBox.create()
5757
.set("void0").toVoid0(() -> wasRun.set(true))
5858
.build(Language.javascript());
5959
engine.eval("void0()");
@@ -63,7 +63,7 @@ public void testVoid0() throws ScriptException {
6363
@Test
6464
public void testVoid1() throws ScriptException {
6565
AtomicReference<String> arg1 = new AtomicReference<>();
66-
ScriptEngine engine = ScriptBox.create()
66+
ScriptEngine engine = JScriptBox.create()
6767
.set("void1").toVoid1(arg1::set)
6868
.build(Language.javascript());
6969
engine.eval("void1('it lives!')");
@@ -74,7 +74,7 @@ public void testVoid1() throws ScriptException {
7474
public void testVoid2() throws ScriptException {
7575
AtomicReference<Object> arg1 = new AtomicReference<>();
7676
AtomicReference<Object> arg2 = new AtomicReference<>();
77-
ScriptEngine engine = ScriptBox.create()
77+
ScriptEngine engine = JScriptBox.create()
7878
.set("void2").toVoid2((a, b) -> {
7979
arg1.set(a);
8080
arg2.set(b);
@@ -90,7 +90,7 @@ public void testVoid3() throws ScriptException {
9090
AtomicReference<Object> arg1 = new AtomicReference<>();
9191
AtomicReference<Object> arg2 = new AtomicReference<>();
9292
AtomicReference<Object> arg3 = new AtomicReference<>();
93-
ScriptEngine engine = ScriptBox.create()
93+
ScriptEngine engine = JScriptBox.create()
9494
.set("void3").toVoid3((a, b, c) -> {
9595
arg1.set(a);
9696
arg2.set(b);
@@ -109,7 +109,7 @@ public void testVoid4() throws ScriptException {
109109
AtomicReference<Object> arg2 = new AtomicReference<>();
110110
AtomicReference<Object> arg3 = new AtomicReference<>();
111111
AtomicReference<Object> arg4 = new AtomicReference<>();
112-
ScriptEngine engine = ScriptBox.create()
112+
ScriptEngine engine = JScriptBox.create()
113113
.set("void4").toVoid4((a, b, c, d) -> {
114114
arg1.set(a);
115115
arg2.set(b);
@@ -129,15 +129,15 @@ public void testVoid4() throws ScriptException {
129129
//////////////////////////////
130130
@Test
131131
public void testFunc0() throws ScriptException {
132-
ScriptEngine engine = ScriptBox.create()
132+
ScriptEngine engine = JScriptBox.create()
133133
.set("func0").toFunc0(() -> "wassup")
134134
.build(Language.javascript());
135135
Assert.assertEquals("wassup", engine.eval("func0()"));
136136
}
137137

138138
@Test
139139
public void testFunc1() throws ScriptException {
140-
ScriptEngine engine = ScriptBox.create()
140+
ScriptEngine engine = JScriptBox.create()
141141
.set("func1").toFunc1(a -> a)
142142
.build(Language.javascript());
143143
Assert.assertEquals("identity", engine.eval("func1('identity')"));
@@ -147,23 +147,23 @@ public void testFunc1() throws ScriptException {
147147

148148
@Test
149149
public void testFunc2() throws ScriptException {
150-
ScriptEngine engine = ScriptBox.create()
150+
ScriptEngine engine = JScriptBox.create()
151151
.set("func2").toFunc2((String a, String b) -> a + b)
152152
.build(Language.javascript());
153153
Assert.assertEquals("ab", engine.eval("func2('a', 'b')"));
154154
}
155155

156156
@Test
157157
public void testFunc3() throws ScriptException {
158-
ScriptEngine engine = ScriptBox.create()
158+
ScriptEngine engine = JScriptBox.create()
159159
.set("func3").toFunc3((String a, String b, String c) -> a + b + c)
160160
.build(Language.javascript());
161161
Assert.assertEquals("abc", engine.eval("func3('a', 'b', 'c')"));
162162
}
163163

164164
@Test
165165
public void testFunc4() throws ScriptException {
166-
ScriptEngine engine = ScriptBox.create()
166+
ScriptEngine engine = JScriptBox.create()
167167
.set("func4").toFunc4((String a, String b, String c, String d) -> a + b + c + d)
168168
.build(Language.javascript());
169169
Assert.assertEquals("abcd", engine.eval("func4('a', 'b', 'c', 'd')"));

0 commit comments

Comments
 (0)