OLD | NEW |
---|---|
(Empty) | |
1 package org.chromium.distiller; | |
2 | |
3 import java.util.List; | |
4 | |
5 /** | |
6 * This class is used to unify the Markup generation when producing the | |
7 * HTML output for Structured Data, reducing the coupling between | |
8 * the representation of the data and the Markup generation. | |
9 */ | |
10 public class MarkupGenerator { | |
11 | |
12 public static String generateMarkup( | |
13 SchemaOrgParser.RecipeItem.Recipe recipe) { | |
14 return generateImage(recipe.imageSrc) + | |
15 createElement("p", "Author: ", recipe.author) + | |
wychen
2016/03/14 22:58:42
L10n issues again. It would look weird on non-Engl
| |
16 createElement("p", "Creator: ", recipe.creator) + | |
17 createElement("p", "Description: ", recipe.description) + | |
18 createElement("p", "Serves: ", recipe.recipeYield) + | |
19 createElement("p", "Prep time: ", recipe.prepTime) + | |
20 createElement("p", "Cook time: ", recipe.cookTime) + | |
21 createElement("p", "Total time: ", recipe.totalTime) + | |
22 generateList("Ingredients: ", recipe.recipeIngredient, | |
23 false) + | |
24 generateList("Instructions: <br />", | |
25 recipe.recipeInstructions, false); | |
26 } | |
27 | |
28 public static String generateMarkup( | |
29 SchemaOrgParser.PersonItem.Person person) { | |
30 return createElement("span", person.name); | |
31 } | |
32 | |
33 private static String generateList(String name, List<String> value, | |
34 boolean ordered) { | |
35 if (value.size() == 1) { | |
36 return createElement("p", name, value.get(0)); | |
37 } | |
38 String output = ""; | |
39 for (String s : value) { | |
40 output += createElement("li", s); | |
41 } | |
42 return name + createElement(ordered ? "ol" : "ul", output); | |
43 } | |
44 | |
45 private static String createElement(String tag, String name, | |
46 String value) { | |
47 String output = ""; | |
48 if (!value.isEmpty()) { | |
49 output = format(createElement(tag, name + value)); | |
50 } | |
51 return output; | |
52 } | |
53 | |
54 private static String createElement(String tag, String value) { | |
55 return "<" + tag + ">" + value + "</" + tag + ">"; | |
56 } | |
57 | |
58 private static String generateImage(String src) { | |
59 String output = ""; | |
60 if (!src.isEmpty()) { | |
61 output = "<img src='" + src + "' />"; | |
62 } | |
63 return output; | |
64 } | |
65 | |
66 /** | |
67 * Format a string replacing the new line char to | |
68 * <br> tag. It also detects when 2 paragraphs are concatenated | |
69 * together, splitting them by a <br> tag. | |
70 */ | |
71 private static String format(String string) { | |
72 return string.trim() | |
73 .replaceAll("([.])([A-Z]{1}[a-z]+)", "$1<br />$2") | |
74 .replaceAll("[\n]+[\\s]*", "<br />"); | |
75 } | |
76 } | |
OLD | NEW |