server_.convertToYAML(input, new ServerRequestCallback<RmdYamlResult>()
{
@Override
public void onResponseReceived(RmdYamlResult yamlResult)
{
YamlTree yamlTree = new YamlTree(yamlResult.getYaml());
// quote fields
quoteField(yamlTree, "title");
quoteField(yamlTree, "author");
quoteField(yamlTree, "date");
// Order the fields more semantically
yamlTree.reorder(
Arrays.asList("title", "author", "date", "output"));
// Bring the chosen format to the top
if (format != null)
yamlTree.reorder(Arrays.asList(format));
onFinished.execute(yamlTree.toString());
}
@Override
public void onError(ServerError error)
{
onFinished.execute("");
}
private void quoteField(YamlTree yamlTree, String field)
{
String value = yamlTree.getKeyValue(field);
// The string should be quoted if it's a single line.
if (value.length() > 0 && value.indexOf("\n") == -1)
{
if (!((value.startsWith("\"") && value.endsWith("\"")) ||
(value.startsWith("'") && value.endsWith("'"))))
yamlTree.setKeyValue(field, "\"" + value + "\"");
}
}
});
}