From e6cff569d73d7a7ca49353878a447dfb8842c32b Mon Sep 17 00:00:00 2001 From: Jonathan Bullock Date: Sat, 19 Oct 2013 12:22:55 +0100 Subject: [PATCH] Cleaned up code from work on #44 and fixed #45 --- src/main/java/org/jbake/app/Parser.java | 198 +++++++----------------- 1 file changed, 53 insertions(+), 145 deletions(-) diff --git a/src/main/java/org/jbake/app/Parser.java b/src/main/java/org/jbake/app/Parser.java index 81d5cedeb..dfd10f34b 100644 --- a/src/main/java/org/jbake/app/Parser.java +++ b/src/main/java/org/jbake/app/Parser.java @@ -37,15 +37,18 @@ public class Parser { private CompositeConfiguration config; private Map content = new HashMap(); + private Asciidoctor asciidoctor; /** * Creates a new instance of Parser. */ public Parser() { + asciidoctor = Factory.create(); } public Parser(CompositeConfiguration config) { this.config = config; + asciidoctor = Factory.create(); } /** @@ -56,131 +59,61 @@ public Parser(CompositeConfiguration config) { */ public Map processFile(File file) { content = new HashMap(); + BufferedReader reader = null; + List fileContents = null; try { - BufferedReader reader = new BufferedReader(new FileReader(file)); - List 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 contents) { - boolean headerFound = false; - boolean headerSeparatorFound = false; - boolean statusFound = false; - boolean typeFound = false; - - List header = new ArrayList(); + 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 contents) { - boolean headerFound = false; + private boolean hasHeader(List contents) { + boolean headerValid = false; boolean headerSeparatorFound = false; boolean statusFound = false; boolean typeFound = false; @@ -205,27 +138,16 @@ private boolean validate(List 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; @@ -268,7 +190,6 @@ private void processHeader(List 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; @@ -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; @@ -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()); @@ -367,14 +279,7 @@ private void processBody(List 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 options = new HashMap(); -// Map attributes = new HashMap(); -// 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()); } @@ -386,15 +291,18 @@ private void processBody(List contents, File file) { * @param contents Contents of file * @param file Source file */ - private void processAsciiDoc(List contents, File file) { + private void processAsciiDoc(List 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)); } }