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.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 @@ private void tableCell(boolean headerRow, MutableAttributeSet attributes) { && 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 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 @@ && getCellJustif() != null) { } } + 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 void testFigureFromUrl() { /** * Checks that the sequence [table(), - * tableRows(Sink.JUSTIFY_CENTER, false), tableRow(), tableCell(), - * text(cell), tableCell_(), tableRow_(), tableRows_(), tableCaption(), - * text(caption), tableCaption_(), table_()], + * 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_()], * invoked on the current sink, produces the same result as * {@link #getTableBlock getTableBlock}(cell, caption). */ @@ -648,13 +650,19 @@ public void testFigureFromUrl() { 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.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 @@ private void buildRowLine() { 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 @@ protected String getTableBlock(String cell, String caption) { 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 @@ private void writeEmptyTableHeader() { /** 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 @@ private void writeTableDelimiterRow() { 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 void tableCell(SinkEventAttributes attributes) { } 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 @@ protected String getFigureBlock(String source, String caption) { /** {@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 @@ protected String getTableWithHeaderBlock(String... rowPrefixes) { 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 @@ protected String getFigureBlock(String source, String caption) { /** {@inheritDoc} */ protected String getTableBlock(String cell, String caption) { - return "\n\n
" + caption + "
" + cell - + "
"; + return "\n\n" + + "\n" + + "\n" + + "
" + caption + "
" + cell + "" + cell + "" + cell + "
"; } @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 @@ protected String getFigureBlock(String source, String caption) { /** {@inheritDoc} */ protected String getTableBlock(String cell, String caption) { return "" - + "\n" - + "
Table caption
cell
"; + + "" + caption + "\n" + + "\n" + + "cell\n" + + "" + cell + "\n" + + "" + cell + "" + + ""; } @Override @@ -219,14 +223,6 @@ protected String getTableWithHeaderBlock(String... rowPrefixes) { + ""; } - // 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 "

" + text + "

"; 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);