Skip to content

Commit

Permalink
Merge 20c054c into c3ca239
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasblaesing committed Jan 17, 2019
2 parents c3ca239 + 20c054c commit de68a0d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 22 deletions.
Expand Up @@ -98,7 +98,11 @@ public void processHeader(final ParserContext context) {

if (hasJBakePrefix(key)) {
String pKey = key.substring(6);
documentModel.put(pKey, value);
if(canCastToString(value)) {
storeHeaderValue(pKey, (String) value, documentModel);
} else {
documentModel.put(pKey, value);
}
}
if (hasRevdate(key) && canCastToString(value)) {

Expand All @@ -124,7 +128,7 @@ public void processHeader(final ParserContext context) {
}

private boolean canCastToString(Object value) {
return value != null && value instanceof String;
return value instanceof String;
}

private String getValueClassName(Object value) {
Expand Down
42 changes: 22 additions & 20 deletions jbake-core/src/main/java/org/jbake/parser/MarkupEngine.java
Expand Up @@ -253,33 +253,35 @@ private void processDefaultHeader(ParserContext context) {
if (hasHeaderSeparator(line)) {
break;
}
processLine(line, context.getDocumentModel());
processHeaderLine(line, context.getDocumentModel());
}
}

private void processLine(String line, Map<String, Object> content) {
private void processHeaderLine(String line, Map<String, Object> content) {
String[] parts = line.split("=", 2);
if (!line.isEmpty() && parts.length == 2) {
storeHeaderValue(parts[0], parts[1], content);
}
}


String key = sanitizeKey(parts[0]);
String value = sanitizeValue(parts[1]);

if (key.equalsIgnoreCase(Crawler.Attributes.DATE)) {
DateFormat df = new SimpleDateFormat(configuration.getDateFormat());
try {
Date date = df.parse(value);
content.put(key, date);
} catch (ParseException e) {
LOGGER.error("unable to parse date {}", value);
}
} else if (key.equalsIgnoreCase(Crawler.Attributes.TAGS)) {
content.put(key, getTags(value));
} else if (isJson(value)) {
content.put(key, JSONValue.parse(value));
} else {
content.put(key, value);
void storeHeaderValue(String inputKey, String inputValue, Map<String, Object> content) {
String key = sanitizeKey(inputKey);
String value = sanitizeValue(inputValue);

if (key.equalsIgnoreCase(Crawler.Attributes.DATE)) {
DateFormat df = new SimpleDateFormat(configuration.getDateFormat());
try {
Date date = df.parse(value);
content.put(key, date);
} catch (ParseException e) {
LOGGER.error("unable to parse date {}", value);
}
} else if (key.equalsIgnoreCase(Crawler.Attributes.TAGS)) {
content.put(key, getTags(value));
} else if (isJson(value)) {
content.put(key, JSONValue.parse(value));
} else {
content.put(key, value);
}
}

Expand Down
85 changes: 85 additions & 0 deletions jbake-core/src/test/java/org/jbake/app/ParserTest.java
Expand Up @@ -9,8 +9,12 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.io.File;
import java.io.PrintWriter;
import java.util.AbstractMap.SimpleEntry;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
Expand Down Expand Up @@ -40,9 +44,13 @@ public class ParserTest {
private File invalidMarkdownFileWithoutDefaultStatus;
private File invalidMDFile;
private File invalidExtensionFile;
private File validHTMLWithJSONFile;
private File validAsciiDocWithJSONFile;
private File validAsciiDocWithADHeaderJSONFile;

private String validHeader = "title=This is a Title = This is a valid Title\nstatus=draft\ntype=post\ndate=2013-09-02\n~~~~~~";
private String invalidHeader = "title=This is a Title\n~~~~~~";
private String sampleJsonData = "{\"numberValue\": 42, \"stringValue\": \"Answer to live, the universe and everything\", \"nullValue\": null, \"arrayValue\": [1, 2], \"objectValue\": {\"val1\": 1, \"val2\": 2}}";
private String customHeaderSeparator;


Expand Down Expand Up @@ -209,6 +217,47 @@ public void createSampleFile() throws Exception {
out = new PrintWriter(invalidExtensionFile);
out.println("invalid content");
out.close();

validHTMLWithJSONFile = folder.newFile("validHTMLWithJSONFile.html");
out = new PrintWriter(validHTMLWithJSONFile);
out.println("title=This is a Title = This is a valid Title");
out.println("status=draft");
out.println("type=post");
out.println("date=2013-09-02");
out.print("jsondata=");
out.println(sampleJsonData);
out.println("~~~~~~");
out.println("Sample Body");
out.close();

validAsciiDocWithJSONFile = folder.newFile("validAsciiDocWithJSONFile.ad");
out = new PrintWriter(validAsciiDocWithJSONFile);
out.println("title=This is a Title = This is a valid Title");
out.println("status=draft");
out.println("type=post");
out.println("date=2013-09-02");
out.print("jsondata=");
out.println(sampleJsonData);
out.println("~~~~~~");
out.println("= Hello, AsciiDoc!");
out.println("Test User <user@test.org>");
out.println("");
out.println("JBake now supports AsciiDoc.");
out.close();

validAsciiDocWithADHeaderJSONFile = folder.newFile("validAsciiDocWithADHeaderJSONFile.ad");
out = new PrintWriter(validAsciiDocWithADHeaderJSONFile);
out.println("= Hello: AsciiDoc!");
out.println("Test User <user@test.org>");
out.println("2013-09-02");
out.println(":jbake-status: published");
out.println(":jbake-type: page");
out.print(":jbake-jsondata: ");
out.println(sampleJsonData);
out.println("");
out.println("JBake now supports AsciiDoc.");
out.close();
out.close();
}

@Test
Expand Down Expand Up @@ -356,4 +405,40 @@ public void parseInvalidMarkdownFile() {
Assert.assertNull(map);
}

@Test
public void parseValidHTMLWithJSONFile() {
Map<String, Object> map = parser.processFile(validHTMLWithJSONFile);
assertJSONExtracted(map.get("jsondata"));
}

@Test
public void parseValidAsciiDocWithJSONFile() {
Map<String, Object> map = parser.processFile(validAsciiDocWithJSONFile);
assertJSONExtracted(map.get("jsondata"));
}

@Test
public void testValidAsciiDocWithADHeaderJSONFile() {
Map<String, Object> map = parser.processFile(validAsciiDocWithADHeaderJSONFile);
assertJSONExtracted(map.get("jsondata"));
}

private void assertJSONExtracted(Object jsonDataEntry) {
assertThat(jsonDataEntry).isInstanceOf(JSONObject.class);
JSONObject jsonData = (JSONObject) jsonDataEntry;
assertThat(jsonData.containsKey("numberValue")).isTrue();
assertThat(jsonData.get("numberValue")).isInstanceOf(Number.class);
assertThat(((Number)jsonData.get("numberValue")).intValue()).isEqualTo(42);
assertThat(jsonData.containsKey("stringValue")).isTrue();
assertThat(jsonData.get("stringValue")).isInstanceOf(String.class);
assertThat((String)jsonData.get("stringValue")).isEqualTo("Answer to live, the universe and everything");
assertThat(jsonData.containsKey("nullValue")).isTrue();
assertThat(jsonData.get("nullValue")).isNull();
assertThat(jsonData.containsKey("arrayValue")).isTrue();
assertThat(jsonData.get("arrayValue")).isInstanceOf(JSONArray.class);
assertThat((JSONArray)jsonData.get("arrayValue")).contains(1L,2L);
assertThat(jsonData.containsKey("objectValue")).isTrue();
assertThat(jsonData.get("objectValue")).isInstanceOf(JSONObject.class);
assertThat((JSONObject)jsonData.get("objectValue")).contains(new SimpleEntry("val1", 1L), new SimpleEntry("val2", 2L));
}
}

0 comments on commit de68a0d

Please sign in to comment.