CSV to HTML translation: Difference between revisions
Content deleted Content added
→Output (rendered): Standardize thumbnail size |
|||
Line 1,172: | Line 1,172: | ||
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table></lang> |
</table></lang> |
||
=={{header|Java}}== |
|||
'''Solution including simple and extra credit version''' |
|||
<tt>for simple solution : java -cp . Csv2Html < text.csv > simple.html |
|||
for extended solution: java -cp . Csv2Html header < text.csv > extended.html</tt> |
|||
<lang java>import java.io.BufferedReader; |
|||
import java.io.InputStreamReader; |
|||
import java.io.PrintStream; |
|||
class Csv2Html { |
|||
public static String escapeChars(String lineIn) { |
|||
StringBuilder sb = new StringBuilder(); |
|||
int lineLength = lineIn.length(); |
|||
for (int i = 0; i < lineLength; i++) { |
|||
char c = lineIn.charAt(i); |
|||
switch (c) { |
|||
case '"': |
|||
sb.append("""); |
|||
break; |
|||
case '&': |
|||
sb.append("&"); |
|||
break; |
|||
case '\'': |
|||
sb.append("'"); |
|||
break; |
|||
case '<': |
|||
sb.append("<"); |
|||
break; |
|||
case '>': |
|||
sb.append(">"); |
|||
break; |
|||
default: sb.append(c); |
|||
} |
|||
} |
|||
return sb.toString(); |
|||
} |
|||
public static void tableHeader(PrintStream ps, String[] columns) { |
|||
ps.print("<tr>"); |
|||
for (int i = 0; i < columns.length; i++) { |
|||
ps.print("<th>"); |
|||
ps.print(columns[i]); |
|||
ps.print("</th>"); |
|||
} |
|||
ps.println("</tr>"); |
|||
} |
|||
public static void tableRow(PrintStream ps, String[] columns) { |
|||
ps.print("<tr>"); |
|||
for (int i = 0; i < columns.length; i++) { |
|||
ps.print("<td>"); |
|||
ps.print(columns[i]); |
|||
ps.print("</td>"); |
|||
} |
|||
ps.println("</tr>"); |
|||
} |
|||
public static void main(String[] args) throws Exception { |
|||
boolean withTableHeader = (args.length != 0); |
|||
InputStreamReader isr = new InputStreamReader(System.in); |
|||
BufferedReader br = new BufferedReader(isr); |
|||
PrintStream stdout = System.out; |
|||
stdout.print("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " |
|||
+ "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" |
|||
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">" |
|||
+ "<head><meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\"/>" |
|||
+ "<title>Csv2Html</title><style type=\"text/css\">" |
|||
+ "body{background-color:#FFF;color:#000;font-family:OpenSans,sans-serif;font-size:10px;}" |
|||
+ "table{border:0.2em solid #2F6FAB;border-collapse:collapse;}" |
|||
+ "th{border:0.15em solid #2F6FAB;padding:0.5em;background-color:#E9E9E9;}" |
|||
+ "td{border:0.1em solid #2F6FAB;padding:0.5em;background-color:#F9F9F9;}</style>" |
|||
+ "</head><body><h1>Csv2Html</h1>"); |
|||
stdout.println("<table>"); |
|||
String stdinLine; |
|||
boolean firstLine = true; |
|||
while ((stdinLine = br.readLine()) != null) { |
|||
String[] columns = escapeChars(stdinLine).split(","); |
|||
if (withTableHeader == true && firstLine == true) { |
|||
tableHeader(stdout, columns); |
|||
firstLine = false; |
|||
} else { |
|||
tableRow(stdout, columns); |
|||
} |
|||
} |
|||
stdout.println("</table></body></html>"); |
|||
} |
|||
} |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
<lang lua>FS = "," -- field separator |
<lang lua>FS = "," -- field separator |