Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Unified Diff: java/org/chromium/distiller/MarkupGenerator.java

Issue 1705123002: Add support for Schema.org/Recipe Base URL: https://github.com/chromium/dom-distiller.git@master
Patch Set: wychen's comments addressed Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 />");
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698