Added text-alignment for table.js extension

This commit is contained in:
Karthik Balakrishnan 2014-12-28 16:39:37 +05:30
parent 053288f2fe
commit cb00d4c636
11 changed files with 84 additions and 49 deletions

View File

@ -11,62 +11,94 @@
* |**bold** | ![Valid XHTML] (http://w3.org/Icons/valid-xhtml10) | * |**bold** | ![Valid XHTML] (http://w3.org/Icons/valid-xhtml10) |
* | Plain | Value | * | Plain | Value |
* *
*
*
* | Col 1 | Col 2|
* |:========:|===================================================:|
* | col 1 | is center aligned|
* | col 2 | is right aligned|
*
*/ */
(function(){ (function(){
var table = function(converter) { var table = function(converter) {
var tables = {}, style = 'text-align:left;', filter; var tables = {}, filter;
tables.th = function(header){ tables.th = function(header, style) {
if (header.trim() === "") { return "";} if (header.trim() === "") { return "";}
var id = header.trim().replace(/ /g, '_').toLowerCase(); var id = header.trim().replace(/ /g, '_').toLowerCase();
return '<th id="' + id + '" style="'+style+'">' + header + '</th>'; return '<th id="' + id + '" style="' + style + '">' + header + '</th>';
}; };
tables.td = function(cell) { tables.td = function(cell, style) {
return '<td style="'+style+'">' + converter.makeHtml(cell) + '</td>'; return '<td style="' + style + '">' + converter.makeHtml(cell) + '</td>';
}; };
tables.ths = function(){ tables.ths = function() {
var out = "", i = 0, hs = [].slice.apply(arguments); var out = "", i = 0, hs = [].slice.apply(arguments[0]), style = [].slice.apply(arguments[1]);
for (i;i<hs.length;i+=1) { for (i;i<hs.length;i+=1) {
out += tables.th(hs[i]) + '\n'; out += tables.th(hs[i], style[i]) + '\n';
} }
return out; return out;
}; };
tables.tds = function(){ tables.tds = function() {
var out = "", i = 0, ds = [].slice.apply(arguments); var out = "", i = 0, ds = [].slice.apply(arguments[0]), style = [].slice.apply(arguments[1]);
for (i;i<ds.length;i+=1) { for (i;i<ds.length;i+=1) {
out += tables.td(ds[i]) + '\n'; out += tables.td(ds[i], style[i]) + '\n';
} }
return out; return out;
}; };
tables.thead = function() { tables.thead = function() {
var out, i = 0, hs = [].slice.apply(arguments); var out, i = 0, hs = [].slice.apply(arguments[0]), style = [].slice.apply(arguments[1]);
out = "<thead>\n"; out = "<thead>\n";
out += "<tr>\n"; out += "<tr>\n";
out += tables.ths.apply(this, hs); out += tables.ths.apply(this, [hs, style]);
out += "</tr>\n"; out += "</tr>\n";
out += "</thead>\n"; out += "</thead>\n";
return out; return out;
}; };
tables.tr = function() { tables.tr = function() {
var out, i = 0, cs = [].slice.apply(arguments); var out, i = 0, cs = [].slice.apply(arguments[0]), style = [].slice.apply(arguments[1]);
out = "<tr>\n"; out = "<tr>\n";
out += tables.tds.apply(this, cs); out += tables.tds.apply(this, [cs, style]);
out += "</tr>\n"; out += "</tr>\n";
return out; return out;
}; };
filter = function(text) { filter = function(text) {
var i=0, lines = text.split('\n'), line, hs, rows, out = []; var i=0, lines = text.split('\n'), line, hs, rows, out = [];
for (i; i<lines.length;i+=1) { for (i;i<lines.length;i+=1) {
line = lines[i]; line = lines[i];
// looks like a table heading // looks like a table heading
if (line.trim().match(/^[|]{1}.*[|]{1}$/)) { if (line.trim().match(/^[|]{1}.*[|]{1}$/)) {
line = line.trim(); line = line.trim();
var tbl = []; var tbl = [];
var align = lines[i+1];
align = align.trim();
var styles = [];
if (align.match(/^[|]{1}[-=|: ]+[|]{1}$/)) {
styles = align.substring(1, align.length -1).split('|');
var j=0;
for (j;j<styles.length;j+=1) {
styles[j] = styles[j].trim();
if (styles[j].match(/^[:]{1}[-=| ]+[:]{1}$/)) {
styles[j] = 'text-align:center;';
}
else if (styles[j].match(/^[-=| ]+[:]{1}$/)) {
styles[j] = 'text-align:right;';
}
else {
styles[j] = 'text-align:left;';
}
}
}
tbl.push('<table>'); tbl.push('<table>');
hs = line.substring(1, line.length -1).split('|'); hs = line.substring(1, line.length -1).split('|');
tbl.push(tables.thead.apply(this, hs)); if (styles.length === 0) {
var j=0;
for (j;j<hs.length;j+=1) {
styles.push('text-align:left');
}
}
tbl.push(tables.thead.apply(this, [hs, styles]));
line = lines[++i]; line = lines[++i];
if (!line.trim().match(/^[|]{1}[-=| ]+[|]{1}$/)) { if (!line.trim().match(/^[|]{1}[-=|: ]+[|]{1}$/)) {
// not a table rolling back // not a table rolling back
line = lines[--i]; line = lines[--i];
} }
@ -75,7 +107,7 @@
tbl.push('<tbody>'); tbl.push('<tbody>');
while (line.trim().match(/^[|]{1}.*[|]{1}$/)) { while (line.trim().match(/^[|]{1}.*[|]{1}$/)) {
line = line.trim(); line = line.trim();
tbl.push(tables.tr.apply(this, line.substring(1, line.length -1).split('|'))); tbl.push(tables.tr.apply(this, [line.substring(1, line.length -1).split('|'), styles]));
line = lines[++i]; line = lines[++i];
} }
tbl.push('</tbody>'); tbl.push('</tbody>');

View File

@ -2,19 +2,19 @@
<thead> <thead>
<tr> <tr>
<th id="first_header" style="text-align:left;"> First Header </th> <th id="first_header" style="text-align:left;"> First Header </th>
<th id="second_header" style="text-align:left;"> Second Header </th> <th id="second_header" style="text-align:center;"> Second Header </th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td style="text-align:left;"><p>Row 1 Cell 1 </p></td> <td style="text-align:left;"><p>Row 1 Cell 1 </p></td>
<td style="text-align:left;"><p>Row 1 Cell 2 </p></td> <td style="text-align:center;"><p>Row 1 Cell 2 </p></td>
</tr> </tr>
<tr> <tr>
<td style="text-align:left;"><p>Row 2 Cell 1 </p></td> <td style="text-align:left;"><p>Row 2 Cell 1 </p></td>
<td style="text-align:left;"><p>Row 2 Cell 2 </p></td> <td style="text-align:center;"><p>Row 2 Cell 2 </p></td>
</tr> </tr>
</tbody> </tbody>

View File

@ -1,4 +1,4 @@
| First Header | Second Header | | First Header | Second Header |
| ------------- | ------------- | | ------------- |: ----------- :|
| Row 1 Cell 1 | Row 1 Cell 2 | | Row 1 Cell 1 | Row 1 Cell 2 |
| Row 2 Cell 1 | Row 2 Cell 2 | | Row 2 Cell 1 | Row 2 Cell 2 |

View File

@ -2,8 +2,8 @@
<thead> <thead>
<tr> <tr>
<th id="first_header" style="text-align:left;"> First Header </th> <th id="first_header" style="text-align:left;"> First Header </th>
<th id="second_header" style="text-align:left;"> Second Header </th> <th id="second_header" style="text-align:center;"> Second Header </th>
<th id="third_header" style="text-align:left;"> Third Header </th> <th id="third_header" style="text-align:right;"> Third Header </th>
<th id="fourth_header" style="text-align:left;"> Fourth Header </th> <th id="fourth_header" style="text-align:left;"> Fourth Header </th>
</tr> </tr>
</thead> </thead>
@ -11,36 +11,36 @@
<tbody> <tbody>
<tr> <tr>
<td style="text-align:left;"><p>Row 1 Cell 1 </p></td> <td style="text-align:left;"><p>Row 1 Cell 1 </p></td>
<td style="text-align:left;"><p>Row 1 Cell 2 </p></td> <td style="text-align:center;"><p>Row 1 Cell 2 </p></td>
<td style="text-align:left;"><p>Row 1 Cell 3 </p></td> <td style="text-align:right;"><p>Row 1 Cell 3 </p></td>
<td style="text-align:left;"><p>Row 1 Cell 4 </p></td> <td style="text-align:left;"><p>Row 1 Cell 4 </p></td>
</tr> </tr>
<tr> <tr>
<td style="text-align:left;"><p>Row 2 Cell 1 </p></td> <td style="text-align:left;"><p>Row 2 Cell 1 </p></td>
<td style="text-align:left;"><p>Row 2 Cell 2 </p></td> <td style="text-align:center;"><p>Row 2 Cell 2 </p></td>
<td style="text-align:left;"><p>Row 2 Cell 3 </p></td> <td style="text-align:right;"><p>Row 2 Cell 3 </p></td>
<td style="text-align:left;"><p>Row 2 Cell 4 </p></td> <td style="text-align:left;"><p>Row 2 Cell 4 </p></td>
</tr> </tr>
<tr> <tr>
<td style="text-align:left;"><p>Row 3 Cell 1 </p></td> <td style="text-align:left;"><p>Row 3 Cell 1 </p></td>
<td style="text-align:left;"><p>Row 3 Cell 2 </p></td> <td style="text-align:center;"><p>Row 3 Cell 2 </p></td>
<td style="text-align:left;"><p>Row 3 Cell 3 </p></td> <td style="text-align:right;"><p>Row 3 Cell 3 </p></td>
<td style="text-align:left;"><p>Row 3 Cell 4 </p></td> <td style="text-align:left;"><p>Row 3 Cell 4 </p></td>
</tr> </tr>
<tr> <tr>
<td style="text-align:left;"><p>Row 4 Cell 1 </p></td> <td style="text-align:left;"><p>Row 4 Cell 1 </p></td>
<td style="text-align:left;"><p>Row 4 Cell 2 </p></td> <td style="text-align:center;"><p>Row 4 Cell 2 </p></td>
<td style="text-align:left;"><p>Row 4 Cell 3 </p></td> <td style="text-align:right;"><p>Row 4 Cell 3 </p></td>
<td style="text-align:left;"><p>Row 4 Cell 4 </p></td> <td style="text-align:left;"><p>Row 4 Cell 4 </p></td>
</tr> </tr>
<tr> <tr>
<td style="text-align:left;"><p>Row 5 Cell 1 </p></td> <td style="text-align:left;"><p>Row 5 Cell 1 </p></td>
<td style="text-align:left;"><p>Row 5 Cell 2 </p></td> <td style="text-align:center;"><p>Row 5 Cell 2 </p></td>
<td style="text-align:left;"><p>Row 5 Cell 3 </p></td> <td style="text-align:right;"><p>Row 5 Cell 3 </p></td>
<td style="text-align:left;"><p>Row 5 Cell 4 </p></td> <td style="text-align:left;"><p>Row 5 Cell 4 </p></td>
</tr> </tr>

View File

@ -1,5 +1,5 @@
| First Header | Second Header | Third Header | Fourth Header | | First Header | Second Header | Third Header | Fourth Header |
| ------------- | ------------- | ------------ | ------------- | | ------------- |: ----------- :| ------------ :| ------------- |
| Row 1 Cell 1 | Row 1 Cell 2 | Row 1 Cell 3 | Row 1 Cell 4 | | Row 1 Cell 1 | Row 1 Cell 2 | Row 1 Cell 3 | Row 1 Cell 4 |
| Row 2 Cell 1 | Row 2 Cell 2 | Row 2 Cell 3 | Row 2 Cell 4 | | Row 2 Cell 1 | Row 2 Cell 2 | Row 2 Cell 3 | Row 2 Cell 4 |
| Row 3 Cell 1 | Row 3 Cell 2 | Row 3 Cell 3 | Row 3 Cell 4 | | Row 3 Cell 1 | Row 3 Cell 2 | Row 3 Cell 3 | Row 3 Cell 4 |

View File

@ -2,19 +2,19 @@
<thead> <thead>
<tr> <tr>
<th id="first_header" style="text-align:left;"> First Header </th> <th id="first_header" style="text-align:left;"> First Header </th>
<th id="second_header" style="text-align:left;"> Second Header </th> <th id="second_header" style="text-align:center;"> Second Header </th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td style="text-align:left;"><p>Row 1 Cell 1 </p></td> <td style="text-align:left;"><p>Row 1 Cell 1 </p></td>
<td style="text-align:left;"><p>Row 1 Cell 2 </p></td> <td style="text-align:center;"><p>Row 1 Cell 2 </p></td>
</tr> </tr>
<tr> <tr>
<td style="text-align:left;"><p>Row 2 Cell 1 </p></td> <td style="text-align:left;"><p>Row 2 Cell 1 </p></td>
<td style="text-align:left;"><p>Row 2 Cell 2 </p></td> <td style="text-align:center;"><p>Row 2 Cell 2 </p></td>
</tr> </tr>
</tbody> </tbody>

View File

@ -1,4 +1,4 @@
| First Header | Second Header | | First Header | Second Header |
| ============= | ============= | |:============= |:=============:|
| Row 1 Cell 1 | Row 1 Cell 2 | | Row 1 Cell 1 | Row 1 Cell 2 |
| Row 2 Cell 1 | Row 2 Cell 2 | | Row 2 Cell 1 | Row 2 Cell 2 |

View File

@ -6,20 +6,23 @@ vulputate dictum. Vestibulum consequat ultricies nibh, sed tempus nisl mattis a.
<table> <table>
<thead> <thead>
<tr> <tr>
<th id="first_header" style="text-align:left;"> First Header </th> <th id="first_header" style="text-align:right;"> First Header </th>
<th id="second_header" style="text-align:left;"> Second Header </th> <th id="second_header" style="text-align:left;"> Second Header </th>
<th id="third_header" style="text-align:center;"> Third Header </th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td style="text-align:left;"><p>Row 1 Cell 1 </p></td> <td style="text-align:right;"><p>Row 1 Cell 1 </p></td>
<td style="text-align:left;"><p>Row 1 Cell 2 </p></td> <td style="text-align:left;"><p>Row 1 Cell 2 </p></td>
<td style="text-align:center;"><p>Row 1 Cell 3 </p></td>
</tr> </tr>
<tr> <tr>
<td style="text-align:left;"><p>Row 2 Cell 1 </p></td> <td style="text-align:right;"><p>Row 2 Cell 1 </p></td>
<td style="text-align:left;"><p>Row 2 Cell 2 </p></td> <td style="text-align:left;"><p>Row 2 Cell 2 </p></td>
<td style="text-align:center;"><p>Row 2 Cell 3 </p></td>
</tr> </tr>
</tbody> </tbody>

View File

@ -3,10 +3,10 @@ ullamcorper euismod iaculis sed, tristique at neque. Nullam metus risus,
malesuada vitae imperdiet ac, tincidunt eget lacus. Proin ullamcorper malesuada vitae imperdiet ac, tincidunt eget lacus. Proin ullamcorper
vulputate dictum. Vestibulum consequat ultricies nibh, sed tempus nisl mattis a. vulputate dictum. Vestibulum consequat ultricies nibh, sed tempus nisl mattis a.
| First Header | Second Header | | First Header | Second Header | Third Header |
| ------------- | ------------- | | ------------: | ------------- | :-----------: |
| Row 1 Cell 1 | Row 1 Cell 2 | | Row 1 Cell 1 | Row 1 Cell 2 | Row 1 Cell 3 |
| Row 2 Cell 1 | Row 2 Cell 2 | | Row 2 Cell 1 | Row 2 Cell 2 | Row 2 Cell 3 |
Phasellus ac porttitor quam. Integer cursus accumsan mauris nec interdum. Phasellus ac porttitor quam. Integer cursus accumsan mauris nec interdum.
Etiam iaculis urna vitae risus facilisis faucibus eu quis risus. Sed aliquet Etiam iaculis urna vitae risus facilisis faucibus eu quis risus. Sed aliquet

View File

@ -1,7 +1,7 @@
<table> <table>
<thead> <thead>
<tr> <tr>
<th id="first_header" style="text-align:left;"> First Header </th> <th id="first_header" style="text-align:right;"> First Header </th>
<th id="second_header" style="text-align:left;"> Second Header </th> <th id="second_header" style="text-align:left;"> Second Header </th>
</tr> </tr>
</thead> </thead>

View File

@ -1,2 +1,2 @@
| First Header | Second Header | | First Header | Second Header |
| ------------- | ------------- | | ------------ :| ------------- |