Wiki source for OnegWRCsv


Show raw source

===== CSV 2 Table formatter =====
>>==See Also==
~- [[OnegWRNoDefaultCodeClass | NoDefaultCodeClass]] - CSS class workaround.
~- ShowCsv - HandleCsvData - More advanced code by NilsLindenberg.
>>//NOT Included in any Wikka version yet//{{lastedit}}
==What==
~- This formatter would allow you to convert inline csv data into a table.

==Example==
~- The code:
~~%%(csv)
~~;***a***;"***b***";***c***
~~***1***;d;"e";f
~~#test
~~
~~***2***;g;;h
~~***3***;I;J;"K"
~~%%
~- Would give this as result:
~~""<div><table cellpadding="5" cellspacing="1"><tbody>
<tr bgcolor="#eeeeee"><td> </td><td bgcolor="#cccccc">a</td><td bgcolor="#cccccc">b</td><td bgcolor="#cccccc">c</td></tr>
<tr bgcolor="#ffffee"><td bgcolor="#cccccc">1</td><td>d</td><td>e</td><td>f</td></tr>
<tr bgcolor="#eeeeee"><td bgcolor="#cccccc">2</td><td>g</td><td> </td><td>h</td></tr>
<tr bgcolor="#ffffee"><td bgcolor="#cccccc">3</td><td>I</td><td>J</td><td>K</td></tr>
</tbody></table>
</div>""

==Features==
~- Alternating row background. (hint: only use odd/even lines for a fast alternative layout)
~- Ignores empty lines or lines starting with an '#' (comments).
~~To not disturb the alternated layout: add 2 (comment)lines...
~- The " before and after a cell is auto-removed.
~- You can make the background darker by wrapping the cell in "***"-tags.

==To Do==
~- Add more cell options: 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...)
~- Fix the ';' in a cell (""&gt;"",...)
~- Use more CSS classes

==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)
<?php
// convert inline csv data into a table.
// by OnegWR, may 2005, license GPL
$csv_printheader=0;
foreach(split("\n", $text) as $csv_n => $csv_line){
if(preg_match("/^#|^\s*$/",$csv_line)) continue;
if( $csv_printheader == 0){ print "<table cellpadding=\"5\" cellspacing=\"1\"><tbody>\n"; $csv_printheader=1; }
print ($csv_n%2) ? "<tr bgcolor=\"#ffffee\">" : "<tr bgcolor=\"#eeeeee\">";
foreach(split(";", $csv_line) as $csv_nn => $csv_cell){
if(preg_match("/^\s*$/",$csv_cell)){
print "<td> </td>";
}elseif(preg_match("/^(\"?)\*\*\*(.*?)\*\*\*(\"?)$/",$csv_cell,$matches)){
print "<td bgcolor=\"#cccccc\">".
$this->htmlspecialchars_ent($matches[2])."</td>";
}elseif(preg_match("/^(\"?)(.*?)(\"?)$/",$csv_cell,$matches)){
print "<td>".$this->htmlspecialchars_ent($matches[2])."</td>";
}else{
print "<td>".$this->htmlspecialchars_ent($csv_cell)."</td>";
}
}
print "</tr>\n";
}
if( $csv_printheader == 1) print "</tbody></table>\n";
?>%%
==History==
~- Published on main site (2005-05-09) - OnegWR

==Bugs==
~- Remove the the default yellow background via the [[OnegWRNoDefaultCodeClass | NoDefaultCodeClass]] workaround.
~- If there is an ';' within a cell, this script will make 2 cell from it.

----
CategoryUserContributions
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki