8224257: fix issues in files generated by pandoc

Reviewed-by: mchung
This commit is contained in:
Jonathan Gibbons 2019-06-07 14:32:48 -07:00
parent d0725682a8
commit e27ad50eb0

View File

@ -44,6 +44,7 @@ import java.util.Locale;
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 @@ import java.util.regex.Pattern;
*
* <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 @@ public class Main {
}
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;
}
}
}
}