Skip to content

Commit dcaf010

Browse files
committed
Extracted mustache-based templating out of CommentScript and into CommentScriptMustache.
1 parent 152a667 commit dcaf010

File tree

6 files changed

+144
-86
lines changed

6 files changed

+144
-86
lines changed

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

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package com.diffplug.freshmark;
1717

18-
import java.util.function.Function;
19-
import java.util.regex.Matcher;
2018
import java.util.regex.Pattern;
2119

2220
import javax.script.ScriptEngine;
@@ -115,37 +113,14 @@ public String compile(String input) {
115113
return parser.compile(input, compiler);
116114
}
117115

118-
/** Performs templating on the script. Delegates to mustache-style templating through keyToValue by default. */
119-
protected String template(String section, String script) {
120-
return mustacheTemplate(script, key -> keyToValue(section, key));
121-
}
122-
123-
/** For the given section, return the templated value for the given key. */
124-
protected abstract String keyToValue(String section, String key);
116+
/** For the given section, perform templating on the given script. */
117+
protected abstract String template(String section, String script);
125118

126119
/**
127-
* For the given section, setup a ScriptEngine appropriately.
120+
* For the given section, setup any built-in functions and variables.
128121
* <p>
129122
* The {@code input} value will be set for you, and the {@code output} value will
130123
* be extracted for you, but you must do everything else.
131124
*/
132125
protected abstract ScriptEngine setupScriptEngine(String section) throws ScriptException;
133-
134-
/** Replaces whatever is inside of {@code &#123;&#123;key&#125;&#125;} tags using the {@code keyToValue} function. */
135-
static String mustacheTemplate(String input, Function<String, String> keyToValue) {
136-
Matcher matcher = MUSTACHE_PATTERN.matcher(input);
137-
StringBuilder result = new StringBuilder(input.length() * 3 / 2);
138-
139-
int lastElement = 0;
140-
while (matcher.find()) {
141-
result.append(matcher.group(1));
142-
result.append(keyToValue.apply(matcher.group(2)));
143-
lastElement = matcher.end();
144-
}
145-
result.append(input.substring(lastElement));
146-
return result.toString();
147-
}
148-
149-
/** Regex which matches for {@code {{key}}}. */
150-
private static final Pattern MUSTACHE_PATTERN = Pattern.compile("(.*?)\\{\\{(.*?)\\}\\}", Pattern.DOTALL);
151126
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2015 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.freshmark;
17+
18+
import java.util.function.Function;
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
21+
22+
/**
23+
* A CommentScript is a way of automatically generating
24+
* or modifying parts of a document by embedding scripts
25+
* in the comments of that document.
26+
* <p>
27+
* A CommentScript has the following form:
28+
* <pre>
29+
* {@code
30+
* [INTRON] sectionName
31+
* script
32+
* script
33+
* [EXON]
34+
* lastProgramExecutionResult
35+
* lastProgramExecutionResult
36+
* lastProgramExecutionResult
37+
* [INTRON] /sectionName [EXON]
38+
* }
39+
* </pre>
40+
* This class is a minimal implementation of a CommentScript. To create a CommentScript,
41+
* you must provide:
42+
* <ul>
43+
* <li>The intron and exon strings in the constructor.</li>
44+
* <li>{@link #keyToValue} - defines how template keys in the script string are transformed into values</li>
45+
* <li>{@link #setupScriptEngine} - initializes any functions or variables which should be available to the script</li>
46+
* </ul>
47+
* See {@link FreshMark} for a sample implementation.
48+
*/
49+
public abstract class CommentScriptMustache extends CommentScript {
50+
/** @see CommentScript#CommentScript(String, String) */
51+
protected CommentScriptMustache(String intron, String exon) {
52+
super(intron, exon);
53+
}
54+
55+
/** @see CommentScript#CommentScript(String, String, String) */
56+
protected CommentScriptMustache(String intron, String exon, String regex) {
57+
super(intron, exon, regex);
58+
}
59+
60+
protected String template(String section, String script) {
61+
return mustacheTemplate(script, key -> keyToValue(section, script));
62+
}
63+
64+
protected abstract String keyToValue(String section, String script);
65+
66+
/** Replaces whatever is inside of {@code &#123;&#123;key&#125;&#125;} tags using the {@code keyToValue} function. */
67+
static String mustacheTemplate(String input, Function<String, String> keyToValue) {
68+
Matcher matcher = MUSTACHE_PATTERN.matcher(input);
69+
StringBuilder result = new StringBuilder(input.length() * 3 / 2);
70+
71+
int lastElement = 0;
72+
while (matcher.find()) {
73+
result.append(matcher.group(1));
74+
result.append(keyToValue.apply(matcher.group(2)));
75+
lastElement = matcher.end();
76+
}
77+
result.append(input.substring(lastElement));
78+
return result.toString();
79+
}
80+
81+
/** Regex which matches for {@code {{key}}}. */
82+
private static final Pattern MUSTACHE_PATTERN = Pattern.compile("(.*?)\\{\\{(.*?)\\}\\}", Pattern.DOTALL);
83+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import com.diffplug.jscriptbox.Language;
3232

3333
/** The defaault implementation. */
34-
public class FreshMark extends CommentScript {
34+
public class FreshMark extends CommentScriptMustache {
3535
private static final String INTRON = "<!---freshmark";
3636
private static final String EXON = "-->";
3737

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2015 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.freshmark;
17+
18+
import java.util.Locale;
19+
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
public class CommentScriptMustacheTest {
24+
@Test
25+
public void testTemplate() {
26+
String before = TestResource.getTestResource("template_before.txt");
27+
String after = TestResource.getTestResource("template_after.txt");
28+
String afterActual = CommentScriptMustache.mustacheTemplate(before, key -> key.toUpperCase(Locale.US));
29+
Assert.assertEquals(after, afterActual);
30+
}
31+
}

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

Lines changed: 0 additions & 52 deletions
This file was deleted.

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,38 @@
1515
*/
1616
package com.diffplug.freshmark;
1717

18-
import java.util.Locale;
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
1922

2023
import org.junit.Assert;
2124
import org.junit.Test;
2225

2326
public class FreshMarkTest {
2427
@Test
25-
public void testTemplate() {
26-
String before = TestResource.getTestResource("template_before.txt");
27-
String after = TestResource.getTestResource("template_after.txt");
28-
String afterActual = CommentScript.mustacheTemplate(before, key -> key.toUpperCase(Locale.US));
28+
public void testPrefixDelimReplacement() {
29+
String before = TestResource.getTestResource("javadoc_before.txt");
30+
String after = TestResource.getTestResource("javadoc_after.txt");
31+
String afterActual = FreshMark.prefixDelimiterReplace(before, "https://diffplug.github.io/durian/javadoc/", "/", "4.0");
2932
Assert.assertEquals(after, afterActual);
3033
}
34+
35+
@Test
36+
public void testFull() {
37+
String before = TestResource.getTestResource("full_before.txt");
38+
String after = TestResource.getTestResource("full_after.txt");
39+
40+
Map<String, String> props = new HashMap<>();
41+
props.put("stable", "3.2.0");
42+
props.put("version", "3.3.0-SNAPSHOT");
43+
props.put("group", "com.diffplug.durian");
44+
props.put("name", "durian");
45+
props.put("org", "diffplug");
46+
List<String> warnings = new ArrayList<>();
47+
CommentScript freshmark = new FreshMark(props, warnings::add);
48+
String afterActual = freshmark.compile(before);
49+
Assert.assertEquals(after, afterActual);
50+
Assert.assertTrue(warnings.isEmpty());
51+
}
3152
}

0 commit comments

Comments
 (0)