You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kw...@apache.org on 2024/11/21 21:22:14 UTC
(maven-doxia) branch master updated: [DOXIA-760] Clarify table justification semantics and introduce new (#248)
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-doxia.git
The following commit(s) were added to refs/heads/master by this push:
new c36b5169 [DOXIA-760] Clarify table justification semantics and introduce new (#248)
c36b5169 is described below
commit c36b51695bdf4fe6f515d7e3fcdcec264867d990
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Thu Nov 21 22:22:10 2024 +0100
[DOXIA-760] Clarify table justification semantics and introduce new (#248)
"JUSTIFY_DEFAULT" alignment
---
.../maven/doxia/sink/impl/Xhtml5BaseSink.java | 47 ++++++++++++++--------
.../maven/doxia/sink/impl/AbstractSinkTest.java | 16 ++++++--
.../org/apache/maven/doxia/module/apt/AptSink.java | 8 ++--
.../apache/maven/doxia/module/apt/AptSinkTest.java | 8 ++++
.../doxia/module/markdown/MarkdownMarkup.java | 7 +++-
.../maven/doxia/module/markdown/MarkdownSink.java | 9 +++--
.../doxia/module/markdown/MarkdownSinkTest.java | 10 +++--
.../maven/doxia/module/xdoc/XdocSinkTest.java | 6 ++-
.../maven/doxia/module/xhtml5/Xhtml5SinkTest.java | 16 +++-----
.../java/org/apache/maven/doxia/sink/Sink.java | 13 +++++-
10 files changed, 96 insertions(+), 44 deletions(-)
diff --git a/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSink.java b/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSink.java
index f32aba42..2e3ca4f5 100644
--- a/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSink.java
+++ b/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSink.java
@@ -27,10 +27,8 @@ import java.io.Writer;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.Enumeration;
-import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
import java.util.Objects;
import java.util.Stack;
import java.util.regex.Pattern;
@@ -1086,22 +1084,21 @@ public class Xhtml5BaseSink extends AbstractXmlSink implements HtmlMarkup {
&& getCellJustif() != null) {
int cellCount = getCellCount();
int[] cellJustif = getCellJustif();
+ int currentCellJust =
+ cellCount < cellJustif.length ? cellJustif[cellCount] : cellJustif[cellJustif.length - 1];
if (cellCount < cellJustif.length) {
- if (attributes == null) {
- attributes = new SinkEventAttributeSet();
+ String tdStyle = getStyleForTableJustification(currentCellJust);
+ if (tdStyle != null) {
+ if (attributes == null) {
+ attributes = new SinkEventAttributeSet();
+ } else if (attributes.isDefined(SinkEventAttributes.STYLE)) {
+ tdStyle += " "
+ + attributes
+ .getAttribute(SinkEventAttributes.STYLE)
+ .toString();
+ }
+ attributes.addAttribute(SinkEventAttributes.STYLE, tdStyle);
}
- Map<Integer, String> hash = new HashMap<>();
- hash.put(Sink.JUSTIFY_CENTER, "center");
- hash.put(Sink.JUSTIFY_LEFT, "left");
- hash.put(Sink.JUSTIFY_RIGHT, "right");
-
- String tdStyle = "text-align: " + hash.get(cellJustif[cellCount]) + ";";
- if (attributes.isDefined(SinkEventAttributes.STYLE)) {
- tdStyle += " "
- + attributes.getAttribute(SinkEventAttributes.STYLE).toString();
- }
-
- attributes.addAttribute(SinkEventAttributes.STYLE, tdStyle);
}
}
@@ -1112,6 +1109,24 @@ public class Xhtml5BaseSink extends AbstractXmlSink implements HtmlMarkup {
}
}
+ private static String getStyleForTableJustification(int justification) {
+ String style = "text-align: ";
+ switch (justification) {
+ case Sink.JUSTIFY_CENTER:
+ style += "center;";
+ break;
+ case Sink.JUSTIFY_LEFT:
+ style += "left;";
+ break;
+ case Sink.JUSTIFY_RIGHT:
+ style += "right;";
+ break;
+ default:
+ style = null;
+ }
+ return style;
+ }
+
/** {@inheritDoc} */
@Override
public void tableCell_() {
diff --git a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/AbstractSinkTest.java b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/AbstractSinkTest.java
index 5cd096d2..eddc4958 100644
--- a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/AbstractSinkTest.java
+++ b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/AbstractSinkTest.java
@@ -638,9 +638,11 @@ public abstract class AbstractSinkTest extends AbstractModuleTest {
/**
* Checks that the sequence <code>[table(),
- * tableRows(Sink.JUSTIFY_CENTER, false), tableRow(), tableCell(),
- * text(cell), tableCell_(), tableRow_(), tableRows_(), tableCaption(),
- * text(caption), tableCaption_(), table_()]</code>,
+ * tableRows({Sink.JUSTIFY_CENTER, JUSTIFY_DEFAULT}, false), tableRow(),
+ * tableCell(), text(cell), tableCell_(),
+ * tableCell(), text(cell), tableCell_(),
+ * tableCell(), text(cell), tableCell_(),
+ * tableRow_(), tableRows_(), tableCaption(), text(caption), tableCaption_(), table_()]</code>,
* invoked on the current sink, produces the same result as
* {@link #getTableBlock getTableBlock}(cell, caption).
*/
@@ -648,13 +650,19 @@ public abstract class AbstractSinkTest extends AbstractModuleTest {
public void testTable() {
String cell = "cell";
String caption = "Table_caption";
- int[] justify = {Sink.JUSTIFY_CENTER};
+ int[] justify = {Sink.JUSTIFY_CENTER, Sink.JUSTIFY_DEFAULT};
sink.table();
sink.tableRows(justify, false);
sink.tableRow();
sink.tableCell();
sink.text(cell);
sink.tableCell_();
+ sink.tableCell();
+ sink.text(cell);
+ sink.tableCell_();
+ sink.tableCell();
+ sink.text(cell);
+ sink.tableCell_();
sink.tableRow_();
sink.tableRows_();
sink.tableCaption();
diff --git a/doxia-modules/doxia-module-apt/src/main/java/org/apache/maven/doxia/module/apt/AptSink.java b/doxia-modules/doxia-module-apt/src/main/java/org/apache/maven/doxia/module/apt/AptSink.java
index 87a248f3..55834417 100644
--- a/doxia-modules/doxia-module-apt/src/main/java/org/apache/maven/doxia/module/apt/AptSink.java
+++ b/doxia-modules/doxia-module-apt/src/main/java/org/apache/maven/doxia/module/apt/AptSink.java
@@ -29,6 +29,7 @@ import java.util.List;
import java.util.Stack;
import org.apache.commons.lang3.StringUtils;
+import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.doxia.sink.SinkEventAttributes;
import org.apache.maven.doxia.sink.impl.AbstractTextSink;
import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
@@ -574,15 +575,16 @@ public class AptSink extends AbstractTextSink implements AptMarkup {
rLine.append(TABLE_ROW_START_MARKUP);
for (int i = 0; i < cellCount; i++) {
- if (cellJustif != null) {
+ if (cellJustif != null && i < cellJustif.length) {
switch (cellJustif[i]) {
- case 1:
+ case Sink.JUSTIFY_LEFT:
rLine.append(TABLE_COL_LEFT_ALIGNED_MARKUP);
break;
- case 2:
+ case Sink.JUSTIFY_RIGHT:
rLine.append(TABLE_COL_RIGHT_ALIGNED_MARKUP);
break;
default:
+ // default = centered
rLine.append(TABLE_COL_CENTERED_ALIGNED_MARKUP);
}
} else {
diff --git a/doxia-modules/doxia-module-apt/src/test/java/org/apache/maven/doxia/module/apt/AptSinkTest.java b/doxia-modules/doxia-module-apt/src/test/java/org/apache/maven/doxia/module/apt/AptSinkTest.java
index f27e23d8..58caaaf4 100644
--- a/doxia-modules/doxia-module-apt/src/test/java/org/apache/maven/doxia/module/apt/AptSinkTest.java
+++ b/doxia-modules/doxia-module-apt/src/test/java/org/apache/maven/doxia/module/apt/AptSinkTest.java
@@ -182,12 +182,20 @@ public class AptSinkTest extends AbstractSinkTest {
return EOL
+ AptMarkup.TABLE_ROW_START_MARKUP
+ AptMarkup.TABLE_COL_CENTERED_ALIGNED_MARKUP
+ + AptMarkup.TABLE_COL_CENTERED_ALIGNED_MARKUP
+ + AptMarkup.TABLE_COL_CENTERED_ALIGNED_MARKUP
+ EOL
+ cell
+ AptMarkup.TABLE_ROW_SEPARATOR_MARKUP
+ + cell
+ + AptMarkup.TABLE_ROW_SEPARATOR_MARKUP
+ + cell
+ + AptMarkup.TABLE_ROW_SEPARATOR_MARKUP
+ EOL
+ AptMarkup.TABLE_ROW_START_MARKUP
+ AptMarkup.TABLE_COL_CENTERED_ALIGNED_MARKUP
+ + AptMarkup.TABLE_COL_CENTERED_ALIGNED_MARKUP
+ + AptMarkup.TABLE_COL_CENTERED_ALIGNED_MARKUP
+ EOL
+ caption
+ EOL;
diff --git a/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownMarkup.java b/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownMarkup.java
index 96bd79da..563d14c6 100644
--- a/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownMarkup.java
+++ b/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownMarkup.java
@@ -108,8 +108,11 @@ public interface MarkdownMarkup extends TextMarkup {
/** Syntax for the table cell start: "|" */
String TABLE_CELL_SEPARATOR_MARKUP = String.valueOf(PIPE);
- /** Syntax for the table column, left alignment (default): "---" */
- String TABLE_COL_LEFT_ALIGNED_MARKUP = StringUtils.repeat(String.valueOf(MINUS), 3);
+ /** Syntax for the table column, default alignment (default): "---" */
+ String TABLE_COL_DEFAULT_ALIGNED_MARKUP = StringUtils.repeat(String.valueOf(MINUS), 3);
+
+ /** Syntax for the table column, left alignment (default): ":---" */
+ String TABLE_COL_LEFT_ALIGNED_MARKUP = COLON + StringUtils.repeat(String.valueOf(MINUS), 3);
/** Syntax for the table column, right alignment: "---:" */
String TABLE_COL_RIGHT_ALIGNED_MARKUP = StringUtils.repeat(String.valueOf(MINUS), 3) + COLON;
diff --git a/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java b/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java
index fa1b138c..0c1b6b5b 100644
--- a/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java
+++ b/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java
@@ -652,7 +652,7 @@ public class MarkdownSink extends AbstractTextSink implements MarkdownMarkup {
/** Emit the delimiter row which determines the alignment */
private void writeTableDelimiterRow() {
writeUnescaped(TABLE_ROW_PREFIX);
- int justification = Sink.JUSTIFY_LEFT;
+ int justification = Sink.JUSTIFY_DEFAULT;
for (int i = 0; i < cellCount; i++) {
// keep previous column's alignment in case too few are specified
if (cellJustif != null && cellJustif.size() > i) {
@@ -665,9 +665,12 @@ public class MarkdownSink extends AbstractTextSink implements MarkdownMarkup {
case Sink.JUSTIFY_CENTER:
writeUnescaped(TABLE_COL_CENTER_ALIGNED_MARKUP);
break;
- default:
+ case Sink.JUSTIFY_LEFT:
writeUnescaped(TABLE_COL_LEFT_ALIGNED_MARKUP);
break;
+ default:
+ writeUnescaped(TABLE_COL_DEFAULT_ALIGNED_MARKUP);
+ break;
}
writeUnescaped(TABLE_CELL_SEPARATOR_MARKUP);
}
@@ -697,7 +700,7 @@ public class MarkdownSink extends AbstractTextSink implements MarkdownMarkup {
} else {
// create non-existing justifications for preceding columns
for (int precedingCol = cellJustif.size(); precedingCol < cellCount; precedingCol++) {
- cellJustif.add(Sink.JUSTIFY_LEFT);
+ cellJustif.add(Sink.JUSTIFY_DEFAULT);
}
cellJustif.add(cellJustification);
}
diff --git a/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java b/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java
index e3a02636..bafae03e 100644
--- a/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java
+++ b/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java
@@ -179,9 +179,13 @@ public class MarkdownSinkTest extends AbstractSinkTest {
/** {@inheritDoc} */
protected String getTableBlock(String cell, String caption) {
- return MarkdownMarkup.TABLE_ROW_PREFIX + " " + MarkdownMarkup.TABLE_CELL_SEPARATOR_MARKUP + EOL
+ return MarkdownMarkup.TABLE_ROW_PREFIX + " " + MarkdownMarkup.TABLE_CELL_SEPARATOR_MARKUP + " "
+ + MarkdownMarkup.TABLE_CELL_SEPARATOR_MARKUP + " " + MarkdownMarkup.TABLE_CELL_SEPARATOR_MARKUP + EOL
+ MarkdownMarkup.TABLE_ROW_PREFIX
- + ":---:" + MarkdownMarkup.TABLE_CELL_SEPARATOR_MARKUP + EOL + MarkdownMarkup.TABLE_ROW_PREFIX
+ + ":---:" + MarkdownMarkup.TABLE_CELL_SEPARATOR_MARKUP + "---"
+ + MarkdownMarkup.TABLE_CELL_SEPARATOR_MARKUP + "---" + MarkdownMarkup.TABLE_CELL_SEPARATOR_MARKUP + EOL
+ + MarkdownMarkup.TABLE_ROW_PREFIX
+ + cell + MarkdownMarkup.TABLE_CELL_SEPARATOR_MARKUP + cell + MarkdownMarkup.TABLE_CELL_SEPARATOR_MARKUP
+ cell + MarkdownMarkup.TABLE_CELL_SEPARATOR_MARKUP + EOL;
}
@@ -190,7 +194,7 @@ public class MarkdownSinkTest extends AbstractSinkTest {
StringBuilder expectedMarkup = new StringBuilder();
expectedMarkup.append(MarkdownMarkup.TABLE_ROW_PREFIX + getEscapedText(rowPrefixes[0]) + "0|"
+ getEscapedText(rowPrefixes[0]) + "1|" + getEscapedText(rowPrefixes[0]) + "2|" + EOL);
- expectedMarkup.append(MarkdownMarkup.TABLE_ROW_PREFIX + "---|---:|:---:|" + EOL);
+ expectedMarkup.append(MarkdownMarkup.TABLE_ROW_PREFIX + ":---|---:|:---:|" + EOL);
for (int n = 1; n < rowPrefixes.length; n++) {
expectedMarkup.append(MarkdownMarkup.TABLE_ROW_PREFIX + getEscapedText(rowPrefixes[n]) + "0|"
+ getEscapedText(rowPrefixes[n]) + "1|" + getEscapedText(rowPrefixes[n]) + "2|" + EOL);
diff --git a/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocSinkTest.java b/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocSinkTest.java
index 3f0315c4..d1c5ad77 100644
--- a/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocSinkTest.java
+++ b/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocSinkTest.java
@@ -209,8 +209,10 @@ public class XdocSinkTest extends AbstractSinkTest {
/** {@inheritDoc} */
protected String getTableBlock(String cell, String caption) {
- return "<table border=\"0\"><caption>" + caption + "</caption>\n<tr>\n<td style=\"text-align: center;\">" + cell
- + "</td></tr></table>";
+ return "<table border=\"0\"><caption>" + caption + "</caption>\n<tr>\n"
+ + "<td style=\"text-align: center;\">" + cell + "</td>\n"
+ + "<td>" + cell + "</td>\n"
+ + "<td>" + cell + "</td></tr></table>";
}
@Override
diff --git a/doxia-modules/doxia-module-xhtml5/src/test/java/org/apache/maven/doxia/module/xhtml5/Xhtml5SinkTest.java b/doxia-modules/doxia-module-xhtml5/src/test/java/org/apache/maven/doxia/module/xhtml5/Xhtml5SinkTest.java
index c125ee11..c5c1bf36 100644
--- a/doxia-modules/doxia-module-xhtml5/src/test/java/org/apache/maven/doxia/module/xhtml5/Xhtml5SinkTest.java
+++ b/doxia-modules/doxia-module-xhtml5/src/test/java/org/apache/maven/doxia/module/xhtml5/Xhtml5SinkTest.java
@@ -197,8 +197,12 @@ public class Xhtml5SinkTest extends AbstractSinkTest {
/** {@inheritDoc} */
protected String getTableBlock(String cell, String caption) {
return "<table class=\"bodyTable\">"
- + "<caption>Table caption</caption><tr class=\"a\">\n<td>cell</td></tr>"
- + "</table>";
+ + "<caption>" + caption + "</caption>\n"
+ + "<tr class=\"a\">\n"
+ + "<td style=\"text-align: center;\">cell</td>\n"
+ + "<td>" + cell + "</td>\n"
+ + "<td>" + cell + "</td>"
+ + "</tr></table>";
}
@Override
@@ -219,14 +223,6 @@ public class Xhtml5SinkTest extends AbstractSinkTest {
+ "</table>";
}
- // Disable testTable until the order of attributes issue is clarified
- // TODO: remove
- /** {@inheritDoc} */
- @Test
- public void testTable() {
- assertEquals("", "", "Dummy!");
- }
-
/** {@inheritDoc} */
protected String getParagraphBlock(String text) {
return "<p>" + text + "</p>";
diff --git a/doxia-sink-api/src/main/java/org/apache/maven/doxia/sink/Sink.java b/doxia-sink-api/src/main/java/org/apache/maven/doxia/sink/Sink.java
index f92e045b..07b3774c 100644
--- a/doxia-sink-api/src/main/java/org/apache/maven/doxia/sink/Sink.java
+++ b/doxia-sink-api/src/main/java/org/apache/maven/doxia/sink/Sink.java
@@ -121,6 +121,14 @@ public interface Sink extends AutoCloseable {
*/
int SECTION_LEVEL_6 = 6;
+ /**
+ * Default alignment for table cells.
+ * Actual value depends on the implementation.
+ * @see #tableRows(int[], boolean)
+ * @since 2.1.0
+ */
+ int JUSTIFY_DEFAULT = -1;
+
/**
* Center alignment for table cells.
* @see #tableRows(int[], boolean)
@@ -1122,11 +1130,14 @@ public interface Sink extends AutoCloseable {
* If null a left alignment is assumed by default. If this array
* has less elements than there are columns in the table then the value of
* the last array element will be taken as default for the remaining table cells.
- * @param grid true to provide a grid, false otherwise.
+ * Each element of the array must be one of the following constants:
+ * {@link #JUSTIFY_LEFT}, {@link #JUSTIFY_CENTER}, {@link #JUSTIFY_RIGHT} or {@link #JUSTIFY_DEFAULT}.
+ * @param grid true to render a grid, false otherwise.
* @see #table(SinkEventAttributes)
* @see #JUSTIFY_CENTER
* @see #JUSTIFY_LEFT
* @see #JUSTIFY_RIGHT
+ * @see #JUSTIFY_DEFAULT
*/
void tableRows(int[] justification, boolean grid);