Revision [23720]
This is an old revision of PLG-Csv made by ThePLG on 2020-03-02 07:33:34.
CSV 2 Table formatter
See Also
- NoDefaultCodeClass - CSS class workaround.
- ShowCsv - HandleCsvData - More advanced code by NilsLindenberg.
This formatter converts inline csv data into a table.
Features
- Rows of the table alternate background color
- Column alignment: left, right, center or default
- Table headers/footers using wiki style heading markers
- Add comments in between data by starting a line with (#); blank lines are ignored
- Cells can be surrounded in double quotes (")
- It is possible to escape semi-colons with a backslash (\), that should appear in text
- Camel links in cell text are linked to their wiki internal pages
- Text in quotes is preserved e.g., whitespace, no splitting on semi-colons
%%(csv) | ||||
; | ==/First Name/==; | ==\Last Name\==; | ==|Address|==; | == Age == |
==Norwegian==; | Sigurd; | Nordmo; | [[Viggo]] Hansteens allé 119\; 1524 MOSS; | 38 |
==Swede==; | Chanelle; | Blomqvist; | Överhogdal 95\; 282 02 HÖRJA; | 61 |
==German==; | Leah; | Ackermann; | Landhausstraße 73\; 15702 Königs Wusterhausen; | 25 |
# Comments are possible. Yes, the following person is a Hobbit! | ||||
==Hobbit==; | Celendine; | "Gam gee"; | ; | 216 |
%% |
First Name | Last Name | Address | Age | |
---|---|---|---|---|
Norwegian | Sigurd | Nordmo | Viggo Hansteens allé 119; 1524 MOSS | 38 |
Swede | Chanelle | Blomqvist | Överhogdal 95; 282 02 HÖRJA | 61 |
German | Leah | Ackermann | "Landhausstraße 73; 15702 Königs Wusterhausen" | 25 |
Hobbit | Celendine | "Gam gee" | 216 |
Installation
- Copy the code below into a file named formatters/csv.php
- And give it the same file permissions as the other files in that directory.
Code
<?php
// convert inline csv data into a table.
// by OnegWR, May 2005, license GPL http://wikkawiki.org/OnegWRCsv
// by ThePLG, Feb 2020, license GPL http://wikkawiki.org/PLG-Csv
// Copy the code below into a file named formatters/csv.php
// And give it the same file permissions as the other files in that directory.
$comments= 0;
$style_header="background-color:#ccc; ";
$style_even="background-color:#ffe; ";
$style_odd="background-color:#eee; ";
$style_error="background-color:#d30; ";
print "<table><tbody>\n";
foreach ($array_csv_lines= preg_split("/[\n]/", $text) as $csv_n => $csv_line)
{
if (preg_match("/^#|^\s*$/",$csv_line))
{
if (preg_match("/^#!\s*th\s*{\s*background-color:\s*([^\s;]*)\s*;\s*}$/", $csv_line, $color)) {
$style_header= "background-color:". $color[1] ."; ";
}
else
{
if (preg_match("/^#!\s*td\s*{.*background-color-even\s*:\s*([^\s;]*)\s*;.*}$/", $csv_line, $color))
$style_even= "background-color:". $color[1] ."; ";
if (preg_match("/^#!\s*td\s*{.*background-color-odd\s*:\s*([^\s;]*)\s*;.*}$/", $csv_line, $color))
$style_odd= "background-color:". $color[1] ."; ";
if (preg_match("/^#!\s*td\s*{.*background-color-error\s*:\s*([^\s;]*)\s*;.*}$/", $csv_line, $color))
$style_error= "background-color:". $color[1] ."; ";
}
$comments++;
continue;
}
print (($csv_n+$comments)%2) ? "<tr style=\"". $style_even ."\">" : "<tr style=\"". $style_odd ."\">";
// https://www.rexegg.com/regex-lookarounds.html
// asserts what precedes the ; is not a backslash \\\\, doesn't accoutn for \\; (escaped backslash semicolon)
// OMFG! https://stackoverflow.com/questions/40479546/how-to-split-on-white-spaces-not-between-quotes
//
foreach (preg_split("/(?<!\\\\);(?=(?:[^\"]*([\"])[^\"]*\\1)*[^\"]*$)/", $csv_line) as $csv_nn => $csv_cell)
{
// https://www.phpliveregex.com
// https://www.regular-expressions.info/quickstart.html
if ($csv_n == $comments) {
$style[$csv_nn]= "padding: 1px 10px 1px 10px; ";
}
if (preg_match("/^\"?\s*==(.*)==\s*\"?$/", $csv_cell, $header))
{
$title[$csv_nn]= $header[1];
if (preg_match("/([\/\\\\|])([^\/\\\\|]*)\\1$/", $title[$csv_nn], $align))
{
switch ($align[1]) {
case "/" : $style[$csv_nn].= "text-align:right; "; break;
case "\\" : $style[$csv_nn].= "text-align:left; "; break;
case "|" : $style[$csv_nn].= "text-align:center; "; break;
}
$title[$csv_nn]= $align[2];
}
print "<th style=\"". $style_header . $style[$csv_nn] ."\">". $this->htmlspecialchars_ent($title[$csv_nn]) ."</th>";
continue;
}
// if a cell is blank, print
//
if (preg_match("/^\s*$/",$csv_cell)) {
print "<td style=\"". $style[$csv_nn] ."\"> </td>";
}
// extract the cell out of it's quotes
//
elseif (preg_match("/^\s*(\"?)(.*)\\1\s*$/", $csv_cell, $matches))
{
if ($matches[1] == "\"")
{
$style[$csv_nn]= "white-space:pre; ". $style[$csv_nn];
$cell= $matches[2];
}
else
$cell= preg_replace('/\\\\;/', ';', $matches[2]);
// test for CamelLink
//
if (preg_match_all("/\[\[([[:alnum:]-]+)\]\]/", $cell, $all_links))
{
$linked= $cell;
foreach ($all_links[1] as $n => $camel_link)
$linked = preg_replace("/\[\[". $camel_link ."\]\]/", $this->Link($camel_link), $linked);
print "<td style=\"". $style[$csv_nn] ."\">". $linked ."</td>"; // no htmlspecialchars_ent()
}
else
print "<td style=\"". $style[$csv_nn] ."\">". $this->htmlspecialchars_ent($cell) ."</td>";
}
else
print "<td style=\"". $style_error . $style[$csv_nn] ."\">ERROR!</td>"; // $this->htmlspecialchars_ent($csv_cell)
}
print "</tr>\n";
}
print "</tbody></table>\n";
?>
// convert inline csv data into a table.
// by OnegWR, May 2005, license GPL http://wikkawiki.org/OnegWRCsv
// by ThePLG, Feb 2020, license GPL http://wikkawiki.org/PLG-Csv
// Copy the code below into a file named formatters/csv.php
// And give it the same file permissions as the other files in that directory.
$comments= 0;
$style_header="background-color:#ccc; ";
$style_even="background-color:#ffe; ";
$style_odd="background-color:#eee; ";
$style_error="background-color:#d30; ";
print "<table><tbody>\n";
foreach ($array_csv_lines= preg_split("/[\n]/", $text) as $csv_n => $csv_line)
{
if (preg_match("/^#|^\s*$/",$csv_line))
{
if (preg_match("/^#!\s*th\s*{\s*background-color:\s*([^\s;]*)\s*;\s*}$/", $csv_line, $color)) {
$style_header= "background-color:". $color[1] ."; ";
}
else
{
if (preg_match("/^#!\s*td\s*{.*background-color-even\s*:\s*([^\s;]*)\s*;.*}$/", $csv_line, $color))
$style_even= "background-color:". $color[1] ."; ";
if (preg_match("/^#!\s*td\s*{.*background-color-odd\s*:\s*([^\s;]*)\s*;.*}$/", $csv_line, $color))
$style_odd= "background-color:". $color[1] ."; ";
if (preg_match("/^#!\s*td\s*{.*background-color-error\s*:\s*([^\s;]*)\s*;.*}$/", $csv_line, $color))
$style_error= "background-color:". $color[1] ."; ";
}
$comments++;
continue;
}
print (($csv_n+$comments)%2) ? "<tr style=\"". $style_even ."\">" : "<tr style=\"". $style_odd ."\">";
// https://www.rexegg.com/regex-lookarounds.html
// asserts what precedes the ; is not a backslash \\\\, doesn't accoutn for \\; (escaped backslash semicolon)
// OMFG! https://stackoverflow.com/questions/40479546/how-to-split-on-white-spaces-not-between-quotes
//
foreach (preg_split("/(?<!\\\\);(?=(?:[^\"]*([\"])[^\"]*\\1)*[^\"]*$)/", $csv_line) as $csv_nn => $csv_cell)
{
// https://www.phpliveregex.com
// https://www.regular-expressions.info/quickstart.html
if ($csv_n == $comments) {
$style[$csv_nn]= "padding: 1px 10px 1px 10px; ";
}
if (preg_match("/^\"?\s*==(.*)==\s*\"?$/", $csv_cell, $header))
{
$title[$csv_nn]= $header[1];
if (preg_match("/([\/\\\\|])([^\/\\\\|]*)\\1$/", $title[$csv_nn], $align))
{
switch ($align[1]) {
case "/" : $style[$csv_nn].= "text-align:right; "; break;
case "\\" : $style[$csv_nn].= "text-align:left; "; break;
case "|" : $style[$csv_nn].= "text-align:center; "; break;
}
$title[$csv_nn]= $align[2];
}
print "<th style=\"". $style_header . $style[$csv_nn] ."\">". $this->htmlspecialchars_ent($title[$csv_nn]) ."</th>";
continue;
}
// if a cell is blank, print
//
if (preg_match("/^\s*$/",$csv_cell)) {
print "<td style=\"". $style[$csv_nn] ."\"> </td>";
}
// extract the cell out of it's quotes
//
elseif (preg_match("/^\s*(\"?)(.*)\\1\s*$/", $csv_cell, $matches))
{
if ($matches[1] == "\"")
{
$style[$csv_nn]= "white-space:pre; ". $style[$csv_nn];
$cell= $matches[2];
}
else
$cell= preg_replace('/\\\\;/', ';', $matches[2]);
// test for CamelLink
//
if (preg_match_all("/\[\[([[:alnum:]-]+)\]\]/", $cell, $all_links))
{
$linked= $cell;
foreach ($all_links[1] as $n => $camel_link)
$linked = preg_replace("/\[\[". $camel_link ."\]\]/", $this->Link($camel_link), $linked);
print "<td style=\"". $style[$csv_nn] ."\">". $linked ."</td>"; // no htmlspecialchars_ent()
}
else
print "<td style=\"". $style[$csv_nn] ."\">". $this->htmlspecialchars_ent($cell) ."</td>";
}
else
print "<td style=\"". $style_error . $style[$csv_nn] ."\">ERROR!</td>"; // $this->htmlspecialchars_ent($csv_cell)
}
print "</tr>\n";
}
print "</tbody></table>\n";
?>
History
- 2005-05-09 Published on main site - OnegWR
- 2019-09-03 Code clean-up, remove deprecated code, more features, fix bugs - ThePLG
TODO
- camel links are really hard to see
- Add more cell formatting options: text/background color, alignment, bold, italic, ...
- Find a way to implement a "\n" in a cell (every call to $this->Format() is another include of formatters/wakka.php, so I would like to find another way...)
- Use more CSS classes
CategoryUserContributions