Stack Exchange doesn't know how to transpose tables. Let's help.
Given a markdown table, transpose it.
Input assumptions:
- There will be at least two rows (including header) and two columns
- Either all cells have no leading space or all cells have exactly one leading space (you must handle both)
- If the cells have a leading space, then the widest cell in every column has exactly one trailing space, otherwise, the widest cell in every column has no trailing spaces
- All pipes line up
- The header-body separator lines of dashes extend the full width of their column, except a leading and tailing space if the table uses this
- Cell alignment (
-:,:-:, etc.) is not used - No other extraneous spaces appear (this includes between words)
- Either all rows have a trailing pipe or no rows have a trailing pipe (you must handle both)
- Cells contain only printable ASCII, but no pipes (
|), dashes (-) or any characters that need special treatment (\,**, etc.) - All cells will have at least some non-space content
Output requirements:
- Trailing pipe on every row or no trailing pipes (must be consist for any one result)
- Either no leading spaces, or exactly one leading space in every cell (must be consist for any one result)
- If you produce leading spaces, then the widest cell in each column must have exactly one trailing space
- All pipes must line up
- The header-body separator must extend to the full width of the column, save for leading and trailing spaces, if used in that result
- Trailing spaces (and up to one trailing line break) are acceptable
Test cases
A
Test inputs (you must handle every one of these)
| A header | Another header | | -------- | -------------- | | First | row | | Second | row | | A header | Another header | -------- | -------------- | First | row | Second | row |A header|Another header| |--------|--------------| |First |row | |Second |row | |A header|Another header |--------|-------------- |First |row |Second |row For any of the above inputs, output must be any one of the below
(not necessarily corresponding 1:1)
| A header | First | Second | | -------------- | ----- | ------ | | Another header | row | row | | A header | First | Second | -------------- | ----- | ------ | Another header | row | row |A header |First|Second| |--------------|-----|------| |Another header|row |row | |A header |First|Second |--------------|-----|------ |Another header|row |row B
Test inputs (you must handle every one of these)
| A header | Another header | Last column here | | -------- | -------------- | ----------------- | | First | 0 | more content here | | Second | row | that's it! | | A header | Another header | Last column here | -------- | -------------- | ----------------- | First | 0 | more content here | Second | row | that's it! |A header|Another header|Last column here | |--------|--------------|-----------------| |First |0 |more content here| |Second |row |that's it! | |A header|Another header|Last column here |--------|--------------|----------------- |First |0 |more content here |Second |row |that's it! For any of the above inputs, output must be any one of the below
(not necessarily corresponding 1:1)
| A header | First | Second | | ---------------- | ----------------- | ---------- | | Another header | 0 | row | | Last column here | more content here | that's it! | | A header | First | Second | ---------------- | ----------------- | ---------- | Another header | 0 | row | Last column here | more content here | that's it! |A header |First |Second | |----------------|-----------------|----------| |Another header |0 |row | |Last column here|more content here|that's it!| |A header |First |Second |----------------|-----------------|---------- |Another header |0 |row |Last column here|more content here|that's it!