Skip to content

Commit

Permalink
Fixes #180.
Browse files Browse the repository at this point in the history
Made the header parsing code more tolerant of blank lines and spaces.
Also replaced some string literals.
  • Loading branch information
jonbullock committed Aug 21, 2016
1 parent d94e816 commit af8d827
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/jbake/app/Crawler.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static interface Status {
static final String NO_EXTENSION_URI = "noExtensionUri";
static final String ALLTAGS = "alltags";
static final String PUBLISHED_DATE = "published_date";
static final String BODY = "body";
}

private static final Logger LOGGER = LoggerFactory.getLogger(Crawler.class);
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/org/jbake/parser/MarkupEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Map<String, Object> parse(Configuration config, File file, String content
List<String> fileContents = null;
try {
is = new FileInputStream(file);
fileContents = IOUtils.readLines(is, config.getString("render.encoding"));
fileContents = IOUtils.readLines(is, config.getString(Keys.RENDER_ENCODING));
} catch (IOException e) {
LOGGER.error("Error while opening file {}: {}", file, e);

Expand Down Expand Up @@ -121,15 +121,15 @@ public Map<String, Object> parse(Configuration config, File file, String content
return null;
}

if (content.get("tags") != null) {
String[] tags = (String[]) content.get("tags");
if (content.get(Crawler.Attributes.TAGS) != null) {
String[] tags = (String[]) content.get(Crawler.Attributes.TAGS);
for( int i=0; i<tags.length; i++ ) {
tags[i]=tags[i].trim();
if (config.getBoolean(Keys.TAG_SANITIZE)) {
tags[i]=tags[i].replace(" ", "-");
}
}
content.put("tags", tags);
content.put(Crawler.Attributes.TAGS, tags);
}

// TODO: post parsing plugins to hook in here?
Expand All @@ -152,7 +152,9 @@ private boolean hasHeader(List<String> contents) {
List<String> header = new ArrayList<String>();

for (String line : contents) {
header.add(line);
if (!line.isEmpty()){
header.add(line);
}
if (line.contains("=")) {
if (line.startsWith("type=")) {
typeFound = true;
Expand Down Expand Up @@ -194,15 +196,19 @@ private void processHeader(Configuration config, List<String> contents, final Ma
break;
}

if (line.isEmpty()) {
continue;
}

String[] parts = line.split("=",2);
if (parts.length != 2) {
continue;
}

String key = parts[0];
String value = parts[1];
String key = parts[0].trim();
String value = parts[1].trim();

if (key.equalsIgnoreCase("date")) {
if (key.equalsIgnoreCase(Crawler.Attributes.DATE)) {
DateFormat df = new SimpleDateFormat(config.getString(Keys.DATE_FORMAT));
Date date = null;
try {
Expand All @@ -211,7 +217,7 @@ private void processHeader(Configuration config, List<String> contents, final Ma
} catch (ParseException e) {
e.printStackTrace();
}
} else if (key.equalsIgnoreCase("tags")) {
} else if (key.equalsIgnoreCase(Crawler.Attributes.TAGS)) {
content.put(key, getTags(value));
} else if (isJson(value)) {
content.put(key, JSONValue.parse(value));
Expand Down Expand Up @@ -256,6 +262,6 @@ private void processBody(List<String> contents, final Map<String, Object> conten
}
}

content.put("body", body.toString());
content.put(Crawler.Attributes.BODY, body.toString());
}
}
5 changes: 3 additions & 2 deletions src/main/java/org/jbake/parser/ParserContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;

import org.apache.commons.configuration.Configuration;
import org.jbake.app.Crawler;

public class ParserContext {
private final File file;
Expand Down Expand Up @@ -55,10 +56,10 @@ public boolean hasHeader() {

// short methods for common use
public String getBody() {
return contents.get("body").toString();
return contents.get(Crawler.Attributes.BODY).toString();
}

public void setBody(String str) {
contents.put("body", str);
contents.put(Crawler.Attributes.BODY, str);
}
}

0 comments on commit af8d827

Please sign in to comment.