Skip to content

Commit

Permalink
Add reStructuredText Grid and reddit formats
Browse files Browse the repository at this point in the history
ReStructuredText Grid format uses a different separator for the top and bottom of the header, the assumption has been that the header top and bottom are the same when both are present. A more robust solution could be to define headerTop, headerBottom, and bodyBottom horizontal character definitions for each style.

Reddit has no left or right side lines, but has vertical separators in the middle, which was also new. A more robust solution could be to define headerLeft, headerRight, bodyLeft, and bodyRight vertical character definitions for each style.
  • Loading branch information
dwesely committed Sep 13, 2018
1 parent 933d9c1 commit 05cf73c
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 28 deletions.
57 changes: 47 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,53 @@ Press **Create Table** to get something like:
This is a row with only one cell
```

### ASCII table (Github Markdown style)
```
| Col1 | Col2 | Col3 | Numeric Column |
|----------------------------------|---------|------------------------|----------------|
| Value 1 | Value 2 | 123 | 10.0 |
| Separate | cols | with a tab or 4 spaces | -2,027.1 |
| This is a row with only one cell | | | |
```

### ASCII table (Reddit Markdown style)
```
Col1 | Col2 | Col3 | Numeric Column
----------------------------------|---------|------------------------|----------------
Value 1 | Value 2 | 123 | 10.0
Separate | cols | with a tab or 4 spaces | -2,027.1
This is a row with only one cell | | |
```

### ASCII table ([reStructuredText](http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#tables) Grid style)
```
+----------------------------------+---------+------------------------+----------------+
| Col1 | Col2 | Col3 | Numeric Column |
+==================================+=========+========================+================+
| Value 1 | Value 2 | 123 | 10.0 |
| Separate | cols | with a tab or 4 spaces | -2,027.1 |
| This is a row with only one cell | | | |
+----------------------------------+---------+------------------------+----------------+
```

### ASCII table ([reStructuredText](http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#tables) Simple style)
```
================================== ========= ======================== ================
Col1 Col2 Col3 Numeric Column
================================== ========= ======================== ================
Value 1 Value 2 123 10.0
Separate cols with a tab or 4 spaces -2,027.1
This is a row with only one cell
================================== ========= ======================== ================
```

### ASCII table (Jira style)
```
|| Col1 || Col2 || Col3 || Numeric Column ||
| Value 1 | Value 2 | 123 | 10.0 |
| Separate | cols | with a tab or 4 spaces | -2,027.1 |
| This is a row with only one cell | | | |
```

### ASCII table (rounded style)
```
Expand Down Expand Up @@ -138,16 +185,6 @@ Press **Create Table** to get something like:
| This is a row with only one cell | | | |
```

### reStructuredText table
```
================================== ========= ======================== ================
Col1 Col2 Col3 Numeric Column
================================== ========= ======================== ================
Value 1 Value 2 123 10.0
Separate cols with a tab or 4 spaces -2,027.1
This is a row with only one cell
================================== ========= ======================== ================
```
### Wikimedia markup

### Or even a boring html <table>
Expand Down
76 changes: 59 additions & 17 deletions assets/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ function createTable() {
var hasLineSeparators = false; // Defaults to no separator lines btwn data rows
var hasTopLine = true; // Defaults to including the topmost line
var hasBottomLine = true; // Defaults to including the bottom-most line
var hasLeftSide = true; // Defaults to including the left side line
var hasRightSide = true; // Defaults to including the right side line
var topLineUsesBodySeparators = false; // Defaults to top line uses the same separators as the line between header and body
var align; // Default alignment: left-aligned
switch (style) {
case "mysql":
Expand Down Expand Up @@ -182,6 +184,42 @@ function createTable() {
hdV = "|"; hdH = "-";
spV = "|"; spH = "-";
break;
case "reddit":
// reddit markdown
hasTopLine = false;
hasBottomLine = false;
hasLeftSide = false;
hasRightSide = false;
cTL = " "; cTM = "|"; cTR = " ";
cML = " "; cMM = "|"; cMR = " ";
cBL = " "; cBM = "|"; cBR = " ";

hdV = "|"; hdH = "-";
spV = "|"; spH = "-";
break;
case "rstGrid":
// reStructuredText Grid markup
hasTopLine = true;
topLineUsesBodySeparators = true;
hasBottomLine = true;
cTL = "+"; cTM = "+"; cTR = "+";
cML = "+"; cMM = "+"; cMR = "+";
cBL = "+"; cBM = "+"; cBR = "+";

hdV = "|"; hdH = "=";
spV = "|"; spH = "-";
break;
case "rstSimple":
// reStructuredText Simple markup
hasTopLine = true;
hasBottomLine = true;
cTL = " "; cTM = " "; cTR = " ";
cML = " "; cMM = " "; cMR = " ";
cBL = " "; cBM = " "; cBR = " ";

hdV = " "; hdH = "=";
spV = " "; spH = "=";
break;
case "jira":
// jira markdown
hasTopLine = false;
Expand All @@ -194,7 +232,7 @@ function createTable() {
cBL = ""; cBM = ""; cBR = "";

hdV = "||"; hdH = "";
spV = "|"; spH = "";
spV = "| "; spH = "";
break;
case "wikim":
// wikimedia
Expand All @@ -209,15 +247,6 @@ function createTable() {
hdV = "\n!"; hdH = "";
spV = "\n|"; spH = "";
break;
case "restructured":
// restructured table
cTL = " "; cTM = " "; cTR = " ";
cML = " "; cMM = " "; cMR = " ";
cBL = " "; cBM = " "; cBR = " ";

hdV = " "; hdH = "=";
spV = " "; spH = "=";
break;
case "unicode":
// unicode
cTL = "\u2554"; cTM = "\u2566"; cTR = "\u2557";
Expand Down Expand Up @@ -249,14 +278,21 @@ function createTable() {
// output the top most row
// Ex: +---+---+
if (hasTopLine ) {
if (topLineUsesBodySeparators || !hasHeaders) {
topLineHorizontal = spH;
} else {
topLineHorizontal = hdH;
}

for (var j = 0; j <= colLengths.length; j++) {
if ( !hasHeaders ) {
hdH = spH;
}
if ( j == 0 ) {
output += cTL + _repeat(hdH, colLengths[j] + 2);
if (topLineUsesBodySeparators) {
output += cTL + _repeat(topLineHorizontal, colLengths[j] + 2);
} else {
output += cTL + _repeat(topLineHorizontal, colLengths[j] + 2);
}
} else if ( j < colLengths.length ) {
output += cTM + _repeat(hdH, colLengths[j] + 2);
output += cTM + _repeat(topLineHorizontal, colLengths[j] + 2);
} else if (hasRightSide) {
output += cTR + "\n";
} else {
Expand All @@ -270,7 +306,9 @@ function createTable() {
if (hasHeaders && hasHeaderSeparators && i == 1 ) {
// output the header separator row
for (var j = 0; j <= colLengths.length; j++) {
if ( j == 0) {
if ( j == 0 && !hasLeftSide) {
output += _repeat(hdH, colLengths[j] + 2);
} else if ( j == 0) {
output += cML + _repeat(hdH, colLengths[j] + 2);
} else if (j < colLengths.length) {
output += cMM + _repeat(hdH, colLengths[j] + 2);
Expand Down Expand Up @@ -317,7 +355,11 @@ function createTable() {
}
if ( j < colLengths.length ) {
data = _pad(data, colLengths[j], " ", align);
output += verticalBar + " " + data + " ";
if (j == 0 && !hasLeftSide) {
output += " " + data + " ";
} else {
output += verticalBar + " " + data + " ";
}
} else if (hasRightSide) {
output += verticalBar + "\n";
} else {
Expand Down
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ <h2>Input</h2>
<option value="separated">ASCII (separated)</option>
<option value="compact">ASCII (compact)</option>
<option value="gfm">Github Markdown</option>
<option value="reddit">Reddit Markdown</option>
<option value="rstGrid">reStructuredText Grid</option>
<option value="rstSimple">reStructuredText Simple</option>
<option value="jira">Jira</option>
<option value="rounded">ASCII (rounded)</option>
<option value="bubbles">ASCII (bubbles)</option>
<option value="girder">ASCII (girder)</option>
<option value="dots">ASCII (dots)</option>
<option value="unicode">Unicode</option>
<option value="unicode_single_line">Unicode (single line)</option>
<option value="restructured">reStructuredText</option>
<option value="wikim">Wikimedia</option>
<option value="html">HTML</option>
</select>
Expand Down

0 comments on commit 05cf73c

Please sign in to comment.