Skip to content

Commit

Permalink
[DOXIA-760] Clarify table justification semantics and introduce new (#…
Browse files Browse the repository at this point in the history
…248)

"JUSTIFY_DEFAULT" alignment
  • Loading branch information
kwin authored Nov 21, 2024
1 parent d357e9a commit c36b516
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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);
}
}

Expand All @@ -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_() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,23 +638,31 @@ public void testFigureFromUrl() {

/**
* 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).
*/
@Test
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,10 @@ protected String getFigureBlock(String source, String caption) {

/** {@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,12 @@ protected String getFigureBlock(String source, String caption) {
/** {@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
Expand All @@ -219,14 +223,6 @@ protected String getTableWithHeaderBlock(String... rowPrefixes) {
+ "</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>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit c36b516

Please sign in to comment.