Created
November 26, 2023 13:51
-
-
Save SirYwell/201da8fd282554c525fdeacfb5457e2e to your computer and use it in GitHub Desktop.
Because docutils is just terrible...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class RstTableReformatter { | |
public static void main(String[] args) { | |
Table table = parse("Your table here"); | |
System.out.println(print(table)); | |
} | |
public static class Table { | |
public Row header; | |
public List<Row> body; | |
} | |
public static class Row { | |
public String[] entries; | |
} | |
public static Table parse(String string) { | |
Table table = new Table(); | |
table.body = new ArrayList<>(); | |
String[] tableLines = string.lines().toArray(String[]::new); | |
boolean seenHeader = false; | |
Row current = null; | |
for (String line : tableLines) { | |
if (line.startsWith("+")) { | |
if (current != null) { | |
if (seenHeader) { | |
table.body.add(current); | |
} else { | |
table.header = current; | |
} | |
current = null; | |
} | |
if (line.startsWith("+=")) { | |
seenHeader = true; | |
} | |
continue; | |
} | |
if (line.startsWith("|")) { | |
String[] array = Arrays.stream(line.split("\\|")).skip(1).map(String::strip).toArray(String[]::new); | |
if (current == null) { | |
current = new Row(); | |
current.entries = array; | |
} else { | |
for (int i = 0; i < array.length; i++) { | |
current.entries[i] += " " + array[i]; | |
} | |
} | |
} | |
} | |
return table; | |
} | |
public static String print(Table table) { | |
int columns = table.header.entries.length; | |
int[] widths = new int[columns]; | |
String[] entries = table.header.entries; | |
for (int i = 0; i < entries.length; i++) { | |
widths[i] = entries[i].length(); | |
} | |
for (Row row : table.body) { | |
for (int i = 0; i < row.entries.length; i++) { | |
widths[i] = Math.max(widths[i], row.entries[i].length()); | |
} | |
} | |
for (int i = 0; i < widths.length; i++) { | |
widths[i] = widths[i] + 2; // leading and trailing space | |
} | |
StringBuilder sb = new StringBuilder(); | |
for (int width : widths) { | |
sb.append("+"); | |
sb.append("-".repeat(Math.max(0, width))); | |
} | |
sb.append("+\n"); | |
sb.append("|"); | |
for (int i = 0; i < table.header.entries.length; i++) { | |
sb.append(" "); | |
sb.append(table.header.entries[i]); | |
sb.append(" ".repeat(Math.max(0, widths[i] - table.header.entries[i].length() - 1))); | |
sb.append("|"); | |
} | |
sb.append("\n"); | |
for (int width : widths) { | |
sb.append("+"); | |
sb.append("=".repeat(Math.max(0, width))); | |
} | |
sb.append("+\n"); | |
for (Row row : table.body) { | |
sb.append("|"); | |
for (int i = 0; i < row.entries.length; i++) { | |
sb.append(" "); | |
sb.append(row.entries[i]); | |
sb.append(" ".repeat(Math.max(0, widths[i] - row.entries[i].length() - 1))); | |
sb.append("|"); | |
} | |
sb.append("\n"); | |
sb.append("+"); | |
for (int width : widths) { | |
sb.append("-".repeat(Math.max(0, width))); | |
sb.append("+"); | |
} | |
sb.append("\n"); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment