Skip to content

Commit

Permalink
Cleaned up code from work on #44 and fixed #45
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbullock committed Oct 19, 2013
1 parent cf32aaf commit e6cff56
Showing 1 changed file with 53 additions and 145 deletions.
198 changes: 53 additions & 145 deletions src/main/java/org/jbake/app/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ public class Parser {

private CompositeConfiguration config;
private Map<String, Object> content = new HashMap<String, Object>();
private Asciidoctor asciidoctor;

/**
* Creates a new instance of Parser.
*/
public Parser() {
asciidoctor = Factory.create();
}

public Parser(CompositeConfiguration config) {
this.config = config;
asciidoctor = Factory.create();
}

/**
Expand All @@ -56,131 +59,61 @@ public Parser(CompositeConfiguration config) {
*/
public Map<String, Object> processFile(File file) {
content = new HashMap<String, Object>();
BufferedReader reader = null;
List<String> fileContents = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
List<String> fileContents = IOUtils.readLines(reader);
boolean hasHeader = hasHeader(fileContents);
if (file.getPath().endsWith(".md") || file.getPath().endsWith(".html")) {
if (hasHeader) {
// process jbake header
processHeader(fileContents);
processBody(fileContents, file);
} else {
// throw error

return null;
}
} else if (file.getPath().endsWith(".asciidoc") || file.getPath().endsWith(".ad")) {
if (hasHeader) {
// process jbake header
processHeader(fileContents);
processBody(fileContents, file);
} else {
// try extracting meta data out of asciidoc header instead
if (validateAsciiDoc(file)) {
processAsciiDocHeader(file);
processAsciiDoc(fileContents, file);
} else {
// throw error

return null;
}
}
} else {
return null;
}


// boolean validFile = false;
// validFile = validate(fileContents);
// if (validFile) {
// processHeader(fileContents);
// processBody(fileContents, file);
// } else if (file.getPath().endsWith(".asciidoc") || file.getPath().endsWith(".ad")) {
// validFile = validateAsciiDoc(file);
// if (validFile) {
// processAsciiDocHeader(file);
// processBody(fileContents, file);
// } else {
// return null;
// }
// } else {
// return null;
// }
} catch (Exception e) {
reader = new BufferedReader(new FileReader(file));
fileContents = IOUtils.readLines(reader);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}

return content;
}

/**
* Validates if the file has the required elements.
*
* @param contents Contents of file
* @return true if valid, false if not
*/
private boolean hasHeader(List<String> contents) {
boolean headerFound = false;
boolean headerSeparatorFound = false;
boolean statusFound = false;
boolean typeFound = false;

List<String> header = new ArrayList<String>();
boolean hasHeader = hasHeader(fileContents);

for (String line : contents) {
header.add(line);
if (line.contains("=")) {
if (line.startsWith("type=")) {
typeFound = true;
}
if (line.startsWith("status=")) {
statusFound = true;
}
}
if (line.equals("~~~~~~")) {
headerSeparatorFound = true;
header.remove(line);
break;
if (file.getPath().endsWith(".md") || file.getPath().endsWith(".html")) {
if (hasHeader) {
// process jbake header
processHeader(fileContents);
processBody(fileContents, file);
} else {
// output error
System.err.println("Error parsing meta data from header!");
return null;
}
}

if (headerSeparatorFound) {
headerFound = true;
for (String headerLine : header) {
if (!headerLine.contains("=")) {
headerFound = false;
break;
} else if (file.getPath().endsWith(".asciidoc") || file.getPath().endsWith(".ad")) {
if (hasHeader) {
// process jbake header
processHeader(fileContents);
processBody(fileContents, file);
} else {
// try extracting meta data out of asciidoc header instead
if (validateAsciiDoc(file)) {
processAsciiDocHeader(file);
processAsciiDoc(fileContents);
} else {
// output error
System.err.println("Error parsing meta data from header!");
return null;
}
}
} else {
return null;
}

if (!headerFound || !statusFound || !typeFound) {
// System.out.println("");
// if (!headerFound) {
// System.out.println("Missing required JBake header");
// }
// if (!statusFound) {
// System.out.println("Missing required header value: status");
// }
// if (!typeFound) {
// System.out.println("Missing required header value: type");
// }

return false;
}
return true;
return content;
}

/**
* Validates if the file has the required elements.
* Checks if the file has a meta-data header.
*
* @param contents Contents of file
* @return true if valid, false if not
* @return true if header exists, false if not
*/
private boolean validate(List<String> contents) {
boolean headerFound = false;
private boolean hasHeader(List<String> contents) {
boolean headerValid = false;
boolean headerSeparatorFound = false;
boolean statusFound = false;
boolean typeFound = false;
Expand All @@ -205,27 +138,16 @@ private boolean validate(List<String> contents) {
}

if (headerSeparatorFound) {
headerFound = true;
headerValid = true;
for (String headerLine : header) {
if (!headerLine.contains("=")) {
headerFound = false;
headerValid = false;
break;
}
}
}

if (!headerFound || !statusFound || !typeFound) {
System.out.println("");
if (!headerFound) {
System.out.println("Missing required JBake header");
}
if (!statusFound) {
System.out.println("Missing required header value: status");
}
if (!typeFound) {
System.out.println("Missing required header value: type");
}

if (!headerValid || !statusFound || !typeFound) {
return false;
}
return true;
Expand Down Expand Up @@ -268,7 +190,6 @@ private void processHeader(List<String> contents) {
* @param contents Contents of file
*/
private boolean validateAsciiDoc(File file) {
Asciidoctor asciidoctor = Factory.create();
DocumentHeader header = asciidoctor.readDocumentHeader(file);
boolean statusFound = false;
boolean typeFound = false;
Expand All @@ -283,14 +204,6 @@ private boolean validateAsciiDoc(File file) {
}

if (!statusFound || !typeFound) {
System.out.println("");
if (!statusFound) {
System.out.println("Missing required header value: jbake-status");
}
if (!typeFound) {
System.out.println("Missing required header value: jbake-type");
}

return false;
}
return true;
Expand All @@ -302,7 +215,6 @@ private boolean validateAsciiDoc(File file) {
* @param contents Contents of file
*/
private void processAsciiDocHeader(File file) {
Asciidoctor asciidoctor = Factory.create();
DocumentHeader header = asciidoctor.readDocumentHeader(file);
if (header.getDocumentTitle() != null) {
content.put("title", header.getDocumentTitle());
Expand Down Expand Up @@ -367,14 +279,7 @@ private void processBody(List<String> contents, File file) {
MarkdownProcessor markdown = new MarkdownProcessor();
content.put("body", markdown.markdown(body.toString()));
} else if (file.getPath().endsWith(".ad") || file.getPath().endsWith(".asciidoc")) {
Asciidoctor asciidoctor = Factory.create();
// Map<String, Object> options = new HashMap<String, Object>();
// Map<String, Object> attributes = new HashMap<String, Object>();
// attributes.put("source-highlighter", config.getString("asciidoctor.highlighter"));
// options.put("attributes", attributes);
Attributes attributes = AttributesBuilder.attributes(config.getString("asciidoctor.options")).get();
Options options = OptionsBuilder.options().attributes(attributes).get();
content.put("body", asciidoctor.render(body.toString(), options));
processAsciiDoc(body);
} else {
content.put("body", body.toString());
}
Expand All @@ -386,15 +291,18 @@ private void processBody(List<String> contents, File file) {
* @param contents Contents of file
* @param file Source file
*/
private void processAsciiDoc(List<String> contents, File file) {
private void processAsciiDoc(List<String> contents) {
StringBuffer body = new StringBuffer();
for (String line : contents) {
body.append(line + "\n");
}

Asciidoctor asciidoctor = Factory.create();

processAsciiDoc(body);
}

private void processAsciiDoc(StringBuffer contents) {
Attributes attributes = AttributesBuilder.attributes(config.getString("asciidoctor.options")).get();
Options options = OptionsBuilder.options().attributes(attributes).get();
content.put("body", asciidoctor.render(body.toString(), options));
content.put("body", asciidoctor.render(contents.toString(), options));
}
}

0 comments on commit e6cff56

Please sign in to comment.