Skip to content

Commit

Permalink
Fixes asciidoctor#1265. Support setting column widths
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpanzer committed Mar 16, 2024
1 parent c07b2fd commit d31d6fa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ enum VerticalAlignment {
*/
void setGrid(String grid);

void assignColumnWidths();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.asciidoctor.jruby.internal.RubyObjectWrapper;
import org.jruby.RubyArray;
import org.jruby.RubyNil;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

import java.util.AbstractList;
Expand Down Expand Up @@ -72,6 +73,26 @@ public List<Row> getHeader() {
return rows.getHeader();
}

@Override
public void assignColumnWidths() {
int widthBase = 0;
RubyArray autowidthCols = getRuntime().newArray();
for (Column column : getColumns()) {
int width = column.getWidth();
if (width < 0) {
autowidthCols.add(((ColumnImpl)column).getRubyObject());
} else {
widthBase += width;
}
}
ThreadContext threadContext = getRuntime().getThreadService().getCurrentContext();

getRubyObject().callMethod(threadContext, "assign_column_widths", new IRubyObject[] {
getRuntime().newFixnum(widthBase),
autowidthCols.isEmpty() ? getRuntime().getNil() : autowidthCols
});
}

private class Rows extends RubyObjectWrapper {

public Rows(IRubyObject rubyNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ class GithubContributorsBlockMacro extends BlockMacroProcessor {

// Create the columns 'Login' and 'Contributions'
Column avatarColumn = createTableColumn(table, 0)
avatarColumn.width = 1
Column loginColumn = createTableColumn(table, 1)
loginColumn.width = 2
Column contributionsColumn = createTableColumn(table, 2)
contributionsColumn.width = 2
contributionsColumn.horizontalAlignment = Table.HorizontalAlignment.CENTER

table.columns << avatarColumn
table.columns << loginColumn
table.columns << contributionsColumn
table.assignColumnWidths()
// Create the single header row with the column titles
Row headerRow = createTableRow(table)
headerRow.cells << createTableCell(avatarColumn, 'Avatar')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.asciidoctor.test.extension.ClasspathHelper
import org.asciidoctor.util.TestHttpServer
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.select.Elements
import spock.lang.Specification
import spock.lang.TempDir

Expand All @@ -16,6 +17,10 @@ class WhenABlockMacroProcessorCreatesATable extends Specification {
public static final String SECOND_TD = 'td:nth-child(2)'
public static final String THIRD_TD = 'td:nth-child(3)'
public static final String IMG_ELEMENT = 'img'
public static final String COL = 'col'
public static final String STYLE = 'style'
public static final String WIDTH_20 = 'width: 20%'
public static final String WIDTH_40 = 'width: 40%'
public static final String CONTRIBUTOR = 'bob'
public static final String BLOCKMACRO_NAME = 'githubcontributors'

Expand Down Expand Up @@ -62,6 +67,12 @@ githubcontributors::asciidoctor/asciidoctorj[]
then:
Document htmlDocument = Jsoup.parse(resultFile, 'UTF-8')

Elements cols = htmlDocument.select(COL)
cols.size() == 3
cols.get(0).attr(STYLE).contains(WIDTH_20)
cols.get(1).attr(STYLE).contains(WIDTH_40)
cols.get(2).attr(STYLE).contains(WIDTH_40)

htmlDocument.select('table').hasClass('grid-rows')

htmlDocument.select(FIRST_TD).every { tdElement -> tdElement.select(IMG_ELEMENT).size() == 1 }
Expand Down

0 comments on commit d31d6fa

Please sign in to comment.