CSV to HTML translation: Difference between revisions
Content deleted Content added
Line 1,626: | Line 1,626: | ||
</table></lang> |
</table></lang> |
||
=={{header| |
=={{header|Mathematica}}== |
||
<lang Mathematica>a = "Character,Speech |
|||
{{incomplete|MATLAB|No escaping of '<' in '<angry' and '</angry'}} |
|||
The multitude,The messiah! Show us the messiah! |
|||
The easiest way to import csv data into MATLAB is to save it in a csv file, then import the data using the "uiimport -file" command. The result of which will be a cell array with each entry being a string delimited by the newline character. |
|||
Brians mother,<angry>Now you listen here! He's not the messiah;he's a very naughty boy! Now go away!</angry> |
|||
The multitude,Who are you? |
|||
Example: |
|||
Brians mother,I'm his mother;that's who! |
|||
<lang MATLAB>>> data |
|||
The multitude,Behold his mother! Behold his mother!"; |
|||
(*Naive*) |
|||
data = |
|||
StringJoin["<table>\n", |
|||
Map[StringJoin["<tr><td>", #, "</td></tr>\n"] &, StringSplit[StringReplace[a, "," -> "</td><td>"], "\n"]], |
|||
'Character,Speech' |
|||
"</table>"] |
|||
'The multitude,The messiah! Show us the messiah!' |
|||
(*Extra*) |
|||
[1x109 char] |
|||
StringJoin["<table>\n", |
|||
'The multitude,Who are you?' |
|||
StringJoin["<tr><th>", #, "</th></tr>\n"]&[ StringSplit[ StringReplace[a, "," -> "</th><th>"], "\n"] //First], |
|||
'Brians mother,I'm his mother; that's who!' |
|||
Map[StringJoin["<tr><td>",#,"</td></tr>\n"]&,StringSplit[StringReplace[a, "," -> "</td><td>"], "\n"] //Rest], |
|||
'The multitude,Behold his mother! Behold his mother!'</lang> |
|||
"</table>"] |
|||
</lang> |
|||
The other way is to copy and paste the data, but you will have to manually add all of the newline characters and string quotations your self. This is messy and infeasible for large amounts of data, but it is still a valid input. |
|||
Output: |
|||
<pre><table> |
|||
Example: |
|||
< |
<tr><td>Character</td><td>Speech</td></tr> |
||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
|||
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah;he's a very naughty boy! Now go away!</angry></td></tr> |
|||
<tr><td>The multitude</td><td>Who are you?</td></tr> |
|||
<tr><td>Brians mother</td><td>I'm his mother;that's who!</td></tr> |
|||
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
|||
</table> |
|||
csvData = |
|||
Character,Speech |
|||
The multitude,The messiah! Show us the messiah! |
|||
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry> |
|||
The multitude,Who are you? |
|||
Brians mother,I'm his mother; that's who! |
|||
The multitude,Behold his mother! Behold his mother!</lang> |
|||
So, to be able to accept both type of inputs, the function "csvToHTHML()" tests to see if the input is a string as in example 2. If the input is a string it converts the data to a cell array of strings formatted in the exact same way MATLAB formats the first example. The output of the function will be a properly formatted HTML table, which includes the "<THEAD>" around the first row of data so that special formatting can be applied to the column titles. |
|||
<lang MATLAB>function htmlOutput = csvToHTML(csvData) |
|||
if ischar(csvData) |
|||
newlineIndex = find( (csvData == char(10)) ); %find all newline characters |
|||
text = cell( numel(newlineIndex),1 ); %preallocate space for processed data |
|||
for i = (numel(newlineIndex):-1:1) %iterate backwards |
|||
text{i} = csvData(newlineIndex(i)+1:end); |
|||
csvData(newlineIndex(i):end) = []; %delete the newline and everything after it |
|||
end |
|||
csvData = text; |
|||
clear text; |
|||
end |
|||
htmlOutput = '<table>'; |
|||
for i = (1:numel(csvData)) |
|||
if i == 1 %If first entry, then include <thead> |
|||
csvData{i} = [char(10) char(9) '<thead><tr>' char(10) char(9) char(9) '<td>'... |
|||
csvData{i} '</td>' char(10) char(9) '</tr></thead>' char(10)]; |
|||
else %Otherwise, the data is a regular table row |
|||
csvData{i} = [char(9) '<tr>' char(10) char(9) char(9) '<td>' csvData{i}... |
|||
'</td>' char(10) char(9) '</tr>' char(10)]; |
|||
end %if |
|||
%Convert each comma to its HTML equivalent |
|||
for j = fliplr(find( (csvData{i} == ',') )) |
|||
csvData{i} = [csvData{i}(1:j-1) '</td>' char(10) char(9) char(9) '<td>' csvData{i}(j+1:end)]; |
|||
end %for |
|||
%We could make this faster by preallocating htmlOutput but that is |
|||
%more work than necessary to provide a basic solution |
|||
htmlOutput = [htmlOutput csvData{i}]; |
|||
end %for |
|||
htmlOutput = [htmlOutput '</table>']; |
|||
end %csvToHTML</lang> |
|||
Ouput: |
|||
<lang html5>>> csvToHTML(data) |
|||
ans = |
|||
<table> |
<table> |
||
<tr><th>Character</th><th>Speech</th></tr> |
|||
<thead><tr> |
|||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
|||
<td>Character</td> |
|||
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah;he's a very naughty boy! Now go away!</angry></td></tr> |
|||
<td>Speech</td> |
|||
<tr><td>The multitude</td><td>Who are you?</td></tr> |
|||
</tr></thead> |
|||
<tr><td>Brians mother</td><td>I'm his mother;that's who!</td></tr> |
|||
<tr> |
|||
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
|||
</table> |
|||
<td>The messiah! Show us the messiah!</td> |
|||
</pre> |
|||
<tr> |
|||
<td>Brians mother</td> |
|||
<td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td> |
|||
</tr> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</td> |
|||
</tr> |
|||
<tr> |
|||
<td>Brians mother</td> |
|||
<td>I'm his mother; that's who!</td> |
|||
</tr> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Behold his mother! Behold his mother!</td> |
|||
</tr> |
|||
</table></lang> |
|||
=={{header|ML/I}}== |
=={{header|ML/I}}== |