Index: java/org/chromium/distiller/MarkupGenerator.java |
diff --git a/java/org/chromium/distiller/MarkupGenerator.java b/java/org/chromium/distiller/MarkupGenerator.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..96ca3c4a91c4e7f2e31c409911ea6cd30e8e91b7 |
--- /dev/null |
+++ b/java/org/chromium/distiller/MarkupGenerator.java |
@@ -0,0 +1,76 @@ |
+package org.chromium.distiller; |
+ |
+import java.util.List; |
+ |
+/** |
+ * This class is used to unify the Markup generation when producing the |
+ * HTML output for Structured Data, reducing the coupling between |
+ * the representation of the data and the Markup generation. |
+ */ |
+public class MarkupGenerator { |
+ |
+ public static String generateMarkup( |
+ SchemaOrgParser.RecipeItem.Recipe recipe) { |
+ return generateImage(recipe.imageSrc) + |
+ createElement("p", "Author: ", recipe.author) + |
+ createElement("p", "Creator: ", recipe.creator) + |
+ createElement("p", "Description: ", recipe.description) + |
+ createElement("p", "Serves: ", recipe.recipeYield) + |
+ createElement("p", "Prep time: ", recipe.prepTime) + |
+ createElement("p", "Cook time: ", recipe.cookTime) + |
+ createElement("p", "Total time: ", recipe.totalTime) + |
+ generateList("Ingredients: ", recipe.recipeIngredient, |
+ false) + |
+ generateList("Instructions: <br />", |
+ recipe.recipeInstructions, false); |
+ } |
+ |
+ public static String generateMarkup( |
+ SchemaOrgParser.PersonItem.Person person) { |
+ return createElement("span", person.name); |
+ } |
+ |
+ private static String generateList(String name, List<String> value, |
+ boolean ordered) { |
+ if (value.size() == 1) { |
+ return createElement("p", name, value.get(0)); |
+ } |
+ String output = ""; |
+ for (String s : value) { |
+ output += createElement("li", s); |
+ } |
+ return name + createElement(ordered ? "ol" : "ul", output); |
+ } |
+ |
+ private static String createElement(String tag, String name, |
+ String value) { |
+ String output = ""; |
+ if (!value.isEmpty()) { |
+ output = format(createElement(tag, name + value)); |
+ } |
+ return output; |
+ } |
+ |
+ private static String createElement(String tag, String value) { |
+ return "<" + tag + ">" + value + "</" + tag + ">"; |
+ } |
+ |
+ private static String generateImage(String src) { |
+ String output = ""; |
+ if (!src.isEmpty()) { |
+ output = "<img src='" + src + "' />"; |
+ } |
+ return output; |
+ } |
+ |
+ /** |
+ * Format a string replacing the new line char to |
+ * <br> tag. It also detects when 2 paragraphs are concatenated |
+ * together, splitting them by a <br> tag. |
+ */ |
+ private static String format(String string) { |
+ return string.trim() |
+ .replaceAll("([.])([A-Z]{1}[a-z]+)", "$1<br />$2") |
+ .replaceAll("[\n]+[\\s]*", "<br />"); |
+ } |
+} |