Skip to content

Commit

Permalink
Allow spaces around equal in header of content files.
Browse files Browse the repository at this point in the history
See point jbake-org#2 "each field has white space after equal sign" of issue
"Make the parsing
metadata more flexible(intelligent) jbake-org#180"
  • Loading branch information
atao60 committed Nov 8, 2015
1 parent bfc595a commit 4b7b8f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
12 changes: 4 additions & 8 deletions src/main/java/org/jbake/app/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ private boolean hasHeader(List<String> contents) {
}
header.add(line);
if (line.contains("=")) {
if (line.startsWith("type=")) {
if (line.matches("^type\\s*=.*")) {
typeFound = true;
}
if (line.startsWith("status=")) {
if (line.matches("^status\\s*=.*")) {
statusFound = true;
}
}
Expand All @@ -175,13 +175,9 @@ private boolean hasHeader(List<String> contents) {
}
}

// if (!headerValid || !statusFound || !typeFound) {
// return false;
// }
// return true;
return headerValid && statusFound && typeFound;
}

/**
* Process the header of the file.
*
Expand All @@ -193,7 +189,7 @@ private void processHeader(List<String> contents, final Map<String, Object> cont
if (line.equals(END_OF_HEADER)) {
break;
} else {
String[] parts = line.split("=",2);
String[] parts = line.split("\\s*=\\s*",2);
if (parts.length == 2) {
if (parts[0].equalsIgnoreCase("date")) {
DateFormat df = new SimpleDateFormat(config.getString(Keys.DATE_FORMAT));
Expand Down
23 changes: 22 additions & 1 deletion src/test/java/org/jbake/app/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ParserTest {
private File validAsciiDocFileWithBlankFirstLineInHeader;
private File validAsciiDocFileWithEmptyRandomLineInHeader;
private File validAsciiDocFileWithBlankRandomLineInHeader;
private File validAsciiDocFileWithSpacesAroundEqualInHeader;

private String validHeader = "title=This is a Title = This is a valid Title" + EOL
+ "status=draft" + EOL
Expand All @@ -54,6 +55,13 @@ public class ParserTest {
private String validHeaderWithBlankRandomLine = validHeader.substring(0, pos)
+ " " + EOL + validHeader.substring(pos + 1);

private String validHeaderWithSpacesAroundEqual = "title = This is a Title = This is a valid Title" + EOL
+ "status =draft" + EOL
+ "type= post"+ EOL
+ "date = 2013-09-02"+ EOL
+ END_OF_HEADER;


@Before
public void createSampleFile() throws Exception {
rootPath = new File(this.getClass().getResource(".").getFile());
Expand Down Expand Up @@ -148,11 +156,18 @@ public void createSampleFile() throws Exception {
out.println("<p>This is a test with empty random line in header.</p>");
out.close();

validAsciiDocFileWithBlankRandomLineInHeader = folder.newFile("validwithblankrandomlineinheader.ad");System.out.println("---#b> "+validHeaderWithBlankRandomLine.replaceAll(EOL, "EOL"));
validAsciiDocFileWithBlankRandomLineInHeader = folder.newFile("validwithblankrandomlineinheader.ad");
out = new PrintWriter(validAsciiDocFileWithBlankRandomLineInHeader);
out.println(validHeaderWithBlankRandomLine);
out.println("<p>This is a test with blank random line in header.</p>");
out.close();

validAsciiDocFileWithSpacesAroundEqualInHeader = folder.newFile("validwithspacesaroundequalinheader.ad");
out = new PrintWriter(validAsciiDocFileWithSpacesAroundEqualInHeader);
out.println(validHeaderWithSpacesAroundEqual);
out.println("<p>This is a test with spaces around equals in header.</p>");
out.close();

}

@Test
Expand Down Expand Up @@ -258,4 +273,10 @@ public void parseValidAsciiDocFileWithBlankRandomLineInHeader() {
Assert.assertNotNull(map);
}

@Test
public void parseValidAsciiDocFileWithSpacesAroundEqualInHeader() {
Map<String, Object> map = parser.processFile(validAsciiDocFileWithSpacesAroundEqualInHeader);
Assert.assertNotNull(map);
}

}

0 comments on commit 4b7b8f7

Please sign in to comment.