Skip to content

Commit

Permalink
Improve hasHeader(): only scan first N lines, skip #... lines, skip b…
Browse files Browse the repository at this point in the history
…lank lines, test against a regex; do not require status and type if defaults are set.
  • Loading branch information
OndraZizka committed Oct 26, 2018
1 parent 058d403 commit 2026116
Showing 1 changed file with 61 additions and 11 deletions.
72 changes: 61 additions & 11 deletions jbake-core/src/main/java/org/jbake/parser/MarkupEngine.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package org.jbake.parser;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.jbake.app.Crawler;
import org.jbake.app.configuration.DefaultJBakeConfiguration;
import org.jbake.app.configuration.JBakeConfiguration;
import org.json.simple.JSONValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.jbake.app.Crawler;
import org.jbake.app.configuration.DefaultJBakeConfiguration;
import org.jbake.app.configuration.JBakeConfiguration;
import org.json.simple.JSONValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Base class for markup engine wrappers. A markup engine is responsible for rendering
Expand All @@ -35,6 +36,9 @@ public abstract class MarkupEngine implements ParserEngine {

private JBakeConfiguration configuration;

public static final int MAX_HEADER_LINES = 50;


/**
* Tests if this markup engine can process the document.
*
Expand Down Expand Up @@ -156,12 +160,58 @@ private void setModelDefaultsIfNotSetInHeader(ParserContext context) {
}
}


/**
* Checks if the file has a meta-data header.
*
* @param contents Contents of file
* @return true if header exists, false if not
*/
private boolean hasHeader2(Configuration config, List<String> contents) {
boolean headerSeparatorFound = false;
boolean statusFound = false;
boolean typeFound = false;

List<String> headerLines = new ArrayList<>();


//for (String line : contents) {
for (int i = 0; i < contents.size() && i < MAX_HEADER_LINES; i++) {
String line = contents.get(i);
if (StringUtils.isBlank(line))
continue;

if (line.startsWith("#"))
continue;

if (line.equals(configuration.getHeaderSeparator())) {
headerSeparatorFound = true;
break;
}

if (!line.matches("^\\p{Graph}+\\p{Blank}*(=|:)\\p{Blank}*\\p{Graph}*"))
continue;

headerLines.add(line);

typeFound |= line.startsWith("type=");
statusFound |= line.startsWith("status=");
}

if (headerSeparatorFound) {
for (String headerLine : headerLines) {
if (!headerLine.contains("=")) {
LOGGER.warn("Document has invalid header line (without '='):\n{}", headerLines);
return false;
}
}
}

return (statusFound || null != configuration.getDefaultStatus())
&& (typeFound || null != configuration.getDefaultType());
}


private boolean hasHeader(List<String> contents) {
boolean headerValid = true;
boolean statusFound = false;
Expand Down Expand Up @@ -332,4 +382,4 @@ private void processDefaultBody(ParserContext context) {
}
context.setBody(body.toString());
}
}
}

0 comments on commit 2026116

Please sign in to comment.