8224257: fix issues in files generated by pandoc
authorjjg
Fri, 07 Jun 2019 14:32:48 -0700
changeset 55297 fd61ef6c4091
parent 55296 357c9dcb6eb9
child 55298 1fe17d2be502
8224257: fix issues in files generated by pandoc Reviewed-by: mchung
make/jdk/src/classes/build/tools/fixuppandoc/Main.java
--- a/make/jdk/src/classes/build/tools/fixuppandoc/Main.java	Fri Jun 07 14:03:17 2019 -0700
+++ b/make/jdk/src/classes/build/tools/fixuppandoc/Main.java	Fri Jun 07 14:32:48 2019 -0700
@@ -44,6 +44,7 @@
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 /**
@@ -63,11 +64,18 @@
  *
  * <h2>Tables: row headings</h2>
  *
- * {@code scope="row"} is added to the {@code <td>} elements in the first
- * column whose cell contents are all different and therefore which can be
- * used to identify the row. In case of ambiguity, a column containing
- * a {@code <th>} whose contents begin <em>name</em> is preferred.
+ * For simple tables, as typically generated by _pandoc_, determine the column
+ * whose contents are unique, and convert the cells in that column to be header
+ * cells with {@code scope="row"}. In case of ambiguity, a column containing a
+ * {@code <th>} whose contents begin with <em>name</em> is preferred.
+ * When converting the cell, the {@code style} attribute will be updated to
+ * specify {@code font-weight: normal}, and if there is not already an explicit
+ * setting for {@code text-align}, then the style will be updated to include
+ * {@code text-align:left;}.
  *
+ * These rules do not apply if the table contains any cells that include
+ * a setting for the {@code scope} attribute, or if the table contains
+ * spanning cells or nested tables.
  *
  * <h2>{@code <meta name="generator">}</h2>
  *
@@ -533,12 +541,39 @@
                 }
                 index++;
             }
+            boolean updateEndTd = false;
+            Pattern styleAttr = Pattern.compile("(?<before>.*style=\")(?<style>[^\"]*)(?<after>\".*)");
             for (Entry e : entries) {
                 if (simple && e.column == maxIndex) {
-                    out.write(e.html.substring(0, e.html.length() - 1));
-                    out.write(" scope=\"row\">");
+                    String attrs = e.html.substring(3, e.html.length() - 1);
+                    out.write("<th");
+                    Matcher m = styleAttr.matcher(attrs);
+                    if (m.matches()) {
+                        out.write(m.group("before"));
+                        out.write("font-weight: normal; ");
+                        String style = m.group("style");
+                        if (!style.contains("text-align")) {
+                            out.write("text-align: left; ");
+                        }
+                        out.write(style);
+                        out.write(m.group("after"));
+                    } else {
+                        out.write(" style=\"font-weight: normal; text-align:left\"; ");
+                        out.write(attrs);
+                    }
+                    out.write(" scope=\"row\"");
+                    out.write(">");
+                    updateEndTd = true;
+                } else if (updateEndTd && e.html.equalsIgnoreCase("</td>")) {
+                    out.write("</th>");
+                    updateEndTd = false;
                 } else {
                     out.write(e.html);
+                    if (updateEndTd && e.html.regionMatches(true, 0, "<td", 0, 3)) {
+                        // a new cell has been started without explicitly closing the
+                        // cell that was being updated
+                        updateEndTd = false;
+                    }
                 }
             }
         }