Skip to content

Instantly share code, notes, and snippets.

@nhocki
Created February 13, 2014 21:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhocki/95599aa0987dad76582b to your computer and use it in GitHub Desktop.
Save nhocki/95599aa0987dad76582b to your computer and use it in GitHub Desktop.
// Real usage
WhiTemplater.Formatter FEATURED_FORMAT = new WhiTemplater.Formatter() {
@Override
public String format(Object value) {
return "<b>" + String.valueOf(value)+ "</b>";
}
};
WhiTemplater.Formatter NORMAL_FORMAT = new WhiTemplater.Formatter() {
@Override
public String format(Object value) {
return "<b><font color='#FD93B1'>" + String.valueOf(value)+ "</font></b>";
}
};
// later on
WhiTemplater.Formatter formatter = isFeatured() ? FEATURED_FORMAT : NORMAL_FORMAT;
String template = descriptionHash.getString("template");
setMessage(WhiTemplater.compile(template, descriptionHash, formatter));
package com.weheartit.util;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// Turns strings with mustache variables (like {{username}}) into readable strings where the replacement
// for the variable is looked inside a JSON context.
//
// If we have "{{username}} is now following you" and the context has the 'username' key, this will
// replace `{{username}}` with the value for that key on the context.
//
// This is the simplest Mustache replacement, so having a complex library for that was not
// worth it for now, specially when the library (jmustache) has failing tests.
//
// on matcher.group(1) we'll have '{{username}}' and on matcher.group(2) we'll have 'username',
// we have to quote '{{username}}' since "{{" is invalid in Java regexps.
//
// More examples can be found on the test files.
public class WhiTemplater {
final static Pattern mustachePattern = Pattern.compile("(\\{\\{([.[^\\}\\}]]+)\\}\\})");
/** Handles converting objects to strings when rendering templates. */
public interface Formatter {
String format (Object value);
};
protected static final Formatter DEFAULT_FORMATTER = new Formatter() {
public String format (Object value) {
return String.valueOf(value);
}
};
public final static String compile(String template, JSONObject context) throws JSONException, ParseException {
return compile(template, context, DEFAULT_FORMATTER);
}
public final static String compile(String template, JSONObject context, Formatter formatter) throws JSONException, ParseException {
Matcher matcher = mustachePattern.matcher(template);
while(matcher.find()){
String stringToReplace = Pattern.quote(matcher.group(1));
if(context.has(matcher.group(2))){
String replaceWith = formatter.format(context.getString(matcher.group(2)));
template = template.replaceAll(stringToReplace, replaceWith);
}
}
return template;
}
}
package com.weheartit.model;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.weheartit.WeHeartItTestRunner;
import com.weheartit.util.WhiTemplater;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
import java.text.ParseException;
import dalvik.annotation.TestTarget;
@RunWith(WeHeartItTestRunner.class)
public class WhiTemplaterTest {
@Test
public void itCompilesTemplates() throws JSONException, ParseException{
JSONObject context = new JSONObject("{\"username\":\"nhocki\"}");
String result = WhiTemplater.compile("{{username}} is now following you", context);
assertThat(result, equalTo("nhocki is now following you"));
}
@Test
public void itAcceptsAFormatterObject() throws JSONException, ParseException{
JSONObject context = new JSONObject("{\"username\":\"nhocki\"}");
WhiTemplater.Formatter f = new WhiTemplater.Formatter() {
@Override
public String format(Object value) {
return "<b>" + String.valueOf(value) + "</b>";
}
};
String result = WhiTemplater.compile("{{username}} is now following you", context, f);
assertThat(result, equalTo("<b>nhocki</b> is now following you"));
}
@Test
public void itReplacesMultipleVariablesMultipleTimes() throws JSONException, ParseException{
JSONObject context = new JSONObject("{\"username\":\"nhocki\", \"name\": \"Nicolas\"}");
String result = WhiTemplater.compile("{{username}} {{name}} {{username}} {{name}}", context);
assertThat(result, equalTo("nhocki Nicolas nhocki Nicolas"));
}
@Test
public void itDoentDieWithNonExistentVariables() throws JSONException, ParseException{
JSONObject context = new JSONObject("{\"username\":\"nhocki\"}");
String result = WhiTemplater.compile("{{idontexist}} yeah", context);
assertThat(result, equalTo("{{idontexist}} yeah"));
}
@Test
public void itWorksWithTemplatesWithoutVariables() throws JSONException, ParseException{
String result = WhiTemplater.compile("yeah", new JSONObject("{}"));
assertThat(result, equalTo("yeah"));
result = WhiTemplater.compile("oh yeah", new JSONObject("{\"name\": \"Nicolas\"}"));
assertThat(result, equalTo("oh yeah"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment